执行SQL语句
Select WValue From [SSBallDB].[dbo].[WMNumber] Where WNo=01
Select WValue From [SSBallDB].[dbo].[WMNumber] Where WNo=06
Select WValue From [SSBallDB].[dbo].[WMNumber] Where WNo=12
Select WValue From [SSBallDB].[dbo].[WMNumber] Where WNo=22
Select WValue From [SSBallDB].[dbo].[WMNumber] Where WNo=23
Select WValue From [SSBallDB].[dbo].[WMNumber] Where WNo=26 得到结果WValue
-----------
2(1 行受影响)WValue
-----------
9(1 行受影响)WValue
-----------
4(1 行受影响)WValue
-----------
9(1 行受影响)WValue
-----------
4(1 行受影响)WValue
-----------
3
(1 行受影响)但我希望结果是这样的,请问如何修改WNO1. .WNO2. .WNO3. .WNO4. .WNO5. .WNO6
--------------------------------------------
. .2. .9. .4. .9. .4. .3
(1 行受影响)备注:用“. .”表示列与列之间的间隔
Select WValue From [SSBallDB].[dbo].[WMNumber] Where WNo=01
Select WValue From [SSBallDB].[dbo].[WMNumber] Where WNo=06
Select WValue From [SSBallDB].[dbo].[WMNumber] Where WNo=12
Select WValue From [SSBallDB].[dbo].[WMNumber] Where WNo=22
Select WValue From [SSBallDB].[dbo].[WMNumber] Where WNo=23
Select WValue From [SSBallDB].[dbo].[WMNumber] Where WNo=26 得到结果WValue
-----------
2(1 行受影响)WValue
-----------
9(1 行受影响)WValue
-----------
4(1 行受影响)WValue
-----------
9(1 行受影响)WValue
-----------
4(1 行受影响)WValue
-----------
3
(1 行受影响)但我希望结果是这样的,请问如何修改WNO1. .WNO2. .WNO3. .WNO4. .WNO5. .WNO6
--------------------------------------------
. .2. .9. .4. .9. .4. .3
(1 行受影响)备注:用“. .”表示列与列之间的间隔
解决方案 »
- sqlca.sqlerrtext是什么意思?
- SQL SERVER 2005连接其它数据库并导入数据表
- 为什么使用sqlserver导出数据功能却导不出字段描述,表约束也没有了..???
- [Help]有难度的左连接语句写法
- 刚用mssql2005,求教如何用2005把数据从oracle(10g)中导入。急等,谢谢!
- 求助! SQL合并查询
- 一个复杂的SQL排序问题?高手请进!!!!!!!!!
- 高手来看看! 如何使用sql语句实现ftp上传文件? 谢了
- 怎样将一个远程机器上(Internet上已知IP的机器)的数据库导入到本地?
- 请教sql server 指定的登录用户只能看到并访问它自己的数据库问题
- 使用winsock发消息的问题.
- 远程连接数据库问题 急 求助
可以这样啊!
Select WValue From [SSBallDB].[dbo].[WMNumber] Where WNo in (01,06,12,22,23,26)还有就是你的查询记录的条数是否固定,也就是最后的结果的列数是否固定,如果不固定那就要用动态SQL语句来处理了。
max(case when WNo=06 then WValue else null end),
max(case when WNo=12 then WValue else null end),
max(case when WNo=22 then WValue else null end)
from [SSBallDB].[dbo].[WMNumber]
WNO1=max(case when WNo=01 then WValue end),
WNO2=max(case when WNo=06 then WValue end),
WNO3=max(case when WNo=12 then WValue end),
WNO4=max(case when WNo=22 then WValue end),
WNO5=max(case when WNo=23 then WValue end),
WNO6=max(case when WNo=26 then WValue end)
from
(select 1 as id,WNO,WVALUE FROM [SSBallDB].[dbo].[WMNumber]) T
GROUP BY ID
执行后,结果如下
WValue
-----------
2
9
4
9
4
3(6 行受影响我要的结果是WValue
-----------
2 9 4 9 4 3(1 行受影响
Select
(Case When WNo = 01 Then WValue Else '' end) WNO1,
(Case When WNo = 06 Then WValue Else '' end) WNO2,
(Case When WNo = 12 Then WValue Else '' end) WNO3,
(Case When WNo = 22 Then WValue Else '' end) WNO4,
(Case When WNo = 23 Then WValue Else '' end) WNO5,
(Case When WNo = 26 Then WValue Else '' end) WNO6
From [User]
Select
(Case When WNo = 01 Then WValue Else '' end) WNO1,
(Case When WNo = 06 Then WValue Else '' end) WNO2,
(Case When WNo = 12 Then WValue Else '' end) WNO3,
(Case When WNo = 22 Then WValue Else '' end) WNO4,
(Case When WNo = 23 Then WValue Else '' end) WNO5,
(Case When WNo = 26 Then WValue Else '' end) WNO6
From [SSBallDB].[dbo].[WMNumber]
Insert Into 表
Select
(Case When WNo = 01 Then WValue Else '' end) WNO1,
(Case When WNo = 06 Then WValue Else '' end) WNO2,
(Case When WNo = 12 Then WValue Else '' end) WNO3,
(Case When WNo = 22 Then WValue Else '' end) WNO4,
(Case When WNo = 23 Then WValue Else '' end) WNO5,
(Case When WNo = 26 Then WValue Else '' end) WNO6
From [SSBallDB].[dbo].[WMNumber]
最后的结果的列数是固定7列的,就是WNO1. .WNO2. .WNO3. .WNO4. .WNO5. .WNO6和这6个数的和值WVALUE,一共7列,谢谢指导
WNO1=max(case when WNo=07 then WValue end),
WNO2=max(case when WNo=17 then WValue end),
WNO3=max(case when WNo=23 then WValue end),
WNO4=max(case when WNo=24 then WValue end),
WNO5=max(case when WNo=27 then WValue end),
WNO6=max(case when WNo=32 then WValue end)
into #临时表
from
(select WNO,WVALUE FROM [SSBallDB].[dbo].[WMNumber]) Tselect WNO1,WNO2,WNO3,WNO4,WNO5,WNO6,WNO1+WNO2+WNO3+WNO4+WNO5+WNO6 as WVALUEfrom #临时表
drop table #临时表