统计数据方面的Transact-SQL查询语句
来源:互联网 发布:ubuntu 安装pip出错 编辑:程序博客网 时间:2024/05/01 21:48
4 如何删除表中的重复数据,用Transact-SQL写出代码。
select distinct * into #Tmp from t2
drop table t2
select * into t2 from #Tmp
drop table #Tmp
6 人员情况表(employee)t3中字段包括,员工号(ID),姓名(name),年龄(age),文化程度(wh):包括四种情况(本科以上,大专,高中,初中以下),现在我要根据年龄字段查询统计出:表中文化程度为本科以上,大专,高中,初中以下,各有多少人,占总人数多少。结果如下:
学历 年龄 人数 百分比
本科以上 20 34 14.45
大专 20 33 13.69
高中 20 33 13.57
初中以下 20 100 40.95
本科以上 21 50 20.11
。。。。。。
Transact-SQL查询语句如何写?
---方法一:一句SQL即可
----生成的%为整数;
----前两个count(*)是group by分组后,各组的记录数;而第三个count(*)为表t3在group by前表中所有的记录数。
select edu,age,count(*) as '人数',count(*)*100/(select count(*) from t3) as '%' from t3
group by edu,age
order by age
---方法二:写一个存储过程
create proc see
as
declare @ren int
select @ren=count(*) from t3
--select edu,age,count(*) as '人数',cast(count(*) as float(2)) *100/cast(@ren as float(2)) as '%' ----注:生成的%小数位数不确定.
select edu,age,count(*) as '人数',convert(decimal(5,2),(cast(count(*) as float(2)) *100/cast(@ren as float(2))),0) as '%' ----生成的%小数位为两位.
from t3
group by edu,age
order by age
exec see
7表一(t1)
商品名称t1id 商品总量t1num
A 100
B 120
A 10
B 20
表二(t2)
商品名称t2id 出库数量t2num
A 10
A 20
B 10
B 20
B 30
用一条Transact-SQL语句算出商品A,B目前还剩多少?
---方法一:一句SQL即可
select t1id as 产品ID,(t1num-t2num) as 库存量
from (select t1id,sum(t1num) as t1num from t1 group by t1id) t1,
(select t2id,sum(t2num) as t2num from t2 group by t2id) t2
where t1.t1id=t2.t2id
---方法二:写一个存储过程
create proc seekucun
as
select t1id,sum(t1num) as t1num
into #t1
from t1
group by t1id
select t2id,sum(t2num) as t2num
into #t2
from t2
group by t2id
select t1id as 产品ID,(t1num-t2num) as 库存量
from #t1 inner join #t2
on #t1.t1id=#t2.t2id
order by (t1num-t2num) desc
exec seekucun
- 统计数据方面的Transact-SQL查询语句
- 简单的Transact-SQL查询
- 【SQL】 Transact -SQL 语句
- 存储过程:Transact-SQL语句的集合
- Transact-SQL语句总汇
- Transact-SQL语句总汇
- Transact SQL语句功能
- Transact-SQL语句总汇
- Transact-SQL语句总汇
- 查找不重复的统计数据的sql语句的写法
- 查找不重复的统计数据的sql语句的写法
- SQL Server2008查询优化器对统计数据的应用
- 教程:编写 Transact-SQL 语句
- Transact-SQL语句遍历结果集的三种方法
- Transact-SQL语句遍历结果集的三种方法
- 第七章 Transact-SQL 查询
- 关于SQL语句方面的问题
- 编写的Transact-SQL
- 愚人节介绍
- asp.net控件开发基础(18) --------让DadaSource接受过多的数据源
- J2EEWriter
- 建立一个脚本 A Good Shell Tutorial
- XNA perfect in WinForms
- 统计数据方面的Transact-SQL查询语句
- ASP 五大高效提速技巧
- X11,xorg.conf配置详解
- 精心收集的近百个ASP编程网址
- 关于X窗口的设置/etc/X11/xorg.cof 详解
- 在vs2003运用S60_2.1_chinese部署
- X配置文件(xorg.conf)分析
- eclipse 安装python 插件 pydev
- asp创建对象及中文显示解决技巧