如何将数据结构转换为unpivot [英] how can change data structure pivot to unpivot

查看:71
本文介绍了如何将数据结构转换为unpivot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Hi

I have database in structure like below

Newspaper   State1  state2  state3  state4 

ABC          1000     5684   5687   246
DEF           879     2547   2578   2357

But I need to the structure like below

How can i change this 

Newspaper        State      copies

ABC              State1     1000
ABC              State2     5684
ABC              State3     5687
ABC              State4     246
DEF              State1     879
DEF              State2     2547
DEF              State3     2578
DEF              State4     2357



请帮助我



Maideen


Pls help me

Maideen

推荐答案

嗨检查以下查询工作正常

Hi check this below query works fine
SELECT [NewsPaper]
      ,[State],[Copies]
FROM 
   (SELECT [NewsPaper]
      ,[State1]
      ,[State2]
      ,[State3]
      ,[State4]
   FROM [Test].[dbo].[News]) p
UNPIVOT
   (Copies FOR State IN 
      (State1,State2,State3,State4)
)AS unpvt;
GO


这篇关于如何将数据结构转换为unpivot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆