我有一张表(t_saleloginfo),有字段salenum(售出总数量),salemoney(总金额),Isgroup(是否团体【0:个人,1:团体】),Paytype(支付方式【0:现金,1:支票】),OfficeID(售票点编号)。我的问题是,我现在想要分别查出{票类型是个人,支付方式为现金}的各个售票点的总数量和总金额,{售票类型为团体,支付方式为个人}的各个售票点的总数量和总金额,{票类型是个人,支付方式为支票}的各个售票点的总数量和总金额,{票类型是团体,支付方式为支票}的各个售票点的总数量和总金额,然后把所查询出来的数据显示到一张表里。表结构如下OfficeID(售票点),XGSaleNum(现金个人总数),XGSaleMoney(现金个人总金额)..........共九个字段的值。
请各位大侠积极发言。在线等待........

解决方案 »

  1.   


    select
    OfficeID,
    sum(case when Isgroup=0 and Paytype=0 then salenum else 0 end) XGSaleNum,
    sum(case when Isgroup=0 and Paytype=0 then salemoney else 0 end) XGSaleMoney,
    sum(case when Isgroup=1 and Paytype=0 then salenum else 0 end) 现金团体总数,
    sum(case when Isgroup=1 and Paytype=0 then salemoney else 0 end) 现金团体金额,
    sum(case when Isgroup=0 and Paytype=1 then salenum else 0 end) 支票个人总数,
    sum(case when Isgroup=0 and Paytype=1 then salemoney else 0 end) 支票个人总金额,
    sum(case when Isgroup=1 and Paytype=1 then salenum else 0 end) 支票团体总数,
    sum(case when Isgroup=1 and Paytype=1 then salemoney else 0 end) 支票团体金额
    from t_saleloginfo
    group by OfficeID