How can I return two columns that each use different WHERE critia? Obviously, this won’t work:
SELECT Name, COUNT(Column1) AS Total, COUNT(Column1) AS YearToDate
FROM Table1
WHERE Occurred_Date BETWEEN '2010-06-01' AND '2010-06-30' --Total
WHERE Occurred_Date BETWEEN '2010-01-01' AND '2010-06-30' --YearToDate
This is the output I’m looking for:
Name | Total | YTD
-------------------
Item1 | 2 | 3
Item2 | 4 | 8
If you specify a column name for the
COUNTfunction, it doesn’t countNULLvalues.So, the simple way would be to use CASE statements to convert the values you don’t want counted to
NULLI’m not 100% certain the query engine will let you use CASE within COUNT (I’m not even sure what DB platform you’re using), but it gives you the idea. If this way doesn’t work, you can write the query using a derived table that will give you the same result.