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

SQL 查詢動(dòng)態(tài) PIVOT

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

問題描述

可能的重復(fù):
SQL Server 動(dòng)態(tài) PIVOT 查詢?

我有以下結(jié)構(gòu)的臨時(shí)表:

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

我如何在此表中獲得超過一個(gè)月的數(shù)據(jù)透視表,例如:

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

請(qǐng)考慮這種表格格式.我在發(fā)布這種格式時(shí)有點(diǎn)亂.

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

推薦答案

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

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

結(jié)果是:

這篇關(guān)于SQL 查詢動(dòng)態(tài) PIVOT的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Modify Existing decimal places info(修改現(xiàn)有小數(shù)位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關(guān)名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號(hào)或管道運(yùn)算符字符串中刪除重復(fù)項(xiàng))
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關(guān)系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 久久精品视频免费看 | 久久不雅视频 | 国产精品伦 | 久久国产精品免费 | 中国av在线播放 | 亚洲午夜在线观看 | 日本免费网站 | 久久久久久爱 | 久久久久久网 | 欧美一级网站 | av一区二区三区 | 久久综合五月天 | 国产在线第一页 | 免费人成 | 亚洲特黄 | 国产三级在线 | 四虎av| 亚洲福利网站 | 免费的黄色小视频 | 丝袜美腿亚洲综合 | 99久热| 中文字幕在线免费观看 | 国产在线一 | 日本一区二区三区精品 | 日日舔| 亚洲综合在线视频 | 国产成人91 | 日韩av免费看 | 午夜理伦三级理论 | 成人做爰69片免费 | 免费在线成人 | 精品福利一区 | 欧美顶级黄色大片免费 | 国产福利视频在线观看 | 在线免费看毛片 | 永久免费av网站 | 欧美日韩精品一区 | 欧美日韩色 | 欧美综合久久 | 三级理论片 | 久久网av |