Solved

Need multiple COUNTs with different criteria

Posted on 2009-05-05
4
1,133 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

737 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