• 一则SQL问题


     f1    f2    f3
     ---------------------
     001    01    100
     002    01    200
     001    02    300
     002    02    400
     003    01    500
     004    02    600

    若想显示为以下格式,应如果写 SQL语句?

     f1    f2    f3
     ---------------------
     001    01    100 
     001    02    300 
     001    合计    400 
     002    01    200 
     002    02    400 
     002    合计    600 
     003    01    500 
     003    合计    500 
     004    02    600 
     004    合计    600 
    ===============================================================================
    Create table test
    (
    [ID] bigint Identity(1,1) primary key,
    f1 varchar(50),
    f2 varchar(50),
    f3 int
    )

    drop table test

    select * from test

    insert into test values('001','01',100)
    insert into test values('002','01',200)

    insert into test values('001','02',300)
    insert into test values('002','02',400)
    insert into test values('003','01',500)
    insert into test values('004','02',600)

    select f1,'合计' as f2, sum(f3) as f3 from test group by f1
    union
    select f1,f2,f3 from test

  • 相关阅读:
    Linux C/C++编程之(十四)文件操作相关函数
    javascript语法之循环语句
    javascript语法之流程控制语句
    javascript语法之字符串转换成数字
    javascript语法之声明变量
    认识javascript
    css之定位
    css之盒子模型案例
    常见Css样式
    Css详解之(伪类选择器)
  • 原文地址:https://www.cnblogs.com/RobotTech/p/661762.html
Copyright © 2020-2023  润新知