按日期部分分组,使用总月份日总订单

时间:2016-05-17 10:49:24

标签: sql-server datetime group-by

这是我的存储过程..每天有多少新订单和oldo rders在整个月有... 我是申报日期,总计和收入..

结果我只获得了第31个月的最后一天..我想要按日计数...

alter Procedure sp_NewandOld
(
@StartDate  DATETIME,        
@EndDate  DATETIME
)
--[sp_NewandOld] '01/01/2015','01/31/2015'
AS 
BEGIN   
---New Customer Orders Breakup
Declare @NewCount int, @NewRevenue int, @NewDate nvarchar(50)
select @NewDate=(datepart(day,od.OrderDate)),
@NewCount= count(*), 
@NewRevenue= SUM(CONVERT(decimal(18,2),od.TotalAmount)) from orderdetails od 
inner join customer c on od.customerid=c.customerid 
where Convert(Datetime,Convert(varchar(50),od.orderdate,101)) = Convert(Datetime,Convert(varchar(50),c.registereddate,101))
and Convert(Datetime,convert(varchar(50),od.orderdate,101)) between @StartDate and @EndDate
group by datepart(day, od.OrderDate)


Declare @OldCount int, @OldRevenue int, @OldDate nvarchar(50)
select @OldDate=(datepart(day,od.OrderDate)),
@OldCount= count(*), 
@OldRevenue=SUM(CONVERT(decimal(18,2),od.TotalAmount)) from orderdetails od 
inner join  customer c on od.customerid=c.customerid 
where Convert(Datetime,Convert(varchar(50),od.orderdate,101)) != Convert(Datetime,Convert(varchar(50),c.registereddate,101))
and Convert(Datetime,convert(varchar(50),od.orderdate,101)) between @StartDate and @EndDate
group by datepart(day, od.OrderDate)

select @NewDate,@NewCount,@OldCount,@NewRevenue,@OldRevenue
End

1 个答案:

答案 0 :(得分:0)

您只将第一行值选择到变量中,并且需要全部获取。将您的查询更改为此以获取所有数据:

alter Procedure sp_NewandOld (
    @StartDate  DATETIME,        
    @EndDate  DATETIME
)
AS 
BEGIN   

    with [new] as (
        select  datepart(month,od.OrderDate) as newdatemonth,
                datepart(day,od.OrderDate) as newdate,
                count(*) as NewCount, 
                SUM(CONVERT(decimal(18,2),od.TotalAmount)) as NewRevenue
        from orderdetails od 
        inner join customer c 
            on od.customerid=c.customerid 
        where Convert(Datetime,Convert(varchar(50),od.orderdate,101)) = Convert(Datetime,Convert(varchar(50),c.registereddate,101))
        and Convert(Datetime,convert(varchar(50),od.orderdate,101)) between @StartDate and @EndDate
        group by datepart(month,od.OrderDate), datepart(day, od.OrderDate)
    ), [old] as (
        select  datepart(month,od.OrderDate) as OldDateMonth,
                datepart(day,od.OrderDate) as OldDate,
                count(*) as OldCount, 
                SUM(CONVERT(decimal(18,2),od.TotalAmount)) as OldRevenue
        from orderdetails od 
        inner join customer c 
            on od.customerid=c.customerid 
        where Convert(Datetime,Convert(varchar(50),od.orderdate,101)) != Convert(Datetime,Convert(varchar(50),c.registereddate,101))
        and Convert(Datetime,convert(varchar(50),od.orderdate,101)) between @StartDate and @EndDate
        group by datepart(month,od.OrderDate) , datepart(day, od.OrderDate)
    )

    select  n.newdatemonth,
            n.NewDate,
            n.NewCount,
            o.OldCount,
            n.NewRevenue,
            o.OldRevenue
    from [new] n
    inner join [old] o
        on n.newdate = o.olddate and n.newdatemonth = o.OldDateMonth

End