T-SQL代码不会提供所需的输出

时间:2012-11-27 17:48:48

标签: sql sql-server sql-server-2008 tsql

SELECT 
Counting = ISNULL(COUNT(A.Numbers), 0),
B.Date AS DateX
From Schema1.Table A INNER JOIN Schema2.Table B
ON A.xyz=B.xyz
Where B.Date = GetDate()
Group by B.Date

有时,B.Date将不会有今天的日期。我仍然希望结果输出为Counting = 0 and DateX = Todays date

我该怎么做?

谢谢

受欢迎的需求

编辑

A.Numbers = 123;456;789;012,...etc
B.Date = 2012-11-24, 2012-11-24,-212-11-24,2012-11-26

2012-11-24 = 32012-11-26 = 1计算,但在将B.Date设置为2012-11-25时没有输出 我想在Counting = 0

时显示B.Date = 2012-11-25

1 个答案:

答案 0 :(得分:3)

摆脱你的where子句并将你的选择更改为

Counting = (Case when B.Date = GETDATE() then ISNULL(COUNT(A.Numbers),0) else 0 end)