sql查询大挑战,大侠请进...

degehuawu 2010-03-27 02:02:47
表template(id为主键,不可修改,其他都可修改)
id ord_id type desc
1 1 1 A1
2 2 1 A2
3 3 1 A3
4 1 2 B1
5 2 2 B2
6 4 2 B3
7 5 2 B4
8 1 3 C1
9 4 3 C2

表detail(id为主键,id和temlate_id不可修改,其他都可修改,temlate_id对应表template的id)
id ord_id type desc temlate_id exd act
100 1 1 A1 1 ex1 ac1
100 2 1 A2 2 ex2 ac2
100 3 1 A3 3 ex3 ac3
100 1 2 B1 4 ex5 ac5

200 1 2 B1 4 ex6 ac6
200 1 2 A2 2 ex7 ac7
200 1 1 A1 1 ex8 ac7

300 1 1 A1 1 ex1 ac1
300 2 1 A2 2 ex2 ac2
300 3 1 A3 3 ex3 ac3
300 1 3 C1 8 ex5 ac5

400 2 1 A1 1 ex8 ac9

说明:detail表中的temlate_id,ord_id,type,desc对应template表中的id,ord_id,type,desc

查询结果中的如eA1,aA1等都是取自表template的desc字段,只是在原来的如A1前加了e或a,

表detail中的exd和act分别表示预期和实际,所以对应的如A1,就会有像查询结果的eA1,aA1的值.

注意:虽然表detail也有id,ord_id,type,desc字段,但是为了保证表template的id,ord_id,type,desc改变后,

查询结果也要做出相应的改变,比如表detail的400中的ord_id为2,但在表template的ord_id为1,故应该只依据

template_id来取数据

请各位大侠帮帮忙,如何写才能得到下列查询结果,可以用sql语句,也可以用存储过程,期候佳音,谢谢!!!

查询结果:
id eA1 aA1 eA2 aA2 eA3 aA3 eB1 aB1 eB2 aB2 eC1 aC1 eC2 aC2

100 ex1 ac1 ex2 ac2 ex3 ac3 ex5 ac5

200 ex8 ac7 ex7 ac7 ex6 ac6

300 ex1 ac1 ex2 ac2 ex3 ac3 ex5 ac5

400 ex8 ac9
...全文
184 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2010-03-28
  • 打赏
  • 举报
回复
(不要高估你的汉语表达能力或者我的汉语理解能力)
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html

1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)

这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。

degehuawu 2010-03-28
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 qianjin036a 的回复:]

SQL code
id eA1 aA1 eA2 aA2 eA3 aA3 eB1 aB1 eB2 aB2 eC1 aC1 eC2 aC2
----------- ---------- -------……
[/Quote]
谢谢,不过也怪我没讲清楚,类似eA1其实是表template中的desc字段的数据e+A1,不是固定的,而且这个列也不是固定的,要看表template的
-晴天 2010-03-28
  • 打赏
  • 举报
回复
id          eA1        aA1        eA2        aA2        eA3        aA3        eB1        aB1        eB2        aB2        eC1        aC1        eC2        aC2
----------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
100 ex1 ac1 ex2 ac2 ex3 ac3 ex5 ac5 NULL NULL NULL NULL NULL NULL
200 ex8 ac7 ex7 ac7 NULL NULL ex6 ac6 NULL NULL NULL NULL NULL NULL
300 ex1 ac1 ex2 ac2 ex3 ac3 NULL NULL NULL NULL ex5 ac5 NULL NULL
400 ex8 ac9 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL

(4 行受影响)

-晴天 2010-03-28
  • 打赏
  • 举报
回复
create table template(id int,ord_id int,[type] int,[desc] nvarchar(10))
insert into template
select 1,1,1,'A1' union all
select 2,2,1,'A2' union all
select 3,3,1,'A3' union all
select 4,1,2,'B1' union all
select 5,2,2,'B2' union all
select 6,4,2,'B3' union all
select 7,5,2,'B4' union all
select 8,1,3,'C1' union all
select 9,4,3,'C2'
create table detail(id int,ord_id int,[type] int,[desc] nvarchar(10),temlate_id int,exd nvarchar(10),act nvarchar(10))
insert into detail
select 100,1,1,'A1',1,'ex1','ac1' union all
select 100,2,1,'A2',2,'ex2','ac2' union all
select 100,3,1,'A3',3,'ex3','ac3' union all
select 100,1,2,'B1',4,'ex5','ac5' union all
select 200,1,2,'B1',4,'ex6','ac6' union all
select 200,1,2,'A2',2,'ex7','ac7' union all
select 200,1,1,'A1',1,'ex8','ac7' union all
select 300,1,1,'A1',1,'ex1','ac1' union all
select 300,2,1,'A2',2,'ex2','ac2' union all
select 300,3,1,'A3',3,'ex3','ac3' union all
select 300,1,3,'C1',8,'ex5','ac5' union all
select 400,2,1,'A1',1,'ex8','ac9'
go
select a.id,
(select top 1 exd from detail where id=a.id and [desc]='A1')as eA1,
(select top 1 act from detail where id=a.id and [desc]='A1')as aA1,
(select top 1 exd from detail where id=a.id and [desc]='A2')as eA2,
(select top 1 act from detail where id=a.id and [desc]='A2')as aA2,
(select top 1 exd from detail where id=a.id and [desc]='A3')as eA3,
(select top 1 act from detail where id=a.id and [desc]='A3')as aA3,
(select top 1 exd from detail where id=a.id and [desc]='B1')as eB1,
(select top 1 act from detail where id=a.id and [desc]='B1')as aB1,
(select top 1 exd from detail where id=a.id and [desc]='B2')as eB2,
(select top 1 act from detail where id=a.id and [desc]='B2')as aB2,
(select top 1 exd from detail where id=a.id and [desc]='C1')as eC1,
(select top 1 act from detail where id=a.id and [desc]='C1')as aC1,
(select top 1 exd from detail where id=a.id and [desc]='C2')as eC2,
(select top 1 act from detail where id=a.id and [desc]='C2')as aC2
from(
select distinct id from detail
)a
go
drop table template,detail
go
master_fengj 2010-03-27
  • 打赏
  • 举报
回复
可以利用动态拼接SQL的方法,如果是在2005下,可以通过XML PATH模式查询简化这个拼接过程,提供如下一个方案:
declare @t1 table(detail_id int,template_id int,[desc] varchar(30),exd varchar(30),act varchar(30))
insert into @t1
select a.detail_id,a.template_id,a.[desc],b.exd,b.act
from (select template_id=x.id,detail_id=y.id,x.[desc] from (select id,[desc] from template) as x,(select distinct id from detail) as y) as a
left join detail as b on b.template_id=a.template_id and b.id=a.detail_id

declare @sql varchar(8000)
set @sql=''
select @sql=@sql+case @sql when '' then 'select ' else char(13)+' union all select ' end
+ 'id='+convert(varchar(20),a.detail_id)+','+
stuff((
select ',[e'+[desc]+']='''+isnull(exd,'')+''',[a'+[desc]+']='''+isnull(act,'')+'''' from @t1 as x where x.detail_id=a.detail_id for xml path('')
),1,1,'')
from (select distinct detail_id from @t1) as a

exec(@sql)
jiang900116 2010-03-27
  • 打赏
  • 举报
回复
太复杂了
jiang900116 2010-03-27
  • 打赏
  • 举报
回复
头疼、太花了
degehuawu 2010-03-27
  • 打赏
  • 举报
回复
这里我再重发一下,并且简单写成下面这些数据,后面回答问题请以这个为准,好吗?
谢谢大家帮忙
表template(id为主键,不可修改,其他都可修改)

id----ord_id---type-----desc

1------1---------1------A1
2------2---------1------A2
3------3---------1------A3

表detail(id为主键,id和temlate_id不可修改,其他都可修改,temlate_id对应表template的id)

id----ord_id----type----desc----temlate_id----exd------act

100----1--------1--------A1--------1-----------ex1-----ac1
100----2--------1--------A2--------2-----------ex2-----ac2
200----1--------1--------A1--------1-----------ex3-----ac3
200----2--------1--------A3--------3-----------ex4-----ac4

说明:detail表中的temlate_id,ord_id,type,desc对应template表中的id,ord_id,type,desc
查询结果中的如eA1,aA1等都是取自表template的desc字段,只是在原来的如A1前加了e或a,
表detail中的exd和act分别表示预期和实际,所以对应的如A1,就会有像查询结果的eA1,aA1的值.
注意:虽然表detail也有id,ord_id,type,desc字段,但是为了保证表template的id,ord_id,type,desc改

变后,
查询结果也要做出相应的改变,比如表detail的200中的ord_id为2,但在表template的ord_id为3,故应该

只依据template_id来取数据
请各位大侠帮帮忙,如何写才能得到下列查询结果,可以用sql语句,也可以用存储过程,期候佳音,谢谢!!!

查询结果
id---eA1---aA1---eA2---aA2---eA3---aA3

100---ex1---ac1---ex2---ac2

200---ex3---ac3---------------ex4---ac4
degehuawu 2010-03-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ldslove 的回复:]

eB2 aB2,eC2 aC2
没数据的也出现了,那eB3 aB3,eB4 aB4怎么没有
[/Quote]
不好意思 ,这是排版问题其实那里是没有的那个数据是在eC1 aC1的
--小F-- 2010-03-27
  • 打赏
  • 举报
回复
看不懂...
lhblxm 2010-03-27
  • 打赏
  • 举报
回复
第一次看到ID(主键)可以重复的数据
帮顶
claro 2010-03-27
  • 打赏
  • 举报
回复
没看懂
建议看看MSSQL 2005 列转行应用案例
S314324153 2010-03-27
  • 打赏
  • 举报
回复
太长,而且我没看懂
东那个升 2010-03-27
  • 打赏
  • 举报
回复
eB2 aB2,eC2 aC2
没数据的也出现了,那eB3 aB3,eB4 aB4怎么没有
yhtapmys 2010-03-27
  • 打赏
  • 举报
回复
有点长 友情UP

等高手
degehuawu 2010-03-27
  • 打赏
  • 举报
回复
哦,晕倒,不好意思呀,这个排版乱了,
对应的列在发帖前都 好好的,怎么一发完就对应不起来了呢?
大家只要一列列对应下来就可以了
谢谢
ws_hgo 2010-03-27
  • 打赏
  • 举报
回复
好长啊!

22,209

社区成员

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

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