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

T-SQL:水平連接結(jié)果集

T-SQL: Joining result-sets horizontally(T-SQL:水平連接結(jié)果集)
本文介紹了T-SQL:水平連接結(jié)果集的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有兩個(gè)表,每個(gè)表都將自己的結(jié)果集作為一行生成.我想將這些結(jié)果集合并為一行.例如:

I have two tables, each which produce their own result-sets as a single row. I would like to join these result-sets into one single row. For example:

SELECT *
FROM Table 1
WHERE Year = 2012 AND Quarter = 1

結(jié)果:

Year  Quarter  Name  State  Mail
2012  1        Bob   NY     bob@gmail

查詢 #2:

SELECT *
FROM Table 2
WHERE Year = 2012 AND Quarter = 1

結(jié)果:

Year  Quarter  Name  State  Mail
2012  1        Greg  DC     greg@gmail

期望的結(jié)果集:

SELECT *
FROM Table 3
WHERE Year = 2012 AND Quarter = 1

Year  Quarter  T1Name  T1State  T1Mail     T2Name  T2State  T2Mail
2012  1        Bob     NY       bob@gmail  Greg    DC       greg@gmail

結(jié)果被加入/旋轉(zhuǎn)到 Year 和 Quarter 的組合上,這將通過(guò)參數(shù)輸入到查詢中.任何幫助將不勝感激.提前致謝!

The results are joined/pivoted onto the combination of Year and Quarter, which will be fed into the query via parameters. Any assistance would be greatly appreciated. Thanks in advance!

推薦答案

除非我遺漏了什么,看來(lái)你可以加入 year/quarter 似乎沒(méi)有必要對(duì)數(shù)據(jù)進(jìn)行透視:

Unless I am missing something, it looks like you can just join the tables on the year/quarter there doesn't seem to be a need to pivot the data:

select t1.year, 
  t1.quarter,
  t1.name t1Name,
  t1.state t1State,
  t1.mail t1Mail,
  t2.name t2Name,
  t2.state t2State,
  t2.mail t2Mail
from table1 t1
inner join table2 t2
  on t1.year = t2.year
  and t1.quarter = t2.quarter
where t1.year = 2012
  and t1.quarter = 1;

參見(jiàn)SQL Fiddle with Demo

現(xiàn)在如果有關(guān)于 yearquarter 是否存在于兩個(gè)表中的問(wèn)題,那么您可以使用 FULL OUTER JOIN:

Now if there is a question on whether or not the year and quarter will exist in both tables, then you could use a FULL OUTER JOIN:

select coalesce(t1.year, t2.year) year, 
  coalesce(t1.quarter, t2.quarter) quarter,
  t1.name t1Name,
  t1.state t1State,
  t1.mail t1Mail,
  t2.name t2Name,
  t2.state t2State,
  t2.mail t2Mail
from table1 t1
full outer join table2 t2
  on t1.year = t2.year
  and t1.quarter = t2.quarter
where (t1.year = 2012 and t1.quarter = 2)
  or  (t2.year = 2012 and t2.quarter = 2)

參見(jiàn)SQL Fiddle with Demo

這篇關(guān)于T-SQL:水平連接結(jié)果集的文章就介紹到這了,希望我們推薦的答案對(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)文檔推薦

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)
主站蜘蛛池模板: 日韩在线免费视频 | 国产精品一区二区久久精品爱微奶 | 国产福利资源 | 波多野结衣先锋影音 | 国产精品一区二区久久 | 99精品视频一区二区三区 | 亚洲最大成人综合 | 中文字幕视频在线观看免费 | 一本一道久久a久久精品蜜桃 | 夜夜精品视频 | 欧美一级片 | 在线免费观看视频黄 | www.国产一区 | 国内精品久久精品 | 黄色三级免费网站 | 午夜影院网站 | 日韩视频在线一区二区 | 日韩欧美国产一区二区三区 | 国产在线aa | www国产精 | 色综合av| 午夜一区二区三区 | 国产激情视频在线观看 | 精品国产青草久久久久福利 | 亚洲成人精品国产 | 色女人天堂 | 在线观看三级av | 精品视频在线播放 | 亚洲一区二区三区视频 | 久精品久久 | 中文精品视频 | 国产不卡视频在线 | 亚洲啪啪| 精品久久久久久久 | 日韩精品一区二区在线 | 最新中文字幕第一页视频 | 国产精品久久久久久久久久久久 | 日韩欧美一级精品久久 | 欧美日韩国产精品一区 | 久久伊 | 黄色片免费看视频 |