?? sql.txt
字號:
假設(shè)每人每月最多請4天假
明細(xì)表如下:
職工ID 請假日期 原因
1 2003.9.1 A
1 2003.9.5 B
1 2003.9.15 C
1 2003.9.18 D
2 2003.9.2 A
2 2003.9.5 A
.....
如何生成報(bào)表如下
ID 日期1 原因 日期2 原因 日期3 原因 日期4 原因
1 2003.9.1 A 2003.9.5 B 2003.9.15 C 2003.9.18 D
2 2003.9.2 A 2003.9.5 A
.........
select 職工ID,
min(case when flag=1 then 請假日期 end) 日期1,
min(case when flag=1 then 原因 end) 原因1,
min(case when flag=2 then 請假日期 end) 日期2,
min(case when flag=2 then 原因 end) 原因2,
min(case when flag=3 then 請假日期 end) 日期3,
min(case when flag=3 then 原因 end) 原因3,
min(case when flag=4 then 請假日期 end) 日期4,
min(case when flag=4 then 原因 end) 原因4
from (
select *,(select count(*) from 表 where 職工ID=tem.職工ID and 請假日期<=tem.請假日期) flag from 表 tem)
aa group by 職工ID
create table 表1(編號1 nvarchar(50), 名稱1 varchar(10))
insert into 表1 values('WWW' , 'A')
insert into 表1 values('EEE' , 'B')
create table 表2 (編號2 nvarchar(50), 名稱2 varchar(10),名稱3 varchar(10))
insert into 表2 values('RRR' , 'C','DDD')
insert into 表2 values('TTT' , 'D','334D')
insert into 表2 values('YYY' , 'E','D35GF')
go
select m.編號1 , m.名稱1 , n.編號2 , n.名稱2, n.名稱3 from
(select * , px = (select count(1) from 表1 where 編號1 < t.編號1) + '1' from 表1 t) m
full join
(select * , px = (select count(1) from 表2 where 編號2 < t.編號2) + '1' from 表2 t) n
on m.px = n.px
drop table 表1 , 表2
--測試數(shù)據(jù)
create table 表(coumn1 varchar(10),coumn2 varchar(10),coumn3 varchar(10),coumn4 varchar(10))
insert 表 select '藏','阿里地區(qū)','普蘭縣',50
union all select '藏','阿里地區(qū)','噶爾縣',50
union all select '藏','阿里地區(qū)','改則縣',50
union all select '藏','昌都地區(qū)','昌都縣',50
union all select '藏','昌都地區(qū)','類烏齊縣',50
union all select '藏','昌都地區(qū)','丁青縣',50
union all select '藏','昌都地區(qū)','八宿縣',50
union all select '藏','昌都地區(qū)','左貢縣',50
union all select '藏','昌都地區(qū)','洛隆縣',50
go
--處理
select id=identity(int,1,1),* into #t from 表
update #t set coumn4=''
from #t a left join(
select coumn1,coumn2
,id=min(id)+(max(id)-min(id)+1)/2
from #t
group by coumn1,coumn2
)b on a.id=b.id
where b.id is null
--顯示處理結(jié)果
select coumn1,coumn2,coumn3,coumn4 from #t
go
--刪除測試
drop table 表,#t
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -