Solved

Need multiple COUNTs with different criteria

Posted on 2009-05-05
4
1,132 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
GeoClustering  and AOG 25 43
Freeze portion of datamart 2 22
Finding Where Clause Value in SQL Views and SP 21 38
What Is an Error? 2 25
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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

749 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