下面一条语句能实现吗?谢谢storeid,plu,qty
店1,A001,10
店1,A002,20
店1,A003,30
店2,A001,11
店2,A002,21
店2,A003,31
店3,A001,3
店3,A002,4
店3,A003,5
.......要得到这样的数据storeid,plu,本店数量qty,其他店数量
店1,A001,10,(11+3=14)
店1,A002,20,(21+5=26)

解决方案 »

  1.   

    select storeid,plu,sum(qty) as 本店,其他店=(select sum(qty) from 表 where plu=t.plu and storeid<>t.storeid)
    from 表 t
    group by storeid,plu
      

  2.   

    DECLARE @t TABLE([storeid] NVARCHAR(2),[plu] NVARCHAR(4),[qty] INT)
    INSERT @t SELECT N'店1',N'A001',10
    UNION ALL SELECT N'店1',N'A002',20
    UNION ALL SELECT N'店1',N'A003',30
    UNION ALL SELECT N'店2',N'A001',11
    UNION ALL SELECT N'店2',N'A002',21
    UNION ALL SELECT N'店2',N'A003',31
    UNION ALL SELECT N'店3',N'A001',3
    UNION ALL SELECT N'店3',N'A002',4
    UNION ALL SELECT N'店3',N'A003',5
    /************/
    /*Test  Data*/
    /***fcuandy**/
    /*2008-11-28*/
    /************/
     --sql2005
    SELECT * FROM @t a
    CROSS APPLY
    (
    SELECT sm = SUM(qty) FROM @t WHERE storeid!=a.storeid AND plu=a.plu
    ) b/*
    店1 A001 10 14
    店1 A002 20 25
    店1 A003 30 36
    店2 A001 11 13
    店2 A002 21 24
    店2 A003 31 35
    店3 A001 3 21
    店3 A002 4 41
    店3 A003 5 61
    */
    --sql2000
    SELECT *,sm=(SELECT SUM(qty) FROM @t WHERE storeid!=a.storeid AND plu=a.plu)FROM @t a/*
    店1 A001 10 14
    店1 A002 20 25
    店1 A003 30 36
    店2 A001 11 13
    店2 A002 21 24
    店2 A003 31 35
    店3 A001 3 21
    店3 A002 4 41
    店3 A003 5 61
    */
      

  3.   

    --> --> (Andy)生成测试数据 2008-11-28
    Set Nocount On
    declare @1 table([storeid] nvarchar(2),[plu] nvarchar(4),[qty] int)
    Insert @1
    select N'店1',N'A001',10 union all
    select N'店1',N'A002',20 union all
    select N'店1',N'A003',30 union all
    select N'店2',N'A001',11 union all
    select N'店2',N'A002',21 union all
    select N'店2',N'A003',31 union all
    select N'店3',N'A001',3 union all
    select N'店3',N'A002',4 union all
    select N'店3',N'A003',5
     
    Select a.storeid,a.plu ,a.qty As [本店数量],b.qty-a.qty As [其他店数量 ]
    From @1 As a
     Inner  Join (Select plu,Sum(qty) As qty From @1 Group By plu) As b
    On a.plu=b.plu
    Order By a.storeid/*
    storeid plu  本店数量        其他店数量 
    ------- ---- ----------- -----------
    店1      A001 10          14
    店1      A002 20          25
    店1      A003 30          36
    店2      A003 31          35
    店2      A001 11          13
    店2      A002 21          24
    店3      A001 3           21
    店3      A002 4           41
    店3      A003 5           61
    */
      

  4.   


    use practice
    if Object_ID('dbo.product')is not null
    drop table product;
    go
    create table product (
    storeID char(3), 
    productID char(4),
    numbers int
    )
    go
    insert into product(storeID,productID,numbers)
     select '店1','A001',10 union all
     select '店1','A002',20 union all
     select '店1','A003',30 union all
     select '店2','A001',11 union all
     select '店2','A002',21 union all
     select '店2','A003',31 union all
     select '店3','A001',3  union all
     select '店3','A002',4  UNION ALL
     select '店3','A003',5  
    go
    select storeID, numbers as 本店数量,
    其他店数量=(select sum(numbers) from product tt where tt.productID=t.productID and 
     tt.storeID<>t.storeID
    group by productID)
    from product t
    order by storeID;
    go
     学习了一下