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

SQL 查詢動態 PIVOT

SQL Query Dynamic PIVOT(SQL 查詢動態 PIVOT)
本文介紹了SQL 查詢動態 PIVOT的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

可能的重復:
SQL Server 動態 PIVOT 查詢?

我有以下結構的臨時表:

I have temporary table with following structure:

MONTH  ID           CNT
-----  -----------  ---
4      TOTAL_COUNT  214
5      TOTAL_COUNT  23
6      TOTAL_COUNT  23
4      FUNC_COUNT   47
5      FUNC_COUNT   5
6      FUNC_COUNT   5
4      INDIL_COUNT  167
5      INDIL_COUNT  18
6      INDIL_COUNT  18

我如何在此表中獲得超過一個月的數據透視表,例如:

How i can get the Pivot over month in this table like:

ID           APRIL  MAY  JUNE 
-----------  -----  ---  ----
TOTAL_COUNT  214    23   23
FUNC_COUNT   47     5    5
INDIL_COUNT  167    18   18

請考慮這種表格格式.我在發布這種格式時有點亂.

Please consider this table format. I am little messy in posting this format.

推薦答案

雖然您可以使用 Static Pivot - 您可以對月份進行硬編碼.在評論中,您表示月數可能未知,如果是這種情況,那么您將需要使用 Dynamic Pivot 來生成月列表.使用 Dynamic Pivot 可讓您靈活地在運行之前不知道所需的列.

While you can use a Static Pivot - one that you hard-code the months. In the comments, you stated that the number of months maybe be unknown, if that is the case then you will want to use a Dynamic Pivot to generate the list of months. Using a Dynamic Pivot gives you the flexibility of not knowing the columns you need until you run it.

create table t
( 
    [month] int, 
    [id] nvarchar(20), 
    [cnt] int 
)

insert t values (4,'TOTAL_COUNT',214)
insert t values (5,'TOTAL_COUNT',23)
insert t values (6,'TOTAL_COUNT',23)
insert t values (4,'FUNC_COUNT',47)
insert t values (5,'FUNC_COUNT',5)
insert t values (6,'FUNC_COUNT',5)
insert t values (4,'INDIL_COUNT',167)
insert t values (5,'INDIL_COUNT',18)
insert t values (6,'INDIL_COUNT',18)

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX);

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(month) 
            FROM t 
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT id, ' + @cols + ' from 
            (
                select month, id, cnt
                from t
           ) x
            pivot 
            (
                 sum(cnt)
                for month in (' + @cols + ')
            ) p '


execute(@query)

drop table t

結果是:

這篇關于SQL 查詢動態 PIVOT的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Modify Existing decimal places info(修改現有小數位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號或管道運算符字符串中刪除重復項)
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 99色视频| 久久久久久久久久久久亚洲 | 毛片一级网站 | www.中文字幕 | 一区二区三区四区视频 | 午夜免费av | 中文字幕第100页 | 中文字幕亚洲免费 | 精品亚洲一区二区 | 欧美日韩中文在线观看 | 日韩一级精品视频在线观看 | 日韩喷潮 | 日日欧美 | 丁香综合| 五月花丁香婷婷 | 亚洲精品一区二区三区四区高清 | 九九99九九精彩46 | 日韩免费av | 日韩在线免费 | 九九热精品在线 | 欧美日韩精品久久久免费观看 | 欧美炮房| 免费久 | 精品国产黄a∨片高清在线 www.一级片 国产欧美日韩综合精品一区二区 | 成人在线观看免费爱爱 | 黄色一级毛片 | 成人午夜在线观看 | 在线观看欧美日韩视频 | 日韩最新网站 | 99国内精品久久久久久久 | 偷拍自拍第一页 | 成人免费大片黄在线播放 | 久久久久亚洲精品 | 好姑娘影视在线观看高清 | 国产精品视频一区二区三区, | 午夜天堂 | 欧美成ee人免费视频 | 久久国产一区二区 | 蜜桃av一区二区三区 | 一区二区三区回区在观看免费视频 | 午夜精品久久久久久不卡欧美一级 |