简单SQL语句,考考你!!

duanzhi1984 2010-03-24 08:54:08
create table #tmp(项目 varchar(4),人数 int,金额 int)  
insert into #tmp
select 1,100,40 UNION ALL
select 2,200,40

create table #tmp2(type varchar(10),typename varchar(20)) --字段名称,单位
insert into #tmp2
select '人数','人' union
select '金额','百万'

输出结果:

项目 类别 单位 数量
1 人数 人 100
2 人数 人 200
1 金额 百万 40
2 金额 百万 40
...全文
254 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
jeadong2015 2010-03-29
  • 打赏
  • 举报
回复
用自连接能做
sych888 2010-03-25
  • 打赏
  • 举报
回复
select a.项目,b.type as 类别,b.typename as 单位,a.人数 as 数量 from #Tmp a,#Tmp2 b where b.type='人数'
union all
select a.项目,b.type,b.typename,a.金额 from #Tmp a,#Tmp2 b where b.type='金额'
keiwei 2010-03-25
  • 打赏
  • 举报
回复
--稍有修改字段
/*
create table #tmp(item_no varchar(4),Mtr_Amt int,Amount1 int)
insert into #tmp
select 1,100,40 UNION ALL
select 2,200,40

create table #tmp2(type varchar(10),typename varchar(20)) --字段名称,单位
insert into #tmp2
select '人数','人' union
select '金额','百万'

输出结果:

项目 类别 单位 数量
1 人数 人 100
2 人数 人 200
1 金额 百万 40
2 金额 百万 40
*/
SELECT a.item_no as [项目],b.type as [类别],b.typename as [单位],
CASE WHEN b.type='人数' THEN a.Mtr_Amt ELSE a.Amount1 END as [数量]
FROM #Tmp a cross join #tmp2 b
ORDER BY b.type desc
samyou 2010-03-24
  • 打赏
  • 举报
回复
这个设计可以吗?
duanzhi1984 2010-03-24
  • 打赏
  • 举报
回复
.............
老黎 2010-03-24
  • 打赏
  • 举报
回复
路过学习不懂帮顶
duanzhi1984 2010-03-24
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 beirut 的回复:]
引用 9 楼 roy_88 的回复:
引用 5 楼 duanzhi1984 的回复:
引用 2 楼 roy_88 的回复:
select a.项目,b.type as 类别,b.typename as 单位,a.人数 as 数量 from #Tmp b,#Tmp2 b where b.type='人数'
union all
select a.项目,b.type,b.typename,a.……
[/Quote]

哪你给个设计!!!!
黄_瓜 2010-03-24
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 roy_88 的回复:]
引用 5 楼 duanzhi1984 的回复:
引用 2 楼 roy_88 的回复:
select a.项目,b.type as 类别,b.typename as 单位,a.人数 as 数量 from #Tmp b,#Tmp2 b where b.type='人数'
union all
select a.项目,b.type,b.typename,a.金额 from #Tmp b,#Tmp2……
[/Quote]
我也觉的设计的好别扭
就是just4 2010-03-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 roy_88 的回复:]
[/Quote]
2楼写错一个地方:
select a.项目,b.type as 类别,b.typename as 单位,a.人数 as 数量 
from #Tmp a,#Tmp2 b where b.type='人数'
union all
select a.项目,b.type,b.typename,a.金额
from #Tmp a,#Tmp2 b where b.type='金额'
幸运的意外 2010-03-24
  • 打赏
  • 举报
回复
select p1.项目,P2.type 类别,P2.typename 单位,P1.人数 as 数量 from #tmp P1,#tmp2 P2
where type='人数'
union all
select p1.项目,P2.type 类别,P2.typename 单位,P1.金额 as 数量 from #tmp P1,#tmp2 P2
where type='金额'
chuifengde 2010-03-24
  • 打赏
  • 举报
回复
SELECT 项目,Type 类别,typename 单位,case when type='人数' then 人数 else 金额 end FROM #tmp,#tmp2
GUOCHENGJUN 2010-03-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 dawugui 的回复:]
SQL code
create table #tmp(项目 varchar(4),人数 int,金额 int)
insert into #tmp
select 1,100,40 UNION ALL
select 2,200,40

create table #tmp2(type varchar(10),typename varchar(20)) --字段名称,单位
inse……
[/Quote]
sfff
dawugui 2010-03-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 duanzhi1984 的回复:]
如果#tmp表中还有很多的内容,比如:加工费,直接费用,间接费用,........有40项.

我总不能一个一个CASE吧![/Quote]
你设计成这样了,就很麻烦了.帮顶.
中国风 2010-03-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 duanzhi1984 的回复:]
引用 2 楼 roy_88 的回复:
select a.项目,b.type as 类别,b.typename as 单位,a.人数 as 数量 from #Tmp b,#Tmp2 b where b.type='人数'
union all
select a.项目,b.type,b.typename,a.金额 from #Tmp b,#Tmp2 b where b.type='金额'


……
[/Quote]
设计的问题,你可以用动态调用 syscolumns表显示
duanzhi1984 2010-03-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 beirut 的回复:]
SQL code
create table #tmp(项目 varchar(4),人数 int,金额 int)
insert into #tmp
select 1,100,40 UNION ALL
select 2,200,40

create table #tmp2(type varchar(10),typename varchar(20)) --字段名称,单位
inse……
[/Quote]
如果#tmp表中还有很多的内容,比如:加工费,直接费用,间接费用,........有40项.

我总不能一个一个CASE吧!
黄_瓜 2010-03-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 duanzhi1984 的回复:]
引用 2 楼 roy_88 的回复:
select a.项目,b.type as 类别,b.typename as 单位,a.人数 as 数量 from #Tmp b,#Tmp2 b where b.type='人数'
union all
select a.项目,b.type,b.typename,a.金额 from #Tmp b,#Tmp2 b where b.type='金额'


……
[/Quote]
试试我的
ws_hgo 2010-03-24
  • 打赏
  • 举报
回复
create table #tmp(项目 varchar(4),人数 int,金额 int)  
insert into #tmp
select 1,100,40 UNION ALL
select 2,200,40

create table #tmp2(type varchar(10),typename varchar(20)) --字段名称,单位
insert into #tmp2
select '人数','人' union
select '金额','百万'



select p1.项目,P2.type 类别,P2.typename 单位,P1.人数 as 数量 from #tmp P1,#tmp2 P2
where type='人数'
union all
select p1.项目,P2.type 类别,P2.typename 单位,P1.金额 as 数量 from #tmp P1,#tmp2 P2
where type='金额'

项目 类别 单位 数量
---- ---------- -------------------- -----------
1 人数 人 100
2 人数 人 200
1 金额 百万 40
2 金额 百万 40

(4 行受影响)
duanzhi1984 2010-03-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 roy_88 的回复:]
select a.项目,b.type as 类别,b.typename as 单位,a.人数 as 数量 from #Tmp b,#Tmp2 b where b.type='人数'
union all
select a.项目,b.type,b.typename,a.金额 from #Tmp b,#Tmp2 b where b.type='金额'
[/Quote]

如果#tmp表中还有很多的内容,比如:加工费,直接费用,间接费用,........有40项.

我总不能一个一个UNION ALL
黄_瓜 2010-03-24
  • 打赏
  • 举报
回复
create table #tmp(项目 varchar(4),人数 int,金额 int)  
insert into #tmp
select 1,100,40 UNION ALL
select 2,200,40

create table #tmp2(type varchar(10),typename varchar(20)) --字段名称,单位
insert into #tmp2
select '人数','人' union
select '金额','百万'


select 项目 , type as 类别 , typename as 单位 ,数量= case typename when '人' then 人数 else 金额 end
from #tmp2,#tmp order by type desc
/*
项目 类别 单位 数量
---- ---------- -------------------- -----------
1 人数 人 100
2 人数 人 200
1 金额 百万 40
2 金额 百万 40

(4 行受影响)


*/
dawugui 2010-03-24
  • 打赏
  • 举报
回复
create table #tmp(项目 varchar(4),人数 int,金额 int)  
insert into #tmp
select 1,100,40 UNION ALL
select 2,200,40

create table #tmp2(type varchar(10),typename varchar(20)) --字段名称,单位
insert into #tmp2
select '人数','人' union
select '金额','百万'

go

select m.项目 , n.type 类别 ,n.typename 单位 ,m.人数 数量 from #tmp m, #tmp2 n where n.type = '人数'
union all
select m.项目 , n.type 类别 ,n.typename 单位 ,m.金额 数量 from #tmp m, #tmp2 n where n.type = '金额'


drop table #tmp , #tmp2

/*
项目 类别 单位 数量
---- ---------- -------------------- -----------
1 人数 人 100
2 人数 人 200
1 金额 百万 40
2 金额 百万 40

(所影响的行数为 4 行)
*/
加载更多回复(4)

34,590

社区成员

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

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