Solved

Need multiple COUNTs with different criteria

Posted on 2009-05-05
4
1,127 Views
Last Modified: 2012-05-06
I've got a query that's giving me an accurate count based on a searchable date range.  What I'd like to add is a year-to-date count as well -- I can do this with two different queries and then combine them programatically, but I'm sure there's a way to do this in SQL directly but I have no idea how to do so.  Here are the two queries that I've got so far:

SELECT pmUser.userFName, pmUser.userLName, count(woID) as woCount
FROM tblWO
JOIN tblAssetContractLink ON tblWO.assetContractLinkID = tblAssetContractLink.assetContractLinkID
JOIN tblAssets ON tblAssetContractLink.assetID = tblAssets.assetID
JOIN tblSpace ON tblAssets.spaceID = tblSpace.spaceID
JOIN tblTeamMember pmTM ON tblSpace.coID = pmTM.coID AND pmTM.teamPositionID = 1
JOIN tblUsers pmUser ON pmTM.userID = pmUser.userID
JOIN tblTeamMember secTM ON tblSpace.cOID = secTM.coID AND secTM.userID = 9
WHERE woCreatedDate BETWEEN '5/1/2009' AND '5/30/2009'
GROUP BY userFName, userLName
ORDER BY woCount DESC, userLName ASC

SELECT pmUser.userFName, pmUser.userLName, count(woID) as woCount
FROM tblWO
JOIN tblAssetContractLink ON tblWO.assetContractLinkID = tblAssetContractLink.assetContractLinkID
JOIN tblAssets ON tblAssetContractLink.assetID = tblAssets.assetID
JOIN tblSpace ON tblAssets.spaceID = tblSpace.spaceID
JOIN tblTeamMember pmTM ON tblSpace.coID = pmTM.coID AND pmTM.teamPositionID = 1
JOIN tblUsers pmUser ON pmTM.userID = pmUser.userID
JOIN tblTeamMember secTM ON tblSpace.cOID = secTM.coID AND secTM.userID = 9
WHERE woCreatedDate BETWEEN '1/1/2009' AND '5/30/2009'
GROUP BY userFName, userLName
ORDER BY woCount DESC, userLName ASC

What I'd like to get is a way to display 0 for woCount (the one using the monthly search range) since it's possible that a particular user would have a 0 in that field but still have a greater number for the year-to-date number.

Thanks very much in advance!
0
Comment
Question by:saoirse1916
4 Comments
 
LVL 26

Accepted Solution

by:
tigin44 earned 500 total points
ID: 24306148
try the code below
SELECT pmUser.userFName, pmUser.userLName, 

   SUM(CASE WHEN woCreatedDate BETWEEN '1/1/2009' AND '5/30/2009' THEN 1 ELSE O END) woCount1,

   SUM(CASE WHEN woCreatedDate BETWEEN '5/1/2009' AND '5/30/2009' THEN 1 ELSE O END) woCount2

FROM tblWO

JOIN tblAssetContractLink ON tblWO.assetContractLinkID = tblAssetContractLink.assetContractLinkID

JOIN tblAssets ON tblAssetContractLink.assetID = tblAssets.assetID

JOIN tblSpace ON tblAssets.spaceID = tblSpace.spaceID

JOIN tblTeamMember pmTM ON tblSpace.coID = pmTM.coID AND pmTM.teamPositionID = 1

JOIN tblUsers pmUser ON pmTM.userID = pmUser.userID

JOIN tblTeamMember secTM ON tblSpace.cOID = secTM.coID AND secTM.userID = 9

WHERE woCreatedDate BETWEEN '5/1/2009' AND '5/30/2009'

GROUP BY userFName, userLName

ORDER BY woCount DESC, userLName ASC

Open in new window

0
 
LVL 41

Expert Comment

by:ralmada
ID: 24306169
Can you clarify a bit what you are looking for? Maybe posting some sample data and expected results will help. Anyhow, you can have a conditional count using SUM instead, like this:
SELECT

...

SUM(CASE WHEN woCreatedDate BETWEEN '5/1/2009' AND '5/30/2009' THEN 1 else 0 end) as monthcount,

count(woID) as woCount

FROM tblWO

JOIN tblAssetContractLink ON tblWO.assetContractLinkID = tblAssetContractLink.assetContractLinkID

JOIN tblAssets ON tblAssetContractLink.assetID = tblAssets.assetID

JOIN tblSpace ON tblAssets.spaceID = tblSpace.spaceID

JOIN tblTeamMember pmTM ON tblSpace.coID = pmTM.coID AND pmTM.teamPositionID = 1

JOIN tblUsers pmUser ON pmTM.userID = pmUser.userID

JOIN tblTeamMember secTM ON tblSpace.cOID = secTM.coID AND secTM.userID = 9

WHERE woCreatedDate BETWEEN '1/1/2009' AND '5/30/2009'

GROUP BY userFName, userLName

ORDER BY woCount DESC, userLName ASC

Open in new window

0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 24306198
How about using UNION?

SELECT pmUser.userFName, pmUser.userLName, count(woID) as 'monthlycount', 0 as 'yeartodatecount'
FROM tblWO
<WHERE clause>
UNION
SELECT pmUser.userFName, pmUser.userLName, 0 as 'monthlycount', count(woID) as 'yeartodatecount'
FROM tblWO
<WHERE clause>

0
 
LVL 8

Author Closing Comment

by:saoirse1916
ID: 31578035
That did it -- had to make two tweaks, but now it's working great!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now