如何返回两列各自使用不同WHERE标准的列?显然,这不起作用:
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
这是我正在寻找的输出:
Name | Total | YTD
-------------------
Item1 | 2 | 3
Item2 | 4 | 8
答案 0 :(得分:14)
如果为COUNT
函数指定列名,则不会计算NULL
个值。
因此,简单的方法是使用CASE语句将您不想要的值转换为NULL
SELECT
Name,
COUNT(CASE
WHEN Occurred_Date >= '2010-01-01' AND Occurred_Date < '2011-01-01'
THEN Occurred_Date
ELSE NULL
END) AS [YTD]
COUNT(CASE
WHEN Occurred_Date >= '2010-06-01' AND Occurred_Date < '2011-07-01'
THEN Occurred_Date
ELSE NULL
END) AS [MTD]
FROM Table1
GROUP BY Name
我不是100%肯定查询引擎会让你在COUNT中使用CASE(我甚至不确定你正在使用什么数据库平台),但它给你的想法。如果这种方式不起作用,您可以使用派生表编写查询,该表将为您提供相同的结果。
答案 1 :(得分:1)
对于UNION来说,这似乎是一个很好的情况。
SELECT Name, COUNT(Column1) AS Total, COUNT(Column1) AS YearToDate
FROM Table1
WHERE Occurred_Date BETWEEN '2010-06-01' AND '2010-06-30' --Total
UNION
SELECT Name, COUNT(Column1) AS Total, COUNT(Column1) AS YearToDate
FROM Table1
WHERE Occurred_Date BETWEEN '2010-01-01' AND '2010-06-30' --YearToDate
基本上,您分别执行两个查询,然后将它们合并为一组。
不确定您使用的是哪种类型的数据库,但这里是SQL Server和MySql的链接。
答案 2 :(得分:1)
您也可以使用
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
答案 3 :(得分:0)
你可以在COUNT上使用MySQL IF语句
答案 4 :(得分:0)
SELECT COALESCE(CurrMonth.Name, YTD.Name) AS Name, CurrMonth.Total AS Total, YTD.Total AS YearToDate
FROM (
SELECT Name, COUNT(Column1) AS Total
FROM Table1
WHERE Occurred_Date BETWEEN '2010-06-01' AND '2010-06-30' --Total
GROUP BY Name
) AS CurrMonth
FULL OUTER JOIN
(
SELECT Name, COUNT(Column1) AS Total
FROM Table1
WHERE Occurred_Date BETWEEN '2010-01-01' AND '2010-06-30' --YearToDate
GROUP BY Name
) AS YTD
ON CurrMonth.Name = YTD.Name
完整的外连接不是必需的,只是演示了如何处理一个集合不是另一个集合的严格子集的情况。我通常会使用YTD子查询LEFT JOIN到当前月子查询。
另一种策略 - 使用CASE:
SELECT Name
,COUNT(CASE WHEN Occurred_Date BETWEEN '2010-06-01' AND '2010-06-30' THEN Column1 ELSE NULL END) AS Total
,COUNT(CASE WHEN Occurred_Date BETWEEN '2010-01-01' AND '2010-06-30' THEN Column1 ELSE NULL END) AS YearToDate
FROM Table1
WHERE Occurred_Date BETWEEN '2010-06-01' AND '2010-06-30' -- (unnecessary)
OR Occurred_Date BETWEEN '2010-01-01' AND '2010-06-30'
GROUP BY Name
答案 5 :(得分:0)
SELECT Name
,SUM(
CASE WHEN Occurred_Date BETWEEN '2010-06-01' AND '2010-06-30'
THEN 1
ELSE 0
END) AS Total
,SUM(
CASE WHEN Occurred_Date BETWEEN '2010-01-01' AND '2010-06-30'
THEN 1
ELSE 0
END) AS YearToDate
FROM Table1
GROUP BY Name
编辑:这应该在SQL Server中有效。
答案 6 :(得分:0)
SELECT DISTINCT m.field1, b.field2,
SUM (CASE
WHEN m.created_on >=
TO_DATE ('15/11/2012', 'DD/MM/YYYY')
AND m.created_on <
TO_DATE ('15/11/2012', 'DD/MM/YYYY')
+ 1
THEN 1
ELSE 0
END
) AS count1,
COUNT (1) AS count2
FROM table1 m, table2 b
WHERE m.field1 IN (SELECT DISTINCT field1
FROM table1)
AND b.field1 = m.field1
GROUP BY m.field1, b.field2
ORDER BY count2 DESC;