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

SQL 將表數(shù)據(jù)修改為更緊湊的形式

SQL revising table data to a more compact form(SQL 將表數(shù)據(jù)修改為更緊湊的形式)
本文介紹了SQL 將表數(shù)據(jù)修改為更緊湊的形式的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有一個(gè)表,其中包含如下建模的數(shù)據(jù)對(duì):

I have a table with data pairs modeled like the following:

Id1    Id2
-----------
100    50
120    70
70     50
34     20
50     40
40     10

Id1 總是比 Id2 大.這些對(duì)代表要進(jìn)行的替換.所以100會(huì)被50代替,然后50會(huì)被40代替,40會(huì)被10代替.

Id1 is always bigger then Id2. The pairs represent replacements to be made. So 100 will be replaced with 50, but then 50 will be replaced with 40, which will then be replaced by 10.

所以結(jié)果是這樣的:

Id1    Id2
-----------
100    10
120    10
34     20

有沒有一種簡潔的方式可以改變或加入這張表來表示這一點(diǎn)?

Is there a nice succinct way that I can alter, or join this table to represent this?

我知道我可以加入它本身類似于:

I know i can join it on itself something akin to:

SELECT t1.Id1, t2.Id2
  FROM mytable t1
  JOIN myTable t2 ON t2.Id1 = t1.Id2

但這需要多次通過,所以我為什么要問是否有更好的方法來完成它?

But this will require several passes, hence why i ask if there is a nicer way to accomplish it?

推薦答案

declare @t table(Id1 int, Id2 int)
insert @t values (100, 50)
insert @t values (    120,    70)
insert @t values (    70,     50)
insert @t values (    34,     20)
insert @t values (    50,     40)
insert @t values (    40,     10)

;with a as
(
-- find all rows without parent <*>
select id2, id1 from @t t where not exists (select 1 from @t where t.id1 = id2)
union all -- recusive work down to lowest child while storing the parent id1 
select t.id2 , a.id1
from a
join @t t on a.id2 = t.id1
)
-- show the lowest child for each row found in <*>
select id1, min(id2) id2 from a
group by id1

結(jié)果:

id1         id2
----------- -----------
34          20
100         10
120         10

這篇關(guān)于SQL 將表數(shù)據(jù)修改為更緊湊的形式的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(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屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(hào)(問題和答案的組合))
主站蜘蛛池模板: 欧美成人毛片 | 欧美日韩大片 | 99久久久国产精品 | 国产精品一区二区三区四区五区 | 国产欧美在线观看 | 老司机深夜福利视频 | 日韩av不卡在线观看 | 黄色小视频免费看 | 中文有码在线观看 | 一级做a爰片久久毛片潮喷 亚洲黄色天堂 | 欧美精品成人 | 日韩三级网 | 日本黄色录像 | 日韩性生活视频 | 人人看人人干 | 欧美一区二区视频在线观看 | 午夜在线视频观看日韩17c | 久久99深爱久久99精品 | 国产资源视频 | 国产在线a | 天天干天天拍 | 国产精品久久久久久99 | 免费的毛片 | 欧美一区二 | 国产在线一区二区三区 | 色综合久久天天综合网 | 亚洲综合五月天婷婷丁香 | 天天操夜夜 | 看黄网站在线观看 | 欧美精品二区三区四区免费看视频 | 成人免费黄色片 | 国产精品一区二区久久 | 中文字幕国产在线 | 欧美一级免费看 | 日本中文在线 | 国产91在线播放 | 一级片大全 | 五月天在线 | 午夜伦理视频 | 99久久婷婷国产综合精品草原 | 亚洲精品1 |