有一个table,字段如下:
kmh(科目号),jdbz(借贷标志),je(金额)
能否通过一条或几条SQL语句得到以下输出:
kmh,借方笔数,借方总金额,贷方笔数,贷方总金额

解决方案 »

  1.   

    /* 参考如下语句 */
    /* 适用于SQL Server */
    /* 以后问数据库问题的时,不要忘记告诉是什么类型 */
    select 科目号,
      sum(case 借贷标志 when '借方' then 1 else 0 end) as 借方笔数,
      sum(case 借贷标志 when '借方' then 金额 else 0 end) as 借方总金额,
      sum(case 借贷标志 when '贷方' then 1 else 0 end) as 贷方笔数,
      sum(case 借贷标志 when '贷方' then 金额 else 0 end) as 贷方总金额
    from 你的表名
    group by 科目号
      

  2.   

    select kmh,count(*),sum(je) from table where jdbz='借' group by kmhselect kmh,count(*),sum(je) from table where jdbz='贷' group by kmh
    下面自己来吧
      

  3.   


    select distinct kmh,
      (select count(*) from table where jdbz='借' and kmh=a.kmh) as 借方总数,
     (select sum(je) from tabletable where jdbz='借' and kmh=a.kmh) as 借方总金额,
      (select count(*) from table where jdbz='贷' and kmh=a.kmh) as 贷方总数,
     (select sum(je) from tabletable where jdbz='借' and kmh=a.kmh) as 贷方总金额 from table a
      

  4.   

    select kmh"科目号",jdbz"类别",count(*)"笔数",sum(je)"总金额"
      from table
      group by kmh,jdbz;