create table test
(
xm varchar(50), --项目名称
dr decimal(13,2),--当日收入
rq datetime,--日期
ssbm varchar(50)--机构名称
)
insert into test select '营业收入应收合计',10000,'2007-1-1','A营业厅'
insert into test select '实收营业款',8000,'2007-1-1','A营业厅'insert into test select '营业收入应收合计',20000,'2007-1-1','B营业厅'
insert into test select '实收营业款',10000,'2007-1-1','B营业厅'/*
要得到两种格式的报表(其中余额列=应收 - 实收)1. 对某一个营业厅进行统计         日期          摘要        应收        实收        余额
     2007-1-1      A营业厅     10000        8000       2000    2.对所有营业厅进行分组统计
   
     日期          摘要        应收        实收        余额
    2007-1-1      A营业厅     10000       8000        2000
    2007-1-1      B营业厅     20000       10000       10000
*/
drop table test

解决方案 »

  1.   


    create table test 

    xm varchar(50), --项目名称 
    dr decimal(13,2),--当日收入 
    rq datetime,--日期 
    ssbm varchar(50)--机构名称 

    insert into test select  '营业收入应收合计 ',10000, '2007-1-1 ', 'A营业厅 ' 
    insert into test select  '实收营业款 ',8000, '2007-1-1 ', 'A营业厅 ' insert into test select  '营业收入应收合计 ',20000, '2007-1-1 ', 'B营业厅 ' 
    insert into test select  '实收营业款 ',10000, '2007-1-1 ', 'B营业厅 ' 
    --1
    select 
    *,
    应收-实收 as '余额'
    from 
    (
    select 
    rq,ssbm,
    sum(case when xm='营业收入应收合计' then dr end)as '应收',
            sum(case when xm='实收营业款' then dr end)as '实收'
    from test
    where ssbm='A营业厅 '
    group by rq,ssbm
    )arq                                                     ssbm                                               应收                                       实收                                       余额                                       
    ------------------------------------------------------ -------------------------------------------------- ---------------------------------------- ---------------------------------------- ---------------------------------------- 
    2007-01-01 00:00:00.000                                A营业厅                                               10000.00                                 8000.00                                  2000.00(所影响的行数为 1 行)警告: 聚合或其它 SET 操作消除了空值。
    --2
    select 
    *,
    应收-实收 as '余额'
    from 
    (
    select 
    rq,ssbm,
    sum(case when xm='营业收入应收合计' then dr end)as '应收',
            sum(case when xm='实收营业款' then dr end)as '实收'
    from test
    group by rq,ssbm
    )a
    rq                                                     ssbm                                               应收                                       实收                                       余额                                       
    ------------------------------------------------------ -------------------------------------------------- ---------------------------------------- ---------------------------------------- ---------------------------------------- 
    2007-01-01 00:00:00.000                                A营业厅                                               10000.00                                 8000.00                                  2000.00
    2007-01-01 00:00:00.000                                B营业厅                                               20000.00                                 10000.00                                 10000.00(所影响的行数为 2 行)警告: 聚合或其它 SET 操作消除了空值。
      

  2.   

    create table test ( xm varchar(50), --项目名称 
    dr decimal(13,2),--当日收入 
    rq datetime,--日期 
    ssbm varchar(50))--机构名称 ) 
    insert into test select  '营业收入应收合计 ',10000, '2007-1-1 ', 'A营业厅 ' 
    insert into test select  '实收营业款 ',8000, '2007-1-1 ', 'A营业厅 ' 
    insert into test select  '营业收入应收合计 ',20000, '2007-1-1 ', 'B营业厅 ' 
    insert into test select  '实收营业款 ',10000, '2007-1-1 ', 'B营业厅 ' 
    goselect *,应收 - 实收 余额 from
    (
      select convert(varchar(10),rq,120) 日期 , ssbm 摘要, 
        sum(case xm when '营业收入应收合计' then dr else 0 end) '应收',
        sum(case xm when '实收营业款' then dr else 0 end) '实收'
      from test
      group by convert(varchar(10),rq,120) , ssbm 
    ) tdrop table test/*
    日期       摘要     应收      实收      余额   
    ---------- ------- --------- --------  --------
    2007-01-01 A营业厅  10000.00  8000.00  2000.00
    2007-01-01 B营业厅  20000.00  10000.00 10000.00(所影响的行数为 2 行)
    */
      

  3.   

    对第一个问题只需要加上个where条即可.create table test ( xm varchar(50), --项目名称 
    dr decimal(13,2),--当日收入 
    rq datetime,--日期 
    ssbm varchar(50))--机构名称 ) 
    insert into test select  '营业收入应收合计 ',10000, '2007-1-1 ', 'A营业厅 ' 
    insert into test select  '实收营业款 ',8000, '2007-1-1 ', 'A营业厅 ' 
    insert into test select  '营业收入应收合计 ',20000, '2007-1-1 ', 'B营业厅 ' 
    insert into test select  '实收营业款 ',10000, '2007-1-1 ', 'B营业厅 ' 
    goselect *,应收 - 实收 余额 from
    (
      select convert(varchar(10),rq,120) 日期 , ssbm 摘要, 
        sum(case xm when '营业收入应收合计' then dr else 0 end) '应收',
        sum(case xm when '实收营业款' then dr else 0 end) '实收'
      from test
      group by convert(varchar(10),rq,120) , ssbm 
    ) t
    where 摘要 = 'A营业厅'drop table test/*
    日期       摘要     应收      实收      余额   
    ---------- ------- --------- --------  --------
    2007-01-01 A营业厅  10000.00  8000.00  2000.00(所影响的行数为 1 行)
    */
      

  4.   

    create table test  
    (  
    xm varchar(50), --项目名称  
    dr decimal(13,2),--当日收入  
    rq datetime,--日期  
    ssbm varchar(50)--机构名称  
    )  
    insert into test select   '营业收入应收合计  ',10000,  '2007-1-1  ',  'A营业厅  '  
    insert into test select   '实收营业款  ',8000,  '2007-1-1  ',  'A营业厅  '  insert into test select   '营业收入应收合计  ',20000,  '2007-1-1  ',  'B营业厅  '  
    insert into test select   '实收营业款  ',10000,  '2007-1-1  ',  'B营业厅  '  
    --1 
    select  
    *, 
    应收-实收 as  '余额 ' 
    from  

    select  
    convert(varchar(10),rq,120)as rq,ssbm, 
    sum(case when xm= '营业收入应收合计 ' then dr end)as  '应收 ', 
            sum(case when xm= '实收营业款 ' then dr end)as  '实收 ' 
    from test 
    where ssbm= 'A营业厅' 
    group by rq,ssbm 
    )a 
    rq         ssbm                                               应收                                       实收                                       余额                                       
    ---------- -------------------------------------------------- ---------------------------------------- ---------------------------------------- ---------------------------------------- 
    2007-01-01 A营业厅                                               10000.00                                 8000.00                                  2000.00(所影响的行数为 1 行)警告: 聚合或其它 SET 操作消除了空值。
    select  
    *, 
    应收-实收 as  '余额 ' 
    from  

    select  
    convert(varchar(10),rq,120)as rq,ssbm, 
    sum(case when xm= '营业收入应收合计 ' then dr end)as  '应收 ', 
            sum(case when xm= '实收营业款 ' then dr end)as  '实收 ' 
    from test 
    group by rq,ssbm 
    )a rq         ssbm                                               应收                                       实收                                       余额                                       
    ---------- -------------------------------------------------- ---------------------------------------- ---------------------------------------- ---------------------------------------- 
    2007-01-01 A营业厅                                               10000.00                                 8000.00                                  2000.00
    2007-01-01 B营业厅                                               20000.00                                 10000.00                                 10000.00(所影响的行数为 2 行)警告: 聚合或其它 SET 操作消除了空值。
      

  5.   

    可以把日期改为:日期=convert(varchar(10),rd,111)
      

  6.   

    --这个更简单的.
    create table test ( xm varchar(50), --项目名称 
    dr decimal(13,2),--当日收入 
    rq datetime,--日期 
    ssbm varchar(50))--机构名称 ) 
    insert into test select  '营业收入应收合计 ',10000, '2007-1-1 ', 'A营业厅 ' 
    insert into test select  '实收营业款 ',8000, '2007-1-1 ', 'A营业厅 ' 
    insert into test select  '营业收入应收合计 ',20000, '2007-1-1 ', 'B营业厅 ' 
    insert into test select  '实收营业款 ',10000, '2007-1-1 ', 'B营业厅 ' 
    go  select convert(varchar(10),rq,120) 日期 , ssbm 摘要, 
        sum(case xm when '营业收入应收合计' then dr else 0 end) '应收',
        sum(case xm when '实收营业款' then dr else 0 end) '实收',
        sum(case when xm = '营业收入应收合计' then dr when xm = '实收营业款' then -dr else 0 end) 余额 
      from test
      group by convert(varchar(10),rq,120) , ssbm drop table test/* 
    日期         摘要     应收       实收        余额    
    ---------- ------- --------- --------  -------- 
    2007-01-01 A营业厅  10000.00  8000.00   2000.00 
    2007-01-01 B营业厅  20000.00  10000.00  10000.00 (所影响的行数为 2 行) 
    */