日期:2014-05-18  浏览次数:20553 次

求一个复杂的SQL语句?
已知表1     paycode           name
                    1                       现金
                    2                       刷卡
                    3                       支票
     
      表2       paycode           total                   date
                      1                       30.00             2007-4-23
                      2                       40.00             2007-4-24
                      1                       30.00             2007-4-24
                      3                       20.00             2007-4-25
                      2                       50.00             2007-4-25
                      3                       10.00             2007-4-25

如果要查某一天或某段时间每种支付方式的收入,SQL语句应该怎么写,才能得到以下的结果(其中支付方式是从表一里取出的,不是固定的).先谢谢各位了
                  日期                           现金         刷卡       支票       合计
              2007-4-23                   30.00       0.00         0.00       30.00
              2007-4-24                   30.00       40.00       0.00       70.00
              2007-4-25                   0.00         50.00       30.00     80.00
              合计:                           60.00       90.00       30.00     180.00


------解决方案--------------------
--静态
select case grouping(date) when 1 then '合计: ' else convert(varchar(10),date,120) end 日期,
sum(case paycode when 1 then total else 0 end) 现金,
sum(case paycode when 2 then total else 0 end) 刷卡,
sum(case paycode when 3 th