紧急求助一个sql语句!!!!大家帮忙!!!

meilidami 2004-09-06 11:04:22
有个表
tmp
nbr charge name
1 2 x
1 3 y
1 3 z
2 3 x
2 5 y
其中name 的类型只有 x,y,z 三种类型
现在要查询出下列格式:
nbr x y z
1 2 3 3
1 3 5 0
其中x,y,z 列下的数字为相应的nbr 对应的charge
大家帮忙啊!!!
...全文
100 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
drift1981 2004-09-06
  • 打赏
  • 举报
回复
meiqingsong(阿飛) jinjazz(人雅的标记--落寞刺客)的都可以,可以结了
jinjazz 2004-09-06
  • 打赏
  • 举报
回复
--测试

--Ms SQL

CREATE TABLE [temp] (
[nbr] [int] NULL ,
[charge] [int] NULL ,
[name] [char] (10) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO


insert into temp
select 1,2,'x'
union
select 1,3,'y'
union
select 1,3,'z'
union
select 2,3,'x'
union
select 2,5,'y'

select nbr,
min(case when name='x' then charge end) x,
min(case when name='y' then charge end) y,
min(case when name='z' then charge end) z
from temp group by nbr

drop TABLE temp
梅青松 2004-09-06
  • 打赏
  • 举报
回复
忘了加 ‘’了
select distinct a.nbr,
(select charge from tmp where nbr = a.nbr and name = 'x') as x,
(select charge from tmp where nbr = a.nbr and name = 'y') as y,
(select charge from tmp where nbr = a.nbr and name = 'z') as z
from tmp as a
Jumpi 2004-09-06
  • 打赏
  • 举报
回复
^_^,和我的问题一样!
http://community.csdn.net/Expert/TopicView.asp?id=3324969
yinweixian 2004-09-06
  • 打赏
  • 举报
回复
select distinct a.nbr,
(select charge from tmp where nbr = a.nbr and name = x) as x,
(select charge from tmp where nbr = a.nbr and name = y) as y,
(select charge from tmp where nbr = a.nbr and name = z) as z
from tmp as a
shitianj 2004-09-06
  • 打赏
  • 举报
回复
我说的是一楼的~~
shitianj 2004-09-06
  • 打赏
  • 举报
回复
楼上正解~!
给人家分吧~

还有你如果用的是MYSQL的话,这句SQL可能不能执行
jinjazz 2004-09-06
  • 打赏
  • 举报
回复
--测试case when的用法

--Ms SQL

CREATE TABLE [t1] (
[dz1] [int] NULL ,
[dz2] [char] (10) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO


insert into t1
select 1 ,'a'
union
select 1 ,'b'
union
select 1 ,'c'
union
select 2 ,'a'
union
select 2 ,'b'
union
select 2 ,'c'
union
select 3 ,'a'
union
select 3 ,'b'
union
select 3 ,'c'

select sum(countx) sumall,
min(case when dz2='a' then countx end) countx1,
min(case when dz2='b' then countx end) countx2,
min(case when dz2='c' then countx end) countx3
from
(select dz2,count(dz1) as countx from t1 group by dz2) a
梅青松 2004-09-06
  • 打赏
  • 举报
回复
select distinct a.nbr,
(select charge from tmp where nbr = a.nbr and name = x) as x,
(select charge from tmp where nbr = a.nbr and name = y) as y,
(select charge from tmp where nbr = a.nbr and name = z) as z
from tmp as a

2,498

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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