递归查询的问题(不会,想不到)!

rustical 2005-02-16 03:17:18
1.登记表A
bookdate(char8) timeid(char2) id(char6) status(char3)
20050102 08 000001 001
20050102 08 000002 002
20050102 08 000003 001
20050102 08 000004 001
20050102 09 000001 002
20050102 09 000002 002
20050102 09 000003 003
20050102 09 000004 001
......
20050102 16 000001 001
20050102 16 000002 002
20050102 16 000003 001
20050102 16 000004 001

2.id 代码表
id idname
000001 'test1'
000002 'test2'
000003 'test3'
.....

3. timeid 代码表
timeid char(2) timename char(30)
08 'jjjjj'
09 'ttttt'
10 'mmmmm'
....
16 'lllll'

如何得到监控结果:
idname 08 09 10 ... 15 16
test1 001 001 001 002 001
test2 002 002 001 002 001
test3 001 003 001 002 001
....
test13 001 002 001 001 001
...全文
167 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
子陌红尘 2005-02-16
  • 打赏
  • 举报
回复
declare @s varchar(8000)
set @s = ''
select @s = @s + ',['+timeid+'] = max(
case when
A.timeid = '''+timeid+''' then A.status
end)'
from timeid order by timeid

set @s = ' select b.idname' + @s + ' from A ,ID b where A.id = b.id '

--增加"取某一天的日期"的条件限制
set @s = @s + ' and A.bookdate = ''20050102'''

--以 id 排序,不要用 idname 排序
set @s = @s + ' group by b.id ,b.idname order by b.id'

exec(@s)
rustical 2005-02-16
  • 打赏
  • 举报
回复
太谢谢了!
子陌红尘 2005-02-16
  • 打赏
  • 举报
回复
--执行查询:
declare @s varchar(8000)
set @s = ''
select @s = @s + ',['+timeid+'] = max(
case when
A.timeid = '''+timeid+''' then A.status
end)'
from timeid order by timeid

set @s = ' select b.idname' + @s + ' from A ,ID b where A.id = b.id '

--增加"取某一天的日期"的条件限制
set @s = @s + ' and A.bookdate = ''20050102'''

set @s = @s + ' group by b.idnam'

exec(@s)
rustical 2005-02-16
  • 打赏
  • 举报
回复
以 id 排序,不要用 idname 排序
rustical 2005-02-16
  • 打赏
  • 举报
回复
没有说清楚,登记表A中有很多日期,我每次取某一天的日期!
july 2005-02-16
  • 打赏
  • 举报
回复
临时表实现,楼上的先
子陌红尘 2005-02-16
  • 打赏
  • 举报
回复
--生成测试数据
create table A(
bookdate varchar(20),
timeid varchar(20),
id varchar(20),
status varchar(20))

insert into A select '20050102', '08','000001','001'
insert into A select '20050102', '08','000002','002'
insert into A select '20050102', '08','000003','001'
insert into A select '20050102', '08','000004','001'
insert into A select '20050102', '09','000001','002'
insert into A select '20050102', '09','000002','002'
insert into A select '20050102', '09','000003','003'
insert into A select '20050102', '09','000004','001'
insert into A select '20050102', '16','000001','001'
insert into A select '20050102', '16','000002','002'
insert into A select '20050102', '16','000003','001'
insert into A select '20050102', '16','000004','001'

create table ID(
id varchar(20),
idname varchar(20))

insert into ID select '000001', 'test1'
insert into ID select '000002', 'test2'
insert into ID select '000003', 'test3'

create table TIMEID(
timeid varchar(20),
timename varchar(20))

insert into TIMEID select '08','jjjjj'
insert into TIMEID select '09','ttttt'
insert into TIMEID select '10','mmmmm'
insert into TIMEID select '16','lllll'


--执行查询:
declare @s varchar(8000)
set @s = ''
select @s = @s + ',['+timeid+'] = max(case when A.timeid = '''+timeid+''' then A.status end) ' from timeid 代码表 order by timeid
exec('select b.idname'+@s+' from A , ID b where a.id = b.id group by b.idname')
rustical 2005-02-16
  • 打赏
  • 举报
回复
注意:登记表A 中的timeid(char2)如果没有登记,则结果显示为空!

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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