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
Example - Two Conditions in the WHERE Clause (AND Condition)You can use the AND condition in the WHERE clause to specify more than 1 condition that must be met for the record to be selected.
Syntax. SELECT column1, column2, columnN FROM table_name WHERE [condition1] AND [condition2]... AND [conditionN]; You can combine N number of conditions using the AND operator.
You can specify multiple conditions in a single WHERE clause to, say, retrieve rows based on the values in multiple columns. You can use the AND and OR operators to combine two or more conditions into a compound condition. AND, OR, and a third operator, NOT, are logical operators.
Answer. Yes, within a WHERE clause you can compare the values of two columns.
You can also use
SELECT m.count, ytd.count FROM
(SELECT COUNT( id ) count FROM table WHERE date BETWEEN '2010-06-01' AND '2010-06-30') m,
(SELECT COUNT( id ) count FROM table WHERE date BETWEEN '2010-01-01' AND '2010-06-30') ytd
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With