5、子查询
来源:互联网 发布:淘宝网法院司法拍卖 编辑:程序博客网 时间:2024/06/05 08:36
1、求一个聚合的SQL该怎么写
有这样的数据
员工 开始日期 终了日期
A 2013-05-01 00:00:00.000 2013-06-08 00:00:00.000
A 2013-09-02 00:00:00.000 2013-12-31 00:00:00.000
A 2013-09-05 00:00:00.000 NULL
求员工A的所能表示的最大范围的日期,取日期的并集,最后结果应该如下:
员工 开始日期 终了日期
A 2013-05-01 00:00:00.000 2013-06-08 00:00:00.000
A 2013-09-02 00:00:00.000 NULL
解法:
;with t(员工 , 开始日期,终了日期)as(select 'A', '2013-05-01 00:00:00.000','2013-06-08 00:00:00.000'union all select 'A','2013-09-02 00:00:00.000','2013-12-31 00:00:00.000'union all select 'A','2013-09-05 00:00:00.000',NULLunion all select 'A','2013-09-15 00:00:00.000','2013-11-08 00:00:00.000'),ttas(select 员工,开始日期,终了日期, (select MIN(t2.开始日期) from t t2 where t1.员工 = t2.员工 and t1.开始日期 between t2.开始日期 and isnull(t2.终了日期,'3000-01-01') ) as min_开始日期, (select max(isnull(t2.终了日期,'3000-01-01')) from t t2 where t1.员工 = t2.员工 and t1.开始日期 between t2.开始日期 and isnull(t2.终了日期,'3000-01-01') ) as max_终了日期 from t t1)select 员工,min_开始日期 as 开始日期, nullif(max(max_终了日期),'3000-01-01') as 终了日期from ttgroup by 员工,min_开始日期/*员工 开始日期 终了日期A 2013-05-01 00:00:00.0002013-06-08 00:00:00.000A 2013-09-02 00:00:00.000NULL*/
2、sql中怎么判断某个字段的值是否连续?
比如:A表的字段AID的值为:1、2、4、5、7、8、10
怎么用sql查询出2、5、8的结果呢?
要查的结果就是查询这组数据从哪里开始不连续的。
解法:
create table A(AID int) insert into A(AID) select 1 union all select 2 union all select 4 union all select 5 union all select 7 union all select 8 union all select 10select aidfrom(select a.aid, (select min(aid) from a aa where aa.aid > a.aid) min_aidfrom A )awhere aid +1 < min_aid/*aid258*/
3、有个表有入库时间,有出库时间,想得到该样式号每月的销售和历史库存
库结构大概是这样:
货号 样式号 入库时间 出库时间
a001 10 2011-1-10
b002 10 2011-1-10 2011-2-1
c003 10 2012-1-15 2012-2-2
d004 10 2013-2-3 2013-1-5
e005 10 2013-2-3
f006 15 2011-2-15 2011-3-16
g007 15 2011-2-16 2012-3-16
h009 15 2013-1-10
m012 18 2011-1-4
c009 18 2011-4-5 2012-5-6
f008 18 2012-2-19 2013-1-1
e008 18 2013-1-5 2013-2-6
想得到的结果:
样式号 日期 销售件数 库存件数
10 2011-2 1 1
10 2012-2 1 1
10 2013-1 1 2
15 2011-3 1 0
15 2012-3 1 1
18 2012-5 1 1
18 2013-1 1 1
18 2013-2 1 1
if object_id('tb') is not null drop table tbgo create table tb ([货号] varchar(20),[样式号] int,[入库时间] datetime,[出库时间] datetime) insert into tbSELECT 'a001',10,'2011-01-10',null UNION ALLSELECT 'b002',10,'2011-01-10','2011-02-01' UNION ALLSELECT 'c003',10,'2012-01-15','2012-02-02' UNION ALLSELECT 'd004',10,'2013-01-03','2013-01-05' UNION ALLSELECT 'e005',10,'2013-01-03',null UNION ALLSELECT 'f006',15,'2011-02-15','2011-03-16' UNION ALLSELECT 'g007',15,'2011-02-16','2012-03-16' UNION ALLSELECT 'h009',15,'2013-01-10',null UNION ALLSELECT 'm012',18,'2011-01-04',null UNION ALLSELECT 'c009',18,'2011-04-05','2012-05-06' UNION ALLSELECT 'f008',18,'2012-02-19','2013-01-01' UNION ALLSELECT 'e008',18,'2013-01-05','2013-02-06'go;with tas(select *, row_number() over(partition by 样式号 order by 入库时间,出库时间) as rownumfrom tb),ttas(select *, case when 出库时间 is null then (select top 1 出库时间 from t t2 where t1.样式号 = t2.样式号 and t1.rownum > t2.rownum order by t2.rownum desc) else 出库时间 end as prior_row, case when 出库时间 is null then (select top 1 出库时间 from t t2 where t1.样式号 = t2.样式号 and t1.rownum < t2.rownum order by t2.rownum ) else 出库时间 end as next_row from t t1),tttas(select 样式号, convert(varchar(7),isnull(next_row,prior_row),120) as 日期, count(出库时间) 销售件数, count(入库时间) 库存件数, count(入库时间) - count(出库时间) 剩余库存 --row_number() over(partition by 样式号 --order by convert(varchar(7),isnull(next_row,prior_row),120)) as rownumfrom ttgroup by 样式号, convert(varchar(7),isnull(next_row,prior_row),120))select t1.样式号,t1.日期, isnull(t1.销售件数,0) as 销售件数 , isnull(t1.库存件数,0) + isnull((select sum(库存件数)-sum(销售件数) as 剩余库存 from ttt t2 where t2.样式号 = t1.样式号 and t2.日期 < t1.日期 ),0) - isnull(t1.销售件数,0) as 库存件数 from ttt t1order by t1.样式号 /*样式号日期 销售件数库存件数10 2011-021 110 2012-021 110 2013-011 215 2011-031 015 2012-031 118 2012-051 118 2013-011 118 2013-021 1*/
- 5、子查询
- 子查询
- 子查询
- 子查询
- 子查询
- 子查询
- 子查询
- 子查询
- 子查询
- 子查询
- 子查询
- 子查询
- 子查询
- 子查询
- 子查询
- 子查询
- 子查询
- 子查询
- 运行android虚拟机报错:Failed to allocate memory: 1455
- 成长是一种经历
- [Oracle] 位图索引
- 为什么一直封我号
- I/O复用模型之select
- 5、子查询
- Android 在一个程序中启动另一个程序
- 学会知而不言,因为言多必失;学会自我解脱,因为这样才能自我超越;学会一个人静静思考,因为这样才能让自己更清醒、明白;学会用心看世界,因为这样才会看清人的本来面目;学会放下,因为只有放下了才能重新开始;
- C++中换行符
- Android工具:Android开发实用小工具
- Java调用SQL Server的存储过程详解
- 虚拟机里的Ubuntu卸载Unity安装Xfce
- What's Hadoop
- Quartz的cron表达式-cron复杂任务触发器定制任务调度