我正在尝试统计昨天的所有行。我的查询运行良好,但未选择空值。有没有办法查询空值和非空值的计数?
这是我的代码:
SELECT dateadd(day,datediff(day,0,GETDATE())-1,0) as Received_Date,
COUNT(*) as Enrollments_Completed
FROM Table CD,
CCMDB.dbo.ResolutionLetterDetails RD
WHERE CD.ccid = RD.ccid
and CompletedDate >= DATEADD(d,DATEDIFF(d,1,getdate()),0)
and CompletedDate < DATEADD(d,DATEDIFF(d,0,getdate()),0)
AND CatID in('cat0014')
AND IncomingType <> 'RITS'
AND status = 'Completed'
答案 0 :(得分:0)
将CompletedDate
转换为没有时间的日期,并使其等于没有时间的昨天日期(来自GETDATE()
),并使用正确的JOIN
代码。
SELECT dateadd(day,datediff(day,0,GETDATE())-1,0) as Received_Date,
COUNT(*) as Enrollments_Completed
FROM Table CD
LEFT JOIN CCMDB.dbo.ResolutionLetterDetails RD ON CD.ccid = RD.ccid
WHERE dateadd(day,datediff(day,1,CompletedDate),0) = dateadd(day,datediff(day,1,GETDATE()),0)
AND CatID IN ('cat0014')
AND IncomingType != 'RITS'
AND status = 'Completed'
返回NULL
:
SELECT dateadd(day,datediff(day,0,GETDATE())-1,0) as Received_Date,
COUNT(*) as Enrollments_Completed
FROM Table CD
LEFT JOIN CCMDB.dbo.ResolutionLetterDetails RD ON CD.ccid = RD.ccid
WHERE dateadd(day,datediff(day,1,CompletedDate),0) = dateadd(day,datediff(day,1,GETDATE()),0)
AND (CatID IN ('cat0014') OR CatID IS NULL)
AND (IncomingType != 'RITS' OR IncomingType IS NULL)
AND (status = 'Completed' OR status IS NULL)
答案 1 :(得分:0)
我会解决您的查询并执行以下操作:
SELECT fi.movie_title, group_concat(actor_name_column)
FROM film_info fi
INNER JOIN film_actor using(film_id)
INNER JOIN actor_info using(actor_id)
group by film_id
对于日期部分,您也可以执行以下操作:
CAST(完成日期为DATE)= CAST(DATEADD(天,-1,GETDATE())为DATE)
此版本甚至在SQL Server中都是索引安全的(尽管不一定在其他数据库中)。
注意:
SELECT distinct player_id
FROM ticket
WHERE game_code IN ('17','16') AND
create_date BETWEEN '2018-05-01'
AND '2018-05-31'
GROUP BY player_id
HAVING COUNT(*) = 2;
数据类型大大简化了计算。SELECT CAST(DATEADD(day, -1, GETDATE()) as DATE) as Received_Date,
COUNT(*) as Enrollments_Completed
FROM Table CD JOIN
CCMDB.dbo.ResolutionLetterDetails RD
ON CD.ccid = RD.ccid
WHERE CompletedDate >= CAST(DATEADD(day, -1, GETDATE()) as DATE) AND
CompletedDate < CAST(GETDATE() as DATE) AND
CatID IN ('cat0014') AND
IncomingType <> 'RITS' AND
status = 'Completed';
子句中使用逗号。 始终使用正确的,明确的,标准 DATE
语法。