为什么Group By子句不起作用?

时间:2013-11-22 07:01:06

标签: sql-server group-by aggregate-functions

以下是代码。我明智地需要结果。

    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.

4 个答案:

答案 0 :(得分: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)) 

答案 1 :(得分: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 

答案 2 :(得分:0)

您必须在SELECT语句中包含要指定的列才能将其包含在GROUP..BY子句中。因此,将 InvoiceStatus.ExportDateTime 列添加到GROUP..BY子句中。

答案 3 :(得分: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