Jump to content

JOIN ISSUE


Crazy Horse

Recommended Posts

Hi, thanks in advance for any help

 

I have the following 2 statements which I need to combine, however I need to show all records (10) from the first statement and 0 if the count returns nothing from the second statement.

I can get them working fine and even combined working fine - except i always need to show 10 records even if count is empty (so show 0), but i only currently show 9 because one team has no count total

 

This statement gets the list of teams from one table and returns 10 records

 

SELECT teamname

FROM miteams

WHERE deptcode LIKE 'LEGSERV%' AND groupcode IS NULL

ORDER BY teamname ASC

 

This statement counts all the records from another table

 

SELECT feteamname, COUNT(fileopened)

FROM midata

WHERE feteamname NOT IN ('Bike','Golf','Pretend','Contact Centre','Legal Services','Multi Track','Small Claims','') AND fileopened >= '2008-04-01' AND fileopened <= '2008-04-30' AND scotdesc !='Yes' AND sourcename = 'ACM ULR Ltd' AND rejectedreasons !='MIC' AND (smclaim = 'No' OR (smclaim = 'Yes' AND rejectedreasons !='TBA')) AND prodclassdesc !='BIKE' AND (rejectedreasons = 'D - Reject - MIB Untraced' OR rejectedreasons = 'A - Accepted English' OR rejectedreasons LIKE '%B - Minster%')

GROUP BY feteamname

ORDER BY feteamname ASC

 

The common factor to join on is midata.feteamname and miteams.teamname

 

I have tried left joins and left outer joins but i think i need brackets in the right places

 

Hope this makes sense, thanks again

Link to comment
Share on other sites

Try something like this:

SELECT t.teamname, COUNT(d.fileopened)
FROM miteams t
INNER JOIN midata d ON d.feteamname = t.teamname
WHERE t.deptcode LIKE 'LEGSERV%' 
AND t.groupcode IS NULL
AND d.feteamname NOT IN ('Bike','Golf','Pretend','Contact Centre','Legal Services','Multi Track','Small Claims','')
AND d.fileopened BETWEEN '2008-04-01' AND '2008-04-30' 
AND d.scotdesc != 'Yes' 
AND d.sourcename = 'ACM ULR Ltd' 
AND d.rejectedreasons != 'MIC' 
AND (d.smclaim = 'No' OR (d.smclaim = 'Yes' AND d.rejectedreasons !='TBA')) 
AND d.prodclassdesc != 'BIKE' 
AND (d.rejectedreasons = 'D - Reject - MIB Untraced' OR d.rejectedreasons = 'A - Accepted English' OR d.rejectedreasons LIKE '%B - Minster%')
GROUP BY teamname
ORDER BY teamname ASC

 

The INNER JOIN should restrict  your records as you're wanting. Otherwise, it's practically identical to smashing your two queries together.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.