Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在不将行移动到列的情况下将列标题转换为行_Sql_Sql Server_Transpose - Fatal编程技术网

Sql 如何在不将行移动到列的情况下将列标题转换为行

Sql 如何在不将行移动到列的情况下将列标题转换为行,sql,sql-server,transpose,Sql,Sql Server,Transpose,我不确定我的问题措辞是否正确。我正在使用sql server 2014管理工作室 我有这张桌子: |------------------|--01/12/2016--|--02/12/2016--|--03/12/2016--|--04/12/2016--|<br> |----Place------|--Count--Value| Count --Value| Count-- Value|-- Count--value<br> |--Lon

我不确定我的问题措辞是否正确。我正在使用sql server 2014管理工作室

我有这张桌子:

|------------------|--01/12/2016--|--02/12/2016--|--03/12/2016--|--04/12/2016--|<br>
 |----Place------|--Count--Value|   Count   --Value|    Count--     Value|--    Count--value<br> 
|--London------|    random data--|random data--|-------------------|--------------------|                       
|--Manchester|-------------------|------------------|random data---|---------------------|                          
|--Birmingham|--------------------|---etc------------|etc----------------|-etc------------------|                           
|--Leeds--------|                           
|--Luton---------|                          
|--Scotland-----|   
|------------2016年12月1日--2016年12月2日--2016年12月3日--2016年12月4日--
|----地点-------Count--Value | Count--Value | Count--Value |--Count--Value
|--伦敦-----随机数据-----随机数据------------------------------- |--曼彻斯特随机数据 |--伯明翰——等——等——等——等——等 |--利兹----- |--卢顿--------- |--苏格兰------
我要做的是将日期从列标题转换为行,这样看起来像这样:

Place   ----------|---dates------|  Count--------|  Value<br>
London  --------|01/12/2016     
Manchester  --|01/12/2016       
Birmingham  --|01/12/2016       
Leeds   ---------|01/12/2016        
Luton   ----------|01/12/2016       
Scotland    ------|01/12/2016       
London--------  |02/12/2016     
Manchester--    |02/12/2016     
Birmingham--    |02/12/2016     
Leeds   ---------|02/12/2016        
Luton   ----------|02/12/2016       
Scotland------  |02/12/2016     
London  --------|03/12/2016     
Manchester  --|03/12/2016       
Birmingham  --|03/12/2016       
Leeds   ----------|03/12/2016       
Luton   -----------|03/12/2016      
Scotland    -------|03/12/2016      
London  ---------|04/12/2016        
Manchester  ---|04/12/2016      
Birmingham  ---|04/12/2016      
Leeds   ----------|04/12/2016       
Luton-----------    |04/12/2016     
Scotland    -------|04/12/2016
地点---------------日期----------计数----------值
伦敦-----2016年12月1日 曼彻斯特--2016年12月1日 伯明翰--2016年12月1日 利兹-----01/12/2016 卢顿-----------2016年12月1日 苏格兰------2016年12月1日 伦敦-----2016年12月2日 曼彻斯特--2016年12月2日 伯明翰--2016年12月2日 利兹-----2016年12月2日 卢顿-----------2016年12月2日 苏格兰------2016年12月2日 伦敦-----2016年3月12日 曼彻斯特--2016年12月3日 伯明翰--2016年3月12日 利兹-----03/12/2016 卢顿-----2016年12月3日 苏格兰-----2016年3月12日 伦敦------2016年4月12日 曼彻斯特——2016年12月4日 伯明翰--2016年12月4日 利兹-----2016年12月4日 卢顿-----2016年12月4日 苏格兰-----2016年12月4日
我已经搜索了转置数据,但似乎没有一个覆盖这种情况。

您想取消转置数据。我认为交叉应用
是最简单的方法:

select v.*
from t cross apply
     (values (city, cast('2016-01-12' as date), [01/12/2016]),
             (city, cast('2016-02-12' as date), [02/12/2016]),
             (city, cast('2016-03-12' as date), [03/12/2016]),
             . . .
     ) v(city, dte, cnt);

请注意,这使第二列成为真实的日期。

您可以通过R中名为“重塑”的库获得所需的输出

简单代码:

reshape::melt(DF,id="Place")
如果您有任何其他类似于place的依赖列,则可以遵循以下代码

reshape::melt(DF,id=c("Place","Type"))

参考链接是:

mySql sql server。但是有没有更有效的方法?因为不仅仅是这些日期,实际上还有1000个列被命名为日期。@VS1SQL。SQL Server对普通表的限制为1024列。“千”似乎是对的。在任何情况下,您都应该修复数据,以便以更好的格式生成数据。