求sql语句,请各位大侠帮助、指教!!!!!

jbj 2007-05-28 09:11:33
求sql语句
请各位大侠指教!!!

有以下表结构(表名为 t)

bh(编号),type(操作类型)、xh(操作序号),bz(操作步骤) sj(操作时间) zt(操作状态)
10 ,公文起草, 0, 起草, ,2005-01-02, 开始
10, ,公文起草, 1, 编辑, 2005-01-02, 编辑
10, ,公文起草, 3, 修改, 2005-01-02, 修改
10, ,公文起草, 4, 完成, 2005-01-03, 完成

10, ,公文流转, 0, 发起, 2005-01-04, 开始
10, ,公文流转, 1, 发送, 2005-01-04, 发送
10, ,公文流转, 2, 接收, 2005-01-05, 接收
10, ,公文流转, 3, 完成, 2005-01-05, 完成

10, ,公文审核, 0, 审核, 2005-01-06, 开始
10, ,公文审核, 1, 会签, 2005-01-06, 进行中
10, ,公文审核, 2, 修改, 2005-01-07, 完成


11 ,公文起草, 0, 起草, ,2005-01-02, 开始
11, ,公文起草, 1, 编辑, 2005-01-02, 编辑
11, ,公文起草, 3, 修改, 2005-01-02, 修改
11, ,公文起草, 4, 完成, 2005-01-03, 完成

11, ,公文流转, 0, 发起, 2005-01-04, 开始
11, ,公文流转, 1, 发送, 2005-01-04, 发送
11, ,公文流转, 2, 接收, 2005-01-05, 接收
11, ,公文流转, 3, 完成, 2005-01-06, 完成

11, ,公文审核, 0, 审核, 2005-01-07, 开始
11, ,公文审核, 1, 会签, 2005-01-08, 进行中
11, ,公文审核, 2, 修改, null, 进行中


要得到以下结果 :

bh(编号),type(操作类型) 起止时间 zt(操作状态)

10 ,公文起草, ,2005-01-02 2005-01-03, 完成
10, ,公文流转, ,2005-01-04 2005-01-05 完成
10, ,公文审核, ,2005-01-06 2005-01-07 完成

11 ,公文起草, 2005-01-02 2005-01-03 ,完成
11 ,公文起草, 2005-01-04 2005-01-06 ,完成
11 ,公文起草, 2005-01-07 ,未完成


判断起止时间 不同编号经、不同type的起止时间以xh(操作序号)为依据,xh最小的sj表示开始时间,xh最大的sj表示结束时间,
不同编号经、不同type 的操作状态 以xh最大的状态表示状态(未完成的表示'进行中') 其起止时间表示为 开开始时间--

请问用sql如何实现

在这 我先谢谢各位大侠!!!
...全文
184 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jbj 2007-05-29
  • 打赏
  • 举报
回复
非常感谢 bill024(咖啡熊)大哥,问题已解决,现结帖。请接分!!!
bill024 2007-05-28
  • 打赏
  • 举报
回复
改下:
select m.bh,m.type,起止时间=n.sj+' '+m.sj,m.zt from
(
select bh,type,sj=case when convert(char(10),sj,120) is null then '' else convert(char(10),sj,120) end,
zt=case when zt='进行中' then '未完成' else zt end,bz from t a where not exists
(
select sj from t where bh=a.bh and type=a.type and xh>a.xh
)
)m inner join
(
select bh,type,sj=case when convert(char(10),sj,120) is null then '' else convert(char(10),sj,120) end,
zt=case when zt='进行中' then '未完成' else zt end,bz from t a where not exists
(
select sj from t where bh=a.bh and type=a.type and xh<a.xh
)
)n on m.bh=n.bh and m.type=n.type
bill024 2007-05-28
  • 打赏
  • 举报
回复
create table t(bh int,type varchar(20),xh int,bz varchar(20),sj datetime,zt varchar(10))
insert t select 10,'公文起草',0,'起草','2005-01-02','开始'
union all select 10,'公文起草',1,'编辑','2005-01-02','编辑'
union all select 10,'公文起草',2,'修改','2005-01-02','修改'
union all select 10,'公文起草',3,'完成','2005-01-03','完成'

union all select 10,'公文流转',0,'发起','2005-01-04','开始'
union all select 10,'公文流转',1,'发送','2005-01-04','发送'
union all select 10,'公文流转',2,'接收','2005-01-05','接收'
union all select 10,'公文流转',3,'完成','2005-01-05','完成'

union all select 10,'公文审核',0,'审核','2005-01-06','开始'
union all select 10,'公文审核',1,'会签','2005-01-06','进行中'
union all select 10,'公文审核',2,'修改','2005-01-07','完成'

union all select 11,'公文起草',0,'起草','2005-01-02','开始'
union all select 11,'公文起草',1,'编辑','2005-01-02','编辑'
union all select 11,'公文起草',2,'修改','2005-01-02','修改'
union all select 11,'公文起草',3,'完成','2005-01-03','完成'

union all select 11,'公文流转',0,'发起','2005-01-04','开始'
union all select 11,'公文流转',1,'发送','2005-01-04','发送'
union all select 11,'公文流转',2,'接收','2005-01-05','接收'
union all select 11,'公文流转',3,'完成','2005-01-06','完成'

union all select 11,'公文审核',0,'审核','2005-01-07','开始'
union all select 11,'公文审核',1,'会签','2005-01-08','进行中'
union all select 11,'公文审核',2,'修改',null,'进行中'
go

select m.bh,m.type,起止时间=n.sj+' '+m.sj,m.zt from
(
select bh,type,sj=case when convert(char(10),sj,120) is null then '' else convert(char(10),sj,120) end,
zt=case when convert(char(10),sj,120) is null then '未完成' else zt end,bz from t a where not exists
(
select sj from t where bh=a.bh and type=a.type and xh>a.xh
)
)m inner join
(
select bh,type,sj=case when convert(char(10),sj,120) is null then '' else convert(char(10),sj,120) end,
zt=case when convert(char(10),sj,120) is null then '未完成' else zt end,bz from t a where not exists
(
select sj from t where bh=a.bh and type=a.type and xh<a.xh
)
)n on m.bh=n.bh and m.type=n.type

drop table t

/*
bh type 起止时间 zt
----------- -------------------- --------------------- ----------
10 公文起草 2005-01-02 2005-01-03 完成
10 公文流转 2005-01-04 2005-01-05 完成
10 公文审核 2005-01-06 2005-01-07 完成
11 公文起草 2005-01-02 2005-01-03 完成
11 公文流转 2005-01-04 2005-01-06 完成
11 公文审核 2005-01-07 未完成

(所影响的行数为 6 行)
*/
jbj 2007-05-28
  • 打赏
  • 举报
回复
加分了,请各位帮助

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧