列转行结果中将空值也显示出来

Leshami
博客专家认证
2010-03-01 01:29:05

CREATE TABLE tb
(
ID CHAR(10)
,Month1qty INT
,Month2qty INT
,Month3qty INT
,Month4qty INT
,Month5qty INT
,Month6qty INT
)

INSERT INTO tb
SELECT 'F000000E24',55,30,45,NULL,NULL ,60

SELECT * FROM tb

SELECT ID
,timeperiod
,val AS performance
FROM tb
UNPIVOT (val FOR timeperiod IN (Month1qty,Month2qty,Month3qty,Month4qty,Month5qty,Month6qty)) AS x

--结果
ID timeperiod performance
---------- ------------------ -----------
F000000E24 Month1qty 55
F000000E24 Month2qty 30
F000000E24 Month3qty 45
F000000E24 Month6qty 60

(4 row(s) affected)


如何将Month4qty,Month5qty在结果中也显示出来,且performance的值显示为NULL .

...全文
272 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Leshami 2010-03-01
  • 打赏
  • 举报
回复
哦,这样子的话是比较麻烦,有20列啊
快乐_石头 2010-03-01
  • 打赏
  • 举报
回复
drop table tb
go
CREATE TABLE tb
(
ID CHAR(10)
,Month1qty INT
,Month2qty INT
,Month3qty INT
,Month4qty INT
,Month5qty INT
,Month6qty INT
)

INSERT INTO tb
SELECT 'F000000E24',55,30,45,NULL,NULL ,60
select ID,
timeperiod,
case when performance=0 then null else performance end performance
from (
SELECT ID
,timeperiod
,val AS performance
FROM (select ID,
isnull(Month1qty,0)Month1qty,
isnull(Month2qty,0)Month2qty,
isnull(Month3qty,0)Month3qty,
isnull(Month4qty,0)Month4qty,
isnull(Month5qty,0)Month5qty,
isnull(Month6qty,0)Month6qty
from tb)tb
UNPIVOT (val FOR timeperiod IN (Month1qty,Month2qty,Month3qty,Month4qty,Month5qty,Month6qty)) AS x)
t
/*
ID timeperiod performance
---------- -------------------------------------------------------------------------------------------------------------------------------- -----------
F000000E24 Month1qty 55
F000000E24 Month2qty 30
F000000E24 Month3qty 45
F000000E24 Month4qty NULL
F000000E24 Month5qty NULL
F000000E24 Month6qty 60

(6 個資料列受到影響)
*/
快乐_石头 2010-03-01
  • 打赏
  • 举报
回复
引用 7 楼 robinson_0612 的回复:
使得结果为
    ID    timeperiod        performance
---------- ------------------ -----------
F000000E24 Month1qty            55
F000000E24 Month2qty            30
F000000E24 Month3qty            45
F000000E24 Month4qty            NULL
F000000E24 Month5qty            NULL
F000000E24 Month6qty            60

非要這樣嗎
再嵌套?
Leshami 2010-03-01
  • 打赏
  • 举报
回复
使得结果为
ID timeperiod performance
---------- ------------------ -----------
F000000E24 Month1qty 55
F000000E24 Month2qty 30
F000000E24 Month3qty 45
F000000E24 Month4qty NULL
F000000E24 Month5qty NULL
F000000E24 Month6qty 60
黄_瓜 2010-03-01
  • 打赏
  • 举报
回复
引用 5 楼 robinson_0612 的回复:
引用 2 楼 happy_stone 的回复:SQL codeSELECT ID ,timeperiod ,valAS performanceFROM (select ID,isnull(Month1qty,0)Month1qty,isnull(Month2qty,0)Month2qty,isnull(Month3qty,0)Month3qty,isnull(Month4qty,0)Month4qty,isnull(Month5qty,0)Month5qty,isnull(Month6qty,0)Month6qtyfrom tb)tb UNPIVOT (valFOR timeperiodIN (Month1qty,Month2qty,Month3qty,Month4qty,Month5qty,Month6qty))AS x

isnull(Month1qty,0)
能否给个 NULL值,而不是0
--try
isnull(lie,'null')

UNPIVOT 的输入中的 NULL 不会显示在输出中
给个null 就肯定不会输出
Leshami 2010-03-01
  • 打赏
  • 举报
回复
引用 2 楼 happy_stone 的回复:
SQL codeSELECT ID
,timeperiod
,valAS performanceFROM (select ID,isnull(Month1qty,0)Month1qty,isnull(Month2qty,0)Month2qty,isnull(Month3qty,0)Month3qty,isnull(Month4qty,0)Month4qty,isnull(Month5qty,0)Month5qty,isnull(Month6qty,0)Month6qtyfrom tb)tb
UNPIVOT (valFOR timeperiodIN (Month1qty,Month2qty,Month3qty,Month4qty,Month5qty,Month6qty))AS x


isnull(Month1qty,0)
能否给个 NULL值,而不是0
黄_瓜 2010-03-01
  • 打赏
  • 举报
回复
UNPIVOT 的输入中的 NULL 不会显示在输出中
要不你用isnull() 替换掉
快乐_石头 2010-03-01
  • 打赏
  • 举报
回复
/*
ID timeperiod performance
---------- -------------------------------------------------------------------------------------------------------------------------------- -----------
F000000E24 Month1qty 55
F000000E24 Month2qty 30
F000000E24 Month3qty 45
F000000E24 Month4qty 0
F000000E24 Month5qty 0
F000000E24 Month6qty 60

(6 個資料列受到影響)
*/
快乐_石头 2010-03-01
  • 打赏
  • 举报
回复
SELECT ID
,timeperiod
,val AS performance
FROM (select ID,
isnull(Month1qty,0)Month1qty,
isnull(Month2qty,0)Month2qty,
isnull(Month3qty,0)Month3qty,
isnull(Month4qty,0)Month4qty,
isnull(Month5qty,0)Month5qty,
isnull(Month6qty,0)Month6qty
from tb)tb
UNPIVOT (val FOR timeperiod IN (Month1qty,Month2qty,Month3qty,Month4qty,Month5qty,Month6qty)) AS x
百年树人 2010-03-01
  • 打赏
  • 举报
回复
UNPIVOT 不知道,如果字段不多,可以直接union all

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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