2012-03-22 62 views
1

我有可稱爲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 
+0

no @bluefeet!按順序是「選擇convert(varchar(10),DateField,103)作爲DT,IsNull(Livre,0)AS Livre」。 – AymenRM 2012-03-22 13:41:15

回答

1

儘量明確地給你的派生列在子查詢的別名:

Select DT ,IsNull(Livre,0) AS Livre 
From @Days 
LEFT OUTER JOIN 
(SELECT DATEADD(day, DATEDIFF(day, '20040101', [date_livraison]), '20040101') as DT, 
     Count(*) AS Livre 
FROM Colis.dbo.information_livraison 
GROUP BY DATEADD(day, DATEDIFF(day, '20040101', [date_livraison]), '20040101')) LV 
ON DateField = DT 
ORDER BY DateField ASC 
1

我想你想這是你的選擇:

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') DT ,  Count(*) AS Livre 
    FROM @information_livraison 
    GROUP BY DATEADD(day, DATEDIFF(day, '20040101', [date_livraison]), '20040101')) LV 
    ON DateField = DT 
ORDER BY DateField ASC 

我添加了一個列名的DATEADD然後使用提供了join,這似乎給出你之後的結果。

+0

是的這項工作:) – AymenRM 2012-03-22 13:47:26

1

在子查詢中,您需要向第一列添加一個別名。然後,在連接到子查詢的ON子句中,使用別名。在這裏,我用XXX,使它明顯的(並添加別名「DD」,以表@days,爲清楚起見)

Select 
    convert(varchar(10), dd.DateField,103) as DT 
    ,IsNull(LV.Livre,0) AS Livre 
From @Days dd 
    LEFT OUTER JOIN (SELECT 
         DATEADD(day, DATEDIFF(day, '20040101', [date_livraison]), '20040101') xxx 
        ,Count(*) AS Livre 
        FROM Colis.dbo.information_livraison   
        GROUP BY DATEADD(day, DATEDIFF(day, '20040101', [date_livraison]), '20040101')) LV 
    ON dd.DateField = LV.xxx 
ORDER BY dd.DateField ASC 
+0

是的,這是我想要的..謝謝同志 – AymenRM 2012-03-22 13:48:47

0

你需要在你的子查詢提供一個別名爲您的日期字段:

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') as LDate, Count(*) AS Livre 
    FROM Colis.dbo.information_livraison 
    GROUP BY DATEADD(day, DATEDIFF(day, '20040101', [date_livraison]), '20040101') 
) LV 
    ON DateField = LV.LDate 
ORDER BY DateField ASC 

一,你這樣做,那麼當你在田野加盟,將閱讀:

ON DateField = LV.LDate