SQL:给查询添加一个合计行
2009-11-22 21:24
357 查看
.数据表t_test
id 销售人员id 商品id 数量
id emp_id product_id qty
1 01 001 200
2 01 002 300
2 01 002 400
3 02 001 400
4 02 002 500
Create table #t_test(
id int not null,
emp_id int not null,
product_id int not null,
qty int not null
)
insert into #t_test values(1,01,001,200)
insert into #t_test values(2,01,002,300)
insert into #t_test values(3,01,002,400)
insert into #t_test values(4,02,001,400)
insert into #t_test values(5,02,002,500)
select *
from #t_test
2.需要得到的结果
需要得到类似下面的结果
--------------------------------------
emp_id qty
01 900
02 900
合计 1800
--------------------------------------
大家看到了,这里加上了一个合计列
参考sql语句如下
-- for MS SQL Server 2005
select isnull(CONVERT(varchar(20), emp_id),'Total') as 'emp_id'
,sum(qty) as 'qty_Total'
from #t_test
group by emp_id
with rollup
查询的结果如下所示
emp_id qty_Total
1 900
2 900
Total 1800
3.负责一点,统计每个销售人员以及商品的数量
--------------------------------------
emp_id product_id qty
01 001 200
01 001 700
01 小计 900
02 001 400
02 002 500
02 小计 900
合计 1800
--------------------------------------
由于要统计合计以及小计,不能简单的用nvl来产生"合计"了,要用grouping函数,来判断者某行是否有rollup产生的合计行,
select
case when grouping(emp_id)=1 and grouping(product_id)=1 then '合计' else emp_id end emp_id,
case when grouping(emp_id)=0 and grouping(product_id)=1 then '小计' else procudt_id end product_id,
sum(qty) qty
from t_test
group by rollup(emp_id,product_id)
注意,grouping(emp_id)=1,说明是有rollup函数生成的行,0为数据库本身有的行
id 销售人员id 商品id 数量
id emp_id product_id qty
1 01 001 200
2 01 002 300
2 01 002 400
3 02 001 400
4 02 002 500
Create table #t_test(
id int not null,
emp_id int not null,
product_id int not null,
qty int not null
)
insert into #t_test values(1,01,001,200)
insert into #t_test values(2,01,002,300)
insert into #t_test values(3,01,002,400)
insert into #t_test values(4,02,001,400)
insert into #t_test values(5,02,002,500)
select *
from #t_test
2.需要得到的结果
需要得到类似下面的结果
--------------------------------------
emp_id qty
01 900
02 900
合计 1800
--------------------------------------
大家看到了,这里加上了一个合计列
参考sql语句如下
-- for MS SQL Server 2005
select isnull(CONVERT(varchar(20), emp_id),'Total') as 'emp_id'
,sum(qty) as 'qty_Total'
from #t_test
group by emp_id
with rollup
查询的结果如下所示
emp_id qty_Total
1 900
2 900
Total 1800
3.负责一点,统计每个销售人员以及商品的数量
--------------------------------------
emp_id product_id qty
01 001 200
01 001 700
01 小计 900
02 001 400
02 002 500
02 小计 900
合计 1800
--------------------------------------
由于要统计合计以及小计,不能简单的用nvl来产生"合计"了,要用grouping函数,来判断者某行是否有rollup产生的合计行,
select
case when grouping(emp_id)=1 and grouping(product_id)=1 then '合计' else emp_id end emp_id,
case when grouping(emp_id)=0 and grouping(product_id)=1 then '小计' else procudt_id end product_id,
sum(qty) qty
from t_test
group by rollup(emp_id,product_id)
注意,grouping(emp_id)=1,说明是有rollup函数生成的行,0为数据库本身有的行
相关文章推荐
- SQL:给查询添加一个合计行
- 如何把个表的合计字段 合并SQL查询到一个结果集中
- sql的select查询添加一个虚拟字段进去
- 实际——如何给SQL查询添加合计行(sqlserver2008。但是db2的case when 语句end后不能添加其他字段)
- SQL 给一个数据查询结果添加一列从0到n的序号
- Struts2+Ibatis+Spring.30(完整例子,含3.0事务配置,OSCache缓存配置,JreeChart配置,log4j日志输出Sql,对一个表实现完整的查询,批量删除,添加,更新)
- 又一个通用分页存储过程,支持表别名,多表联合查询SQL语句
- Sql 查询一个表中符合条件的且某一项为最大的记录
- 当你添加了一个索引后,你的查询语句出错了...
- 我用vs2005写了一个webservice用来查询sql2005数据库中表值,返回一个dataset类型集合,测试时候显示的xml文档没有问题,但是当我添加web引用的时候却无法正常显示而且报错原因之一。
- 网站后台登录aspcms 提示错误号:-2147467259,错误描述:操作必须使用一个可更新的查询。sql=update AspCms_Content set TimeStatus=0 where TimeStatus=1 and Timeing <= 解决方法。
- flask_sqlalchemy和SQL遇到的一个查询
- 将根据字符查询转换后输出结果,规则为:’A’转换为’男’,’B’转换为’女’,其他字符转换为’未知’,请用一个SQL语句写出。
- sql关联查询一个例子
- 一个 提高SQL 查询的讨论帖
- SQL查询一个表中类别字段中Max()最大值对应的记录
- 在SQL Server中使用SQL语句查询一个存储过程被其它所有的存储过程引用的存储过程名
- 在查询结果中添加一列表示记录的行数的sql语句
- 又一个通用分页存储过程,支持表别名,多表联合查询SQL语句--转载
- 步步为营VS 2008 + .NET 3.5(10) - DLINQ(LINQ to SQL)之调用存储过程的添加、查询、更新和删除