我想提出一个请求,从昨天开始计算第一列的所有结果,并为第二列做同样的事情,但是为期2天。
下面的请求不起作用,因为我想找到一种方法将Where子句与特定列关联。
SELECT
Count(*) as '1 day',
Count(*) as '2 days'
FROM myTable tbl
WHERE
DATEADD(day, 1, MyTimeStampToCompare) >= DATEADD(day, -1, GETDATE()) as '1 day' and
DATEADD(day, 1, MyTimeStampToCompare) >= DATEADD(day, -3, GETDATE()) as '2 days'
我正在研究SQL Server 2012。
由于
答案 0 :(得分:0)
而不是在where子句中将其修改为CASE
函数内的COUNT
语句
SELECT COUNT(CASE WHEN DATEADD(day, 1, MyTimeStampToCompare) >= DATEADD(day, -1, GETDATE()) THEN 1
ELSE NULL
END) AS '1 Day'
,COUNT(CASE WHEN DATEADD(day, 1, MyTimeStampToCompare) >= DATEADD(day, -2, GETDATE()) THEN 1
ELSE NULL
END) AS '2 Day'
FROM MyTable
WHERE MyTimeStampToCompare >= DATEADD(day, -4, GETDATE())
在where子句中,我包括最后4天缩小返回的记录数量,使其更快。
答案 1 :(得分:0)
长时间不使用Ms SQL,但是,你似乎需要这样的东西吗?
SELECT
SUM(CASE WHEN DATEADD(day, 1, first_date_col)>= GETDATE() THEN 1 ELSE 0 END) as '1 day',
SUM(CASE WHEN DATEADD(day, 2, secornd_date_col)>= GETDATE() THEN 1 ELSE 0 END) as '2 day',
FROM myTable tbl
(修复!@Vladimir:打算在写入1时使用从开头的总和0,只是忘记更改COUNT:)
答案 2 :(得分:0)
你应该确保WHERE子句可以追溯到你想要的最远的计数。
您可以像我这样使用SUM,或像其他答案那样使用COUNT。 COUNT依赖于NULL不被计数,并且可以触发“警告:Null值被聚合消除......”警告。
DECLARE @DateTable TABLE
(
MyTimeStampToCompare datetime
);
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate()-7);
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate()-1);
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate()-1);
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate()-1);
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate()-1);
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate()-1);
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate()-5);
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate()-3);
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate()-5);
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate()-2);
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate()-2);
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate()-2);
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate());
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate()-3);
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate()-6);
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate()-8);
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate()-9);
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate()+1);
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate()-1);
INSERT INTO @DateTable (MyTimeStampToCompare) VALUES (getdate());
SELECT
SUM
(
CASE
WHEN MyTimeStampToCompare >= DATEADD(day, -1, FLOOR(CONVERT(float,getdate()))) THEN
1
ELSE
0
END
) AS EverythingSince1DayAgo,
SUM
(
CASE
WHEN MyTimeStampToCompare >= DATEADD(day, -2, FLOOR(CONVERT(float,getdate()))) THEN
1
ELSE
0
END
) AS EverythingSince2DaysAgo
FROM @DateTable
-- you said you want everything from 2 days ago; this makes sure times are stripped off of the current date so it's midnight this morning, and then marches back 2 days
-- SQL Server 2008+ has fancier mechanisms for removing the time from a date, but I don't have convenient access to test.
WHERE MyTimeStampToCompare >= DATEADD(day, -2, FLOOR(CONVERT(float,getdate())));
/*
Original poster's errant code:
SELECT
Count(*) as '1 day',
Count(*) as '2 days'
FROM myTable tbl
WHERE
DATEADD(day, 1, MyTimeStampToCompare) >= DATEADD(day, -1, GETDATE()) as '1 day' and
DATEADD(day, 1, MyTimeStampToCompare) >= DATEADD(day, -3, GETDATE()) as '2 days'
*/