下面是代碼。我需要明智的結果日期。爲什麼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.
我試過了,但即使這不起作用,並拋出上述相同的錯誤。 :( – user1551056
對不起,我錯過了'order by',現在試試 – Szymon
Nopes它不工作 – user1551056