久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

SQL 數據透視表將行展平為列

SQL pivot to flatten rows into columns(SQL 數據透視表將行展平為列)
本文介紹了SQL 數據透視表將行展平為列的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個看起來像這樣的源表:

+--------------+---------+------+-----------+-----------+|車輛索引 |父母 |年 |制作|模型 |+--------------+----------+------+------------+-----------+|1 |1 |2007 |豐田 |SIENNA LE ||2 |1 |2005 |大眾 |捷達GLS |+--------------+----------+------+------------+-----------+

我想從該表中進行選擇,以便輸出如下所示:

+-------+--------+---------+-------+------------+--------------+|第一年 |制作1 |型號1 |年2 |制作2 |模型2 |+-------+--------+-----------+-------+------------+-----------+|2007 |豐田 |塞拉·勒 |2005 |大眾 |捷達GLS |+-------+--------+-----------+-------+------------+-----------+

如何在帶有數據透視表的 SQL Server 數據庫上完成此操作?源表中總是有 1 或 2 輛車.如果有 1 輛車,我希望 Year2、Make2Model2NULL.>

解決方案

類似于 SQLZim 的回答.唯一的區別是使用 Window 函數 Row_Number() 以防 vehicleindex 不是一致的 1 和 2.

Select year1 = max(case when RN=1 then [year] end),make1 = max(當RN=1時make結束),model1 = max(當RN=1時模??型結束),year2 = max(當 RN=2 然后 [year] 結束的情況),make2 = max(當RN=2時make結束),model2 = max(當RN=2時模型結束)從 (選擇 *,RN = Row_Number() over (Partition By parentid Order By Vehicleindex)從你的表) 一種按父ID分組

<塊引用>

選項 2 - 使用 PIVOT

選擇*從 (選擇 parentid,item = concat(B.item,Dense_Rank() over (Partition By parentid Order By Vehicleindex)),價值從你的表交叉應用 ( values ('year' ,cast(Year as varchar(100))),('制作',制作),('模型',模型)) B(項目,價值)) 一種Pivot (max(value) For [item] in ([year1],[make1],[model1],[year2],[make2],[model2]) ) p

I have a source table that looks like this:

+--------------+----------+------+------------+-----------+
| vehicleindex | parentid | year |    make    |   model   |
+--------------+----------+------+------------+-----------+
|            1 |        1 | 2007 | TOYOTA     | SIENNA LE |
|            2 |        1 | 2005 | VOLKSWAGEN | JETTA GLS |
+--------------+----------+------+------------+-----------+

I'd like to select from this table such that the output looks like this:

+-------+--------+-----------+-------+------------+-----------+
| year1 | make1  |  model1   | year2 |   make2    |  model2   |
+-------+--------+-----------+-------+------------+-----------+
|  2007 | TOYOTA | SIELLA LE |  2005 | VOLKSWAGEN | JETTA GLS |
+-------+--------+-----------+-------+------------+-----------+

How can I accomplish this on a SQL Server database with a pivot? There will always be either 1 or 2 vehicles in the source table. In the case where there's 1 vehicle, I would expect Year2, Make2 and Model2 to be NULL.

解決方案

Similar to SQLZim's answer. Only difference is that the Window function Row_Number() is used just in case vehicleindex is not a consistent 1 and 2.

Select year1  = max(case when RN=1 then [year] end)
      ,make1  = max(case when RN=1 then make end)
      ,model1 = max(case when RN=1 then model end)
      ,year2  = max(case when RN=2 then [year] end)
      ,make2  = max(case when RN=2 then make end)
      ,model2 = max(case when RN=2 then model end)
 From (
        Select *
              ,RN = Row_Number() over (Partition By parentid Order By vehicleindex)
         From  YourTable
      ) A
 Group By parentid 

EDIT: Option 2 - Use PIVOT

Select *
From (
        Select parentid
              ,item     = concat(B.item,Dense_Rank() over (Partition By parentid Order By vehicleindex))
              ,value
         From  YourTable
         Cross Apply ( values ('year' ,cast(Year as varchar(100)))
                             ,('make' ,make)
                             ,('model',model)
                      ) B (item,value)
     ) A
 Pivot (max(value) For [item] in ([year1],[make1],[model1],[year2],[make2],[model2]) ) p

這篇關于SQL 數據透視表將行展平為列的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

SQL - Select all when filter value is empty(SQL - 過濾器值為空時全選)
How and where do I set the MAXRECURSION option?(如何以及在何處設置 MAXRECURSION 選項?)
TABLOCKX versus SERIALIZABLE(TABLOCKX 與可序列化)
TSQL Constants... Use Variable or Literal?(TSQL 常量...使用變量還是文字?)
TSQL RIGHT String function not working(TSQL RIGHT 字符串函數不起作用)
What is the comparative speed of temporary tables to physical tables in SQL?(SQL中臨時表與物理表的比較速度是多少?)
主站蜘蛛池模板: 欧美成人高清 | 国产99视频精品免费播放照片 | 精品一区二区三区四区五区 | 毛片毛片毛片毛片毛片 | 日韩精品成人一区二区三区视频 | 国产精品久久久久久久久免费相片 | 欧美日韩在线免费观看 | 天天干视频 | 日韩精品一区二区三区在线观看 | 亚洲精品电影在线观看 | 国产精品久久久久久久久久久新郎 | 99久久久99久久国产片鸭王 | 免费观看日韩av | 一级毛片网 | 欧美成年人网站 | www.国产精 | 国产精品久久久久无码av | 综合久久综合久久 | 久久久噜噜噜www成人网 | 国产 日韩 欧美 在线 | 日韩精品一区二区三区中文在线 | 国产一区二区三区在线 | 国内精品一区二区三区 | 韩日av在线 | 男女下面一进一出网站 | 亚洲视频中文字幕 | 国产你懂的在线观看 | 国产精品一区三区 | 成人免费精品视频 | 国产精品永久 | 国产成人精品福利 | 久久久久国产精品一区二区 | 精品国产网 | 久草www| 精品久久中文字幕 | 波多野结衣av中文字幕 | 亚洲成人福利 | 久久777 | 亚洲第一av| 中文字幕在线观 | 欧美成人视屏 |