求SQL语句(30分)

  • 主题发起人 主题发起人 雨竹
  • 开始时间 开始时间

雨竹

Unregistered / Unconfirmed
GUEST, unregistred user!
已知:表
ID1 ID2 dept
A B 1122
A B 1123
A B 1124
A B 1125
A B 1126
A B 1127
A B 1128
A B 1129
A B 1130
A B 1131
求:
ID1 ID2 DE1 DE2 DE3 DE3 DE4 DE5 DE6 DE7 DE8 DE9
A B 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
 
其中1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
全是字符型 各位大侠帮帮忙
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3478022

看看这个帖子就会了。
 
谢谢hityou给我的回答,我仔细去看看。
 
create procedure p_temp
as
select id,
case mon when 1 then qty else 0 end as [一月数量],
case mon when 2 then qty else 0 end as [二月数量],
case mon when 3 then qty else 0 end as [三月数量],
case mon when 4 then qty else 0 end as [四月数量],
case mon when 5 then qty else 0 end as [五月数量],
case mon when 6 then qty else 0 end as [六月数量],
case mon when 7 then qty else 0 end as [七月数量],
case mon when 8 then qty else 0 end as [八月数量],
case mon when 9 then qty else 0 end as [九月数量],
case mon when 10 then qty else 0 end as [十月数量],
case mon when 11 then qty else 0 end as [十一月数量],
case mon when 12 then qty else 0 end as [十二月数量]
from v_temp
这个我以前写的东西,大致的意思很好理解,我想你应该有思路了吧,这个不难的,主要是工作量的问题。
还有一种比较好的,用循环来写,就比较麻烦了。
 
谢谢hityou大侠给我的回答
问题是我得到下面个表的语句已经很繁琐
ID1 ID2 dept
A B 1122
A B 1123
A B 1124
A B 1125
A B 1126
A B 1127
A B 1128
A B 1129
A B 1130
A B 1131

我看看了您要我看的贴
其中
select a.id ,a.aaa,b.aaa,c.aaa as C from
(select id ,min(aaa) as aaa from 表 group by id) a ,
(select a.id ,a.aaa from 表 a where aaa not in

(
select aaa from
(
select id ,max(aaa) as aaa from 表 group by id
union
select id ,min(aaa) as aaa from 表 group by id
) a
)) b,
(select id ,max(aaa) as aaa from 表 group by id) c
where a.id=b.id and a.id =c.id
好像能满足我的要求
我在仔细看看
 
select a.id ,a.aaa,b.aaa,c.aaa as C from
(select id ,min(aaa) as aaa from 表 group by id) a ,
(select a.id ,a.aaa from 表 a where aaa not in

(
select aaa from
(
select id ,max(aaa) as aaa from 表 group by id
union
select id ,min(aaa) as aaa from 表 group by id
) a
)) b,
(select id ,max(aaa) as aaa from 表 group by id) c
where a.id=b.id and a.id =c.id
这个还是不行
只能满足3个的

您的
create procedure p_temp
as
select id,
case mon when 1 then qty else 0 end as [一月数量],
case mon when 2 then qty else 0 end as [二月数量],
case mon when 3 then qty else 0 end as [三月数量],
case mon when 4 then qty else 0 end as [四月数量],
case mon when 5 then qty else 0 end as [五月数量],
case mon when 6 then qty else 0 end as [六月数量],
case mon when 7 then qty else 0 end as [七月数量],
case mon when 8 then qty else 0 end as [八月数量],
case mon when 9 then qty else 0 end as [九月数量],
case mon when 10 then qty else 0 end as [十月数量],
case mon when 11 then qty else 0 end as [十一月数量],
case mon when 12 then qty else 0 end as [十二月数量]
from v_temp
我能理解
但是其中1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
全是字符型 而且其中有字母
 
你这个是想实现数据库的专制呀,用组合再进行CASE语句,可以吗?
或是用临时表来实现你的功能!
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
703
import
I
回复
1
查看
454
zbdzjx
Z
回复
2
查看
362
胡鸣
W
回复
10
查看
332
wangqinyun2000
W
后退
顶部