主键,是主键的问题!
解决方案 »
- 筛选跌幅超过10%的记录
- 为什么自己写的排序算法没有数据库的SQL语句排序快速?
- 请问char varchar nchar 的区别?什么场合适合用什么?
- 急!!!在线等待,在SQL查询分析器中误改数据,如何回滚???
- 关于树的宽度优先遍历(给magic_jiang (magic) )
- 目前,考一个MCDBA共需多少钱(只含考试费)?
- sqlserver的几个问题,高手请手!
- 关于在视图上写INSTEAD OF触发器
- 您想成为数据处理和信息管理大师吗?
- sql server 如何复制多条记录,且更改其中字段的值
- 存储过程的基本使用
- 如何使用PIVOT ? 求一交叉表的SQL语句!!(SQL SERVER 2005)
manCd studyCd dateFlg insertFlg 不考虑 不考虑 不考虑 不考虑
303044 1101 20080101 1 NULL NULL NULL NULL
303044 1102 20080201 1 NULL NULL NULL NULL
303044 1102 20080218 1 NULL NULL NULL NULL
303044 1102 20080228 2 NULL NULL NULL NULL
303044 1103 20080301 2 NULL NULL NULL NULL
303044 1104 20080401 2 NULL NULL NULL NULL
303044 1105 20080501 2 NULL NULL NULL NULL
303044 9901 20080701 2 NULL NULL NULL NULL
..............还有很多其他manCd的数据,不考虑。
第一个字段是KEY
studyCd studyMei
1101 AA
1102 BB
1103 CC
1104 DD
1105 EE
9901 FF
希望得到:
studyCd studyCd date1 date2 date3 date4 date5 state
1101 AA 20080101 完成(就这,总是 未完成)
1102 BB 20080201 20080218 20080228 未完成
1103 CC 20080301 未完成
1104 DD 20080401 未完成
1105 EE 20080501 未完成
9901 FF 20080701 未完成
create table 基本表(manCd varchar(3),studyCd int,dateFlg datetime,insertFlg int)
insert into 基本表
select '001',1001,'2007/6/24',1 union all
select '001',1001,'2007/6/25',1 union all
select '001',1002,'2007/7/24',1 union all
select '001',1002,'2007/7/25',1 union all
select '001',1002,'2007/7/26',2 union all
select '001',1005,'2007/7/25',2 union all
select '001',1005,'2007/7/28',2 union all
select '001',1006,'2007/6/24',2 union all
select '001',1007,'2007/7/25',1 union all
select '001',1007,'2007/7/26',2 union all
select '010',1008,'2007/7/28',2
--> 测试数据: 内容表
create table 内容表 (studyCd int,studyName Nvarchar(6))
insert into 内容表
select 1001,N'木工' union all
select 1002,N'瓦匠' union all
select 1005,N'水工' union all
select 1006,N'水工工' union all
select 1007,N'瓦匠匠' union all
select 1008,N'木工工'
select studyCd,studyName,[date1],[date2],[date3],[date4],[date5],insertFlgName
from
(
select B.studyCd,B.studyName,A.dateFlg,A.insertFlgName
,'date'+cast(ROW_NUMBER() OVER( PARTITION BY B.studyCd ORDER BY A.dateFlg) as varchar(10)) AS datetype
from ( select *
, insertFlgName=(select case when max(insertFlg)=1 then N'完成' else N'未完成' end
from 基本表 where studyCd=Base.studyCd)
from 基本表 Base) A
,内容表 B
where A.studyCd=B.studyCd
group by B.studyCd,B.studyName,A.dateFlg,A.insertFlgName
) m
pivot (max(dateFlg) for datetype in ([date1],[date2],[date3],[date4],[date5])) as pivdrop table 基本表,内容表
http://topic.csdn.net/u/20080624/18/0ca5f9a3-3dd7-4b5e-9191-1ed7fccca8f9.html?958144869
create table 基本表(manCd varchar(10),studyCd int,dateFlg VARCHAR(8),insertFlg int)
insert into 基本表
select '303044',1101,'20080101',1 union all
select '303044',1102,'20080201',1 union all
select '303044',1102,'20080218',1 union all
select '303044',1102,'20080228',2 union all
select '303044',1103,'20080301',2 union all
select '303044',1104,'20080401',2 union all
select '303044',1105,'20080501',2 union all
select '303044',9901,'20080701',2--> 测试数据: 内容表
create table 内容表 (studyCd int,studyName Nvarchar(6))
insert into 内容表
select 1101,N'AA' union all
select 1102,N'BB' union all
select 1103,N'CC' union all
select 1104,N'DD' union all
select 1105,N'EE' union all
select 9901,N'FF'select studyCd,studyName,isnull([date1],'') date1
,isnull([date2],'') date2
,isnull([date3],'') date3
,isnull([date4],'') date4
,isnull([date5],'') date5
,insertFlgName
from
(
select B.studyCd,B.studyName,A.dateFlg,A.insertFlgName
,'date'+cast(ROW_NUMBER() OVER( PARTITION BY B.studyCd ORDER BY A.dateFlg) as varchar(10)) AS datetype
from ( select *
, insertFlgName=(select case when max(insertFlg)=1 then N'完成' else N'未完成' end
from 基本表 where studyCd=Base.studyCd)
from 基本表 Base) A
,内容表 B
where A.studyCd=B.studyCd
group by B.studyCd,B.studyName,A.dateFlg,A.insertFlgName
) m
pivot (max(dateFlg) for datetype in ([date1],[date2],[date3],[date4],[date5])) as pivdrop table 基本表,内容表/*
1101 AA 20080101 完成
1102 BB 20080201 20080218 20080228 未完成
1103 CC 20080301 未完成
1104 DD 20080401 未完成
1105 EE 20080501 未完成
9901 FF 20080701 未完成
*/
那就加多个条件:create table 基本表(manCd varchar(10),studyCd int,dateFlg VARCHAR(8),insertFlg int)
insert into 基本表
select '303044',1101,'20080101',1 union all
select '303044',1102,'20080201',1 union all
select '303044',1102,'20080218',1 union all
select '303044',1102,'20080228',2 union all
select '303044',1103,'20080301',2 union all
select '303044',1104,'20080401',2 union all
select '303044',1105,'20080501',2 union all
select '303044',9901,'20080701',2--> 测试数据: 内容表
create table 内容表 (studyCd int,studyName Nvarchar(6))
insert into 内容表
select 1101,N'AA' union all
select 1102,N'BB' union all
select 1103,N'CC' union all
select 1104,N'DD' union all
select 1105,N'EE' union all
select 9901,N'FF'declare @manCd varchar(50)
set @manCd='303044'
select studyCd,studyName,isnull([date1],'') date1
,isnull([date2],'') date2
,isnull([date3],'') date3
,isnull([date4],'') date4
,isnull([date5],'') date5
,insertFlgName
from
(
select B.studyCd,B.studyName,A.dateFlg,A.insertFlgName
,'date'+cast(ROW_NUMBER() OVER( PARTITION BY B.studyCd ORDER BY A.dateFlg) as varchar(10)) AS datetype
from ( select *
, insertFlgName=(select case when max(insertFlg)=1 then N'完成' else N'未完成' end
from 基本表 where studyCd=Base.studyCd)
from 基本表 Base) A
,内容表 B
where A.studyCd=B.studyCd
and A.manCd=@manCd
group by B.studyCd,B.studyName,A.dateFlg,A.insertFlgName
) m
pivot (max(dateFlg) for datetype in ([date1],[date2],[date3],[date4],[date5])) as pivdrop table 基本表,内容表/*
1101 AA 20080101 完成
1102 BB 20080201 20080218 20080228 未完成
1103 CC 20080301 未完成
1104 DD 20080401 未完成
1105 EE 20080501 未完成
9901 FF 20080701 未完成
*/
create table 基本表(manCd varchar(10),studyCd int,dateFlg VARCHAR(8),insertFlg int)
insert into 基本表
select '303044',1101,'20080101',1 union all
select '303044',1102,'20080201',1 union all
select '303044',1102,'20080218',1 union all
select '303044',1102,'20080228',2 union all
select '303044',1103,'20080301',2 union all
select '303044',1104,'20080401',2 union all
select '303044',1105,'20080501',2 union all
select '303044',9901,'20080701',2--> 测试数据: 内容表
create table 内容表 (studyCd int,studyName Nvarchar(6))
insert into 内容表
select 1101,N'AA' union all
select 1102,N'BB' union all
select 1103,N'CC' union all
select 1104,N'DD' union all
select 1105,N'EE' union all
select 9901,N'FF'gowith cte as
(
select *,px=row_number() over(partition by studyCd order by dateFlg)
from 基本表
)
select
a.studyCd,
b.studyName,
date1=max(case px when 1 then convert(varchar(10),dateFlg,111) else '' end),
date2=max(case px when 2 then convert(varchar(10),dateFlg,111) else '' end),
date3=max(case px when 3 then convert(varchar(10),dateFlg,111) else '' end),
date4=max(case px when 4 then convert(varchar(10),dateFlg,111) else '' end),
date5=max(case px when 5 then convert(varchar(10),dateFlg,111) else '' end),
state=case
when exists(select * from 基本表
where studyCd=a.studyCd and insertFlg=2)
then '未完成' else '完成' end
from
cte as a
left outer join 内容表 b
on a.studyCd=b.studyCd
where
a.manCd='303044'
group by
a.studyCd,b.studyName
order by
a.studyCddrop table 基本表,内容表/*
studyCd studyName date1 date2 date3 date4 date5 state
----------- --------- ---------- ---------- ---------- ---------- ---------- ------
1101 AA 20080101 完成
1102 BB 20080201 20080218 20080228 未完成
1103 CC 20080301 未完成
1104 DD 20080401 未完成
1105 EE 20080501 未完成
9901 FF 20080701 未完成(6 行受影响)
*/