Sql-server – how can i get an accurate count based on max date when joining 3 tables when one of the join fields is many to 1

database-designoraclesql server

So, I have 3 tables that I am attempting to get counts for based on a groupid, and a task code. There are a few issues I am having as some of the relationships are many to one, which I think is somehow inflating my counts. I will list my 3 tables with the pertinent attributes.

task_table contains:

task_code – would like to get the counts of each one in a group id, would like to use the latest instance basedon event date.

sol_id -used to join to worktable; many sol_id to one m_id is possible

edate -need to use to get one record

cur_id – where cur_id = 1 in the where clause

worktable contains:

sol_id – used to join to task_table

m_id – used to join to grouptable

grouptable contains:

m_id

groupid- used to group the task_code to get count

I'd like the end result to look like:

group_idtask_counttask
555545A
55554N
562467A
562423O
562442X

I have been attempting to run a number of queries, but the counts I am getting back do not look correct. I am concerned that it is somehow returning more than one instance of the m_id somehow? Here is the query in question:

select  c.groupid, count(c.groupid) group_count, a.task_code from task_table a
join worktable b
on a.sol_id = b.sol_id
join grouptable c
on b.m_id= c.m_id
where   a.cur_id = 1 and a.task_code is not null 
group by c.groupid, a.task_code;

If I add 'edate = (select max(edate) from task_table)' in the where clause, it returns an empty table.

I am unsure how to incorporate edate to get only the newest record that fits the criteria in the where clause. The reason I think I want to use this is because there could be more than one sol_id that is associated with a m_id, so i'd just like to include only the newest record with a cur_id in the count. Thank you for your time.

sample data

task_table

task_codesol_idedatecur_id
A236/7/091
A246/4/091
A236/10/090
B456/2/091
B426/3/091
C3410/8/100
C839/10/091

work table

sol_idm_id
231234
241234
451832
421343
837623

group table

m_idgroup_id
1234A76
1832Y23
1343A76
7623Y23

looking at these tables, the result should look like the following

group_idtask_counttask
A762A
Y231C

( A76 should only count sol_id 23 and 42) ( Y23 should only count sol_id 83)

Best Answer

mmm based on your sample date your query is not wrong. but your expected results are wrong.

select
    a.task_code,a.sol_id,a.cur_id
    ,b.m_id
    ,c.groupid
FROM
    task_table AS a
    inner join worktable AS b on a.sol_id = b.sol_id
    inner join grouptable AS c on b.m_id= c.m_id
    where
        a.cur_id = 1 and a.task_code is not null
ORDER BY c.groupid, b.sol_id;

select
    c.groupid
    ,count(c.groupid) AS group_count
    ,a.task_code
from task_table AS a
join worktable AS b on a.sol_id = b.sol_id
join grouptable AS c on b.m_id= c.m_id
where
    a.cur_id = 1 and a.task_code is not null 
group by
    c.groupid, a.task_code
;

enter image description here

the group_id A76 is linked to the work table with sol_id 23,24 and 42; 23 and 24 are task_code A. 42 is task_code B. Y23 has two m_id that connect sol_id 45 and 83 to task code A and B. So the second output in the image is as you expect but has 2 more rows related to the task B. Maybe the sample date is wrong because a can't see a wrong result to fix.