我有可稱爲information_livraison不能按內部別名分組嗎?
Num_bloc date_livraison
14104 2012-03-22 09:18:43.000
14202 2012-03-22 09:18:43.000
13276 2012-03-22 09:19:39.000
12775 2012-03-22 09:24:59.000
12967 2012-03-22 09:29:18.000
13809 2012-03-22 09:50:14.000
14611 2012-03-22 10:04:56.000
10320 2012-03-22 10:05:33.000
14593 2012-03-22 10:06:20.000
15179 2012-03-22 10:06:43.000
我必須建立一個選擇日期範圍的查詢,並返回它的每一個日期的計數表
我的查詢:
Declare @Days Table (DateField datetime)
Declare @CurrentDate datetime
Declare @EndDate datetime
Set @CurrentDate = (SELECT DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0))
Set @EndDate = (SELECT DATEADD(ms,- 3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE())+1,0))))
While @CurrentDate <= @EndDate
Begin
Insert Into @Days Values(@CurrentDate)
Set @CurrentDate = DateAdd(d,1,@CurrentDate)
End
Select convert(varchar(10),DateField,103) as DT ,IsNull(Livre,0) AS Livre
From @Days
LEFT OUTER JOIN
(SELECT DATEADD(day, DATEDIFF(day, '20040101', [date_livraison]), '20040101') , Count(*) AS Livre
FROM Colis.dbo.information_livraison
GROUP BY DATEADD(day, DATEDIFF(day, '20040101', [date_livraison]), '20040101')) LV
ON DateField = DATEADD(day, DATEDIFF(day, '20040101', [date_livraison]), '20040101')
ORDER BY DateField ASC
GO
生成錯誤
Msg 8155,Level 16,State 2,Line 17
Aucune colonnespécifiéepour la colonne 1 de'LV'。
Msg 207,Level 16,State 1,Line 25
Nom de colonne non valide:'date_livraison'。
我想這個結果
DT Livre
01/03/2012 0
02/03/2012 0
03/03/2012 0
04/03/2012 0
05/03/2012 0
......
21/03/2012 10
22/03/2012 0
......
29/03/2012 0
30/03/2012 0
31/03/2012 0
no @bluefeet!按順序是「選擇convert(varchar(10),DateField,103)作爲DT,IsNull(Livre,0)AS Livre」。 – AymenRM 2012-03-22 13:41:15