2013-11-22 43 views
0

下面是代碼。我需要明智的結果日期。爲什麼Group By子句不起作用?

Declare @tempInvoiceStatus TABLE 
    (
     [InvNum] [varchar](25) NOT NULL, 
     [ExportStatus] [int] NOT NULL, 
     [ExportFailReason] [varchar](max) NULL, 
     [ImportStatus] [int] NULL, 
     [ImportFailReason] [varchar](max) NULL, 
     [ExportDateTime] [datetime] NULL, 
     [InvoiceType] [varchar](50) NOT NULL, 
     [ExportType] [varchar](50) NOT NULL 
    ); 

    Insert @tempInvoiceStatus 
    select * from InvoiceStatus where CONVERT(VARCHAR(10),ExportDateTime,10) between CONVERT(VARCHAR(10),@StartDate,10) and CONVERT(VARCHAR(10), @EndDate,10) 


    select CONVERT(VARCHAR(10),ExportDateTime,10)as ExportDate, COUNT(*) as Total_Records, 
    (select COUNT(ExportStatus) from @tempInvoiceStatus I2 where I2.ExportDateTime=I1.ExportDateTime 
    and ExportStatus=1)as Success, 
    (select COUNT(ExportStatus) from @tempInvoiceStatus I3 where I3.ExportDateTime=I1.ExportDateTime 
    and ExportStatus=2)as Failed 
    from @tempInvoiceStatus I1 group by (Cast(ExportDateTime as DATE))order by ExportDateTime 

我需要明智的結果日期。爲什麼我會收到以下錯誤?

Column 'InvoiceStatus.ExportDateTime' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. 

回答

1

你必須使用相同的元素SELECTORDER BY和內查詢,在GROUP BY

select CONVERT(VARCHAR(10),Cast(ExportDateTime as DATE),10)as ExportDate, COUNT(*) as Total_Records, 
(select COUNT(ExportStatus) from @tempInvoiceStatus I2 where Cast(I2.ExportDateTime as date)=Cast(I1.ExportDateTime as DATE) 
and ExportStatus=1)as Success, 
(select COUNT(ExportStatus) from @tempInvoiceStatus I3 where cast(I3.ExportDateTime as date)=Cast(I1.ExportDateTime as DATE) 
and ExportStatus=2)as Failed 
from @tempInvoiceStatus I1 group by (Cast(ExportDateTime as DATE)) 
order by (Cast(ExportDateTime as DATE)) 
+0

我試過了,但即使這不起作用,並拋出上述相同的錯誤。 :( – user1551056

+0

對不起,我錯過了'order by',現在試試 – Szymon

+0

Nopes它不工作 – user1551056

0

InvoiceStatus.ExportDateTime添加到group by子句中。列應在SelectGroup By中相同。因爲您使用的是ExportDateTime而不是(Cast(ExportDateTime as DATE)),您會收到錯誤消息。

select CONVERT(VARCHAR(10),ExportDateTime,10)as ExportDate, COUNT(*) as Total_Records, 
    (select COUNT(ExportStatus) from @tempInvoiceStatus I2 where I2.ExportDateTime=I1.ExportDateTime 
    and ExportStatus=1)as Success, 
    (select COUNT(ExportStatus) from @tempInvoiceStatus I3 where I3.ExportDateTime=I1.ExportDateTime 
    and ExportStatus=2)as Failed 
    from @tempInvoiceStatus I1 group by ExportDateTime)) order by ExportDateTime 
+0

它給了我這個錯誤。 無法綁定多部分標識符「InvoiceStatus.ExportDateTime」。 – user1551056

+0

在group by中,只指定'ExportDateTime'而不是'(Cast(ExportDateTime as DATE))' – Tilak

+0

不,如果我在同一個日期有兩個交易,我不想同時顯示兩個交易。僅編寫ExportDate時間將多次獲取同一日期的交易。 – user1551056

0

您必須在SELECT語句中包含要指定的列以將其放入GROUP..BY子句中。因此,將InvoiceStatus.ExportDateTime列添加到GROUP..BY子句中。

0
select CONVERT(VARCHAR(10),ExportDateTime,10)as ExportDate, COUNT(*) as Total_Records, 
    (select COUNT(ExportStatus) from @tempInvoiceStatus I2 where I2.ExportDateTime=I1.ExportDateTime 
    and ExportStatus=1)as Success, 
    (select COUNT(ExportStatus) from @tempInvoiceStatus I3 where I3.ExportDateTime=I1.ExportDateTime 
    and ExportStatus=2)as Failed 
    from @tempInvoiceStatus I1 group by **ExportDateTime** order by ExportDateTime 
+0

我沒有跟着你。請你簡單介紹一下。 – user1551056

+0

在羣組中,您可以使用「Cast(ExportDateTime as DATE)」的ExportDateTime組的「insteaad」) – somesh