T-SQL聚合查询不起作用

时间:2015-06-27 21:50:26

标签: sql-server tsql aggregate-functions

我试图根据“按日发货”查找迟到的所有包裹和包裹物品的汇总。这是我到目前为止,并且计数在最后的聚合查询中是不正确的。我希望今天发货的包裹(PackageId 123456)和包裹物品数量为1(假设今天是2015年6月27日)

以下是我遇到的示例代码 WHNameWHId代表仓库名称和ID 非常感谢您的帮助

 use test
go

create table #packages
(
  PackageId int ,
  WHName varchar(100),
  WHId int,
  ShipByDate date
)

insert into #packages 
values
(
  123,
  'JAX',
  1,
  '06/25/2015'
),
(
  1234,
  'OH',
  2,
  '06/26/2015'
),
(
  12345,
  'JAX',
  1,
  '06/24/2015'
),
(
  123456,
  'DS',
  3,
  '06/27/2015'
),
(
  1234567,
  'DS',
  3,
  '06/27/2015'
  ),
  (
  12345678,
  'DS',
  3,
  '06/27/2015'
  )



create table #packageItems
(
 PackageItemId int,
 PackageId int,
 Qty int
)

insert into #PackageItems

values
(
  1,
  123,
  2
),
(
  2,
  12345678,
  0
),
(
 3,
 1234,
 3
),
(
 4,
 12345,
 2
),
(
 5,
 123,
 3
),
(
 6,
 123456,
 3

),
(
7,
12345,
10
)

create table #PackageSum
(
  PackageId int,
  ItemsCount int
)

insert into #PackageSum(PackageId,ItemsCount)

select
    pki.PackageId,
    count(pki.PackageItemId) as Items
from
    #packageItems pki
inner join #packages pk
    on pki.PackageId = pk.PackageId
where
    pki.Qty > 0

group by
    pki.PackageId




select * from #Packages
select * from #PackageItems
select * from #PackageSum

declare @Now datetime2(7) = getdate();

select  
    pk.WHName,
    SUM(case when datediff(day,pk.ShipByDate,@Now) = 0 then 1 else 0 end) as PackagesDueToday,
    SUM(case when datediff(day,pk.ShipByDate,@Now)  = 0 then pks.ItemsCount else 0 end) as PackageItemsDueToday,
    SUM(case when datediff(day,pk.ShipByDate,@Now) between 1 and 2  then 1 else 0 end) as Packages1To2DaysOld,  
    SUM(case when datediff(day,pk.ShipByDate,@Now) between 1 and 2  then pks.ItemsCount else 0 end) as PackageItems1To2DaysOld,
    SUM(case when datediff(day,pk.ShipByDate,@Now) between 2 and 3  then 1 else 0 end) as Packages2To3DaysOld,  
    SUM(case when datediff(day,pk.ShipByDate,@Now) between 2 and 3  then pks.ItemsCount else 0 end) as PackageItems2To3DaysOld
from
    #packages pk
inner join #PackageSum pks
    on pk.PackageId = pks.PackageId
group by
pk.WHName



drop table  #packages
drop table #PackageItems

drop table #PackageSum

--=============================== Another way of doing he same above=====================================
use test
go

create table #packages
(
  PackageId int ,
  WHName varchar(100),
  WHId int,
  ShipByDate date
)

insert into #packages 
values
(
  123,
  'JAX',
  1,
  '06/25/2015'
),
(
  1234,
  'OH',
  2,
  '06/26/2015'
),
(
  12345,
  'JAX',
  1,
  '06/24/2015'
),
(
  123456,
  'DS',
  3,
  '06/27/2015'
),
(
  1234567,
  'DS',
  3,
  '06/28/2015'
  ),
  (
  12345678,
  'DS',
  3,
  '06/18/2015'
  )



create table #packageItems
(
 PackageItemId int,
 PackageId int,
 Qty int
)

insert into #PackageItems

values
(
  1,
  123,
  2
),
(
  2,
  1234567,
  3
),
(
  2,
  12345678,
  3
),
(
 3,
 1234,
 3
),
(
 4,
 12345,
 2
),
(
 5,
 123,
 3
),
(
 6,
 123456,
 3

),
(
7,
12345,
10
)

create table #PackageSum
(
  PackageId int,
  ItemsCount int
)

insert into #PackageSum(PackageId,ItemsCount)

select
    pki.PackageId,
    count(pki.PackageItemId) as Items
from
    #packageItems pki
inner join #packages pk
    on pki.PackageId = pk.PackageId
where
    pki.Qty > 0

group by
    pki.PackageId




select * from #Packages
select * from #PackageItems
select * from #PackageSum

declare @Today date = getdate();
declare @Yesterday date = dateadd(day,-1,@Today);
declare @2DaysOld  date = dateadd(day,-2,@Today);
declare @3daysOld  date = dateadd(day,-3,@Today);
declare @5daysOld  date = dateadd(day,-5,@Today);


--select @Now

select  
    pk.WHName,
    SUM(case when pk.ShipByDate = @Today then 1 else 0 end) as PackagesDueToday,
    SUM(case when pk.ShipByDate = @Today then pks.ItemsCount else 0 end) as PackageItemsDueToday,
    SUM(case when pk.ShipByDate >= @2DaysOld and pk.ShipByDate <= @Yesterday then 1 else 0 end) as Packages1To2DaysOld, 
    SUM(case when pk.ShipByDate >= @2DaysOld and pk.ShipByDate <= @Yesterday then   pks.ItemsCount else 0 end) as PackageItems1To2DaysOld,
    SUM(case when pk.ShipByDate >= @3daysOld and pk.ShipByDate <= @2DaysOld   then 1 else 0 end) as Packages2To3DaysOld,    
    SUM(case when pk.ShipByDate >= @3daysOld and pk.ShipByDate <= @2DaysOld   then pks.ItemsCount else 0 end) as PackageItems2To3DaysOld,
    SUM(case when pk.ShipByDate <= @5daysOld then 1 else 0 end) as Packages5DaysAndOLder,
    SUM(case when pk.ShipByDate <= @5daysOld then pks.ItemsCount  else 0 end) as PackageItems5DaysAndOLder
from
    #packages pk
inner join #PackageSum pks
    on pk.PackageId = pks.PackageId
group by
pk.WHName



drop table  #packages
drop table #PackageItems

drop table #PackageSum

0 个答案:

没有答案