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

SQL Server 將多行合并為一行多列

SQL Server Combine multiple rows to one row with multiple columns(SQL Server 將多行合并為一行多列)
本文介紹了SQL Server 將多行合并為一行多列的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有一個(gè)數(shù)據(jù)庫(kù),其中包含以下行:

I have a database in which I have the following rows:

  ID  |  Date start  |  Date end
----------------------------------
  a   |  01-01-1950  |  30-01-1951
  a   |  01-01-1948  |  31-12-1949
  a   |  31-01-1951  |  01-06-2000
  b   |  01-01-1980  |  01-08-2010
  c   |  01-01-1990  |  31-12-2017
  c   |  31-01-1985  |  31-12-1989

我得到了什么

  • 每人多行
  • 每行一個(gè)開(kāi)始和結(jié)束日期
  • 時(shí)間順序


選擇我想返回以下內(nèi)容的查詢(xún):

Select query which I want to return the following:

  ID  |  Date start 1 |  Date end 1  |  Date start 2 |  Date end 2  |  Date start 3 |  Date end 3
---------------------------------------------------------------------------------------------------
  a   |  01-01-1948   |  31-12-1949  |  01-01-1950   |  30-01-1951  |  31-01-1951   |  01-06-2000
  b   |  01-01-1980   |  01-08-2010
  c   |  31-01-1985   |  31-12-1989  |  01-01-1990   |  31-12-2017

我想要的:

  • 每人一行
  • 每行有多個(gè)開(kāi)始和結(jié)束日期
  • 時(shí)間順序


我能找到的大多數(shù)東西都希望它在同一列中,或者不希望它按時(shí)間順序排序,所以不幸的是,這些情況不適用于我.


Most things I was able to find wanted it in the same column, or wouldn't want it sorted on chronological order, so unfortunately those situations didn't apply to me.

我現(xiàn)在真的知道如何解決這個(gè)問(wèn)題了.

I really have now clue how to solve this.

推薦答案

如果你只有三個(gè)日期,那么pivot/conditional聚合應(yīng)該沒(méi)問(wèn)題:

If you have only three dates, then pivot/conditional aggregation should be fine:

select id,
       max(case when seqnum = 1 then dstart end) as start_1,
       max(case when seqnum = 1 then dend end) as end_1,
       max(case when seqnum = 2 then dstart end) as start_2,
       max(case when seqnum = 2 then dend end) as end_2,
       max(case when seqnum = 3 then dstart end) as start_3,
       max(case when seqnum = 3 then dend end) as end_3
from (select t.*,
             row_number() over (partition by id order by dstart) as seqnum
      from t
     ) t
group by id;

注意:您必須指定輸出中的列數(shù).如果您不知道有多少,您可以:

Note: You have to specify the number of columns in the output. If you don't know how many there are, you can either:

  • 生成動(dòng)態(tài) SQL 語(yǔ)句來(lái)提前進(jìn)行計(jì)數(shù).
  • 手動(dòng)計(jì)數(shù)并添加適當(dāng)?shù)牧?

這篇關(guān)于SQL Server 將多行合并為一行多列的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個(gè)子標(biāo)記轉(zhuǎn)換為具有多個(gè)分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個(gè)表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計(jì)算值創(chuàng)建計(jì)算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對(duì)?) - IT屋-程序員軟件開(kāi)發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢(xún))
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱(chēng)轉(zhuǎn)換為日期/月份編號(hào)(問(wèn)題和答案的組合))
主站蜘蛛池模板: 成人免费看片网 | 国产精品亚洲一区 | 农村真人裸体丰满少妇毛片 | 久久久久久国产精品 | 亚洲福利在线观看 | 小川阿佐美pgd-606在线 | 久久亚洲一区二区三区四区 | 亚洲欧美在线一区 | 天堂资源视频 | 日韩精品视频网 | 男女羞羞视频大全 | 丁香综合| 亚洲一区二区三区四区五区中文 | 欧美日在线 | 久久无毛 | 成人午夜精品一区二区三区 | 亚洲精品在线免费 | 五月天国产 | www成人免费 | 国产欧美一区二区精品久导航 | 久久国产精99精产国高潮 | 天天色综| 精国产品一区二区三区 | 91精品国产综合久久香蕉922 | 国产精品视频yy9299一区 | 91精品国产综合久久国产大片 | 久久久久亚洲精品 | 龙珠z在线观看 | 欧美黄色片 | 久久久婷婷 | 99久久久99久久国产片鸭王 | 嫩草视频在线免费观看 | 欧美一区二区三区在线视频 | 一区二区三区av夏目彩春 | 国产精品久久久久久久久免费 | 91亚洲免费 | 欧美aⅴ| 一区中文字幕 | 午夜看电影在线观看 | 伊人久久精品一区二区三区 | 久久国产欧美一区二区三区精品 |