原因是这样的,
表A结构('总店名','总店下的某分店名','该分店某次的销售数量','该分店某次的销售金额')
里面的数据象这样
'总店名1','总店1下的分店名1','分店销售数量1','分店销售金额1'
'总店名1','总店1下的分店名1','分店销售数量2','分店销售金额2'
'总店名1','总店1下的分店名1','分店销售数量3','分店销售金额3'
'总店名1','总店1下的分店名2','分店销售数量1','分店销售金额1'
'总店名1','总店1下的分店名2','分店销售数量2','分店销售金额2'

'总店名2','总店2下的分店名1','分店销售数量1','分店销售金额1'
'总店名2','总店2下的分店名1','分店销售数量2','分店销售金额2'

我现在想用sql查询,以达到这样的查询结果
('总店名','分店名','该分店的销售数量总和','该分店的销售金额总和','该分店所属总店的销售数量总和','该分店所属总店的销售金额总和')比如总店2总共销售了40件,总共销售金额200,总店2下的分店1总共销售了10件,总共销售金额20,那么我查询出来的一条记录就为
(总店2,总店2下分店1,10,20,40,200)请问这样的效果如何实现?请高手赐教

解决方案 »

  1.   


    这样??
    select 总店名,总店下的某分店名,该分店的销售金额总和=sum(该分店某次的销售数量),
    该分店的销售金额总和=sum(该分店某次的销售金额),
    该分店所属总店的销售数量总和=(select sum(该分店某次的销售数量) from 表名 where 总店名=a.总店名 group by 总店名),
    该分店所属总店的销售金额总和=(select sum(该分店某次的销售金额) from 表名 where 总店名=a.总店名 group by 总店名) 
    from 表名 a group by 总店名,总店下的某分店名
    晕死。这什么字段名啊。好长。
      

  2.   

    declare @t table(zd int,fd int,sl int,je int)
    insert @t select 1,1,1,1
    union  select 1,1,2,5
    union  select 1,2,2,5
    union  select 1,3,2,5
    union  select 2,1,3,3
    union  select 2,2,8,3
    union  select 3,1,4,4select zd,fd,sum(sl) sl,sum(je) je,0 zsl ,0 zje  into # from @t group by zd,fd
    update # set zsl = b.zsl,zje = b.zje
    from # a,(select zd,sum(sl) zsl,sum(je) zje from # group by zd) b
    where a.zd = b.zd
    select * from # order by zd,fd
    drop table #
    /*
    zd          fd          sl          je          zsl         zje         
    ----------- ----------- ----------- ----------- ----------- ----------- 
    1           1           3           6           7           16
    1           2           2           5           7           16
    1           3           2           5           7           16
    2           1           3           3           11          6
    2           2           8           3           11          6
    3           1           4           4           4           4(所影响的行数为 6 行)
    */
      

  3.   

    我写中文名是怕表达得不够清楚,看来适得其反了。。-_-|||
    多谢JiangHongTao 帮忙,您的语句我在研究中
      

  4.   

    这样也可以
    declare @t table(zd int,fd int,sl int,je int)
    insert @t select 1,1,1,1
    union  select 1,1,2,5
    union  select 1,2,2,5
    union  select 1,3,2,5
    union  select 2,1,3,3
    union  select 2,2,8,3
    union  select 3,1,4,4select a.zd,a.fd,sum(a.sl) sl,sum(a.je) je,b.zsl,b.zje 
      from @t a,(select zd,sum(sl) zsl,sum(je) zje from @t group by zd) b
      where a.zd = b.zd group by a.zd,a.fd,b.zsl,b.zje/*
    zd          fd          sl          je          zsl         zje         
    ----------- ----------- ----------- ----------- ----------- ----------- 
    1           1           3           6           7           16
    1           2           2           5           7           16
    1           3           2           5           7           16
    2           1           3           3           11          6
    2           2           8           3           11          6
    3           1           4           4           4           4(所影响的行数为 6 行)
    */
      

  5.   

    出于性能的考虑,对JiangHongTao 得代码做了简单修改:
    [SQL]
    declare @t table(zd int,fd int,sl int,je int)
    declare @ret table (zd int,fd int,sl int,je int ,zsl int ,zje int)insert @t select 1,1,1,1
    union  select 1,1,2,5
    union  select 1,2,2,5
    union  select 1,3,2,5
    union  select 2,1,3,3
    union  select 2,2,8,3
    union  select 3,1,4,4insert into @ret
    select zd,fd,sum(sl) sl,sum(je) je,0 zsl ,0 zje  from @t group by zd,fdupdate @ret set zsl = b.zsl,zje = b.zje
    from @ret a,(select zd,sum(sl) zsl,sum(je) zje from @ret group by zd) b
    where a.zd = b.zdselect * from @ret order by zd,fd
    [/SQL]
      

  6.   

    出于性能的考虑,对JiangHongTao   得代码做了简单修改: 
     
    declare   @t   table(zd   int,fd   int,sl   int,je   int) 
    declare   @ret   table   (zd   int,fd   int,sl   int,je   int   ,zsl   int   ,zje   int) insert   @t   select   1,1,1,1 
    union     select   1,1,2,5 
    union     select   1,2,2,5 
    union     select   1,3,2,5 
    union     select   2,1,3,3 
    union     select   2,2,8,3 
    union     select   3,1,4,4 insert   into   @ret 
    select   zd,fd,sum(sl)   sl,sum(je)   je,0   zsl   ,0   zje     from   @t   group   by   zd,fd update   @ret   set   zsl   =   b.zsl,zje   =   b.zje 
    from   @ret   a,(select   zd,sum(sl)   zsl,sum(je)   zje   from   @ret   group   by   zd)   b 
    where   a.zd   =   b.zd select   *   from   @ret   order   by   zd,fd 
      

  7.   

    蒋红涛是最近冒出的黑马,将来必定扬名SQL版块。