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

如果有多個相同的 ID,Where 子句

Where clause if there are multiple of the same ID(如果有多個相同的 ID,Where 子句)
本文介紹了如果有多個相同的 ID,Where 子句的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有下表:

ID | source | Name | Age | ... | ...
1  | SQL    | John | 18  | ... | ...
2  | SAP    | Mike | 21  | ... | ...
2  | SQL    | Mike | 20  | ... | ...
3  | SAP    | Jill | 25  | ... | ...

我希望每個 ID 都有一個記錄.這背后的想法是,如果 ID 只出現一次(無論來源如何),則將采用該記錄.但是,如果一個 ID 有 2 條記錄,那么以 SQL 作為源的記錄將是這里使用的記錄.

I want to have one record for each ID. The idea behind this is that if the ID comes only once (no matter the Source), that record will be taken. But, If there are 2 records for one ID, the one containing SQL as source will be the used record here.

所以,在這種情況下,結果將是:

So, In this case, the result will be:

ID | source | Name | Age | ... | ...
1  | SQL    | John | 18  | ... | ...
2  | SQL    | Mike | 20  | ... | ...
3  | SAP    | Jill | 25  | ... | ...

我是通過一個分區完成的(按 Source desc 排序),但如果有一天會添加第三個源,這將無法正常工作.

I did this with a partition over (ordered by Source desc), but that wouldn't work well if a third source will be added one day.

還有其他選擇/想法嗎?

Any other options/ideas?

推薦答案

最簡單的方法(在我看來)是使用帶有排名功能的 CTE:

The easiest approach(in my opinion) is using a CTE with a ranking function:

with cte as
(
   select ID, source, Name, Age, ... , 
          rn = row_number() over (partition by ID order by case when source = 'sql'
                                                           then 0 else 1 end asc)
   from dbo.tablename
)
select ID, source, Name, Age, ...
from cte
where rn = 1

這篇關于如果有多個相同的 ID,Where 子句的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
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?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 影视一区 | 成人欧美一区二区三区白人 | 国产片侵犯亲女视频播放 | 电影91久久久 | 亚洲一区二区三区免费在线观看 | 国产在线视频一区 | 三级在线视频 | 欧美视频三区 | xnxx 日本免费 | av网站在线免费观看 | 在线观看视频你懂得 | 亚洲一区二区三区视频 | 亚洲国产aⅴ成人精品无吗 欧美激情欧美激情在线五月 | 久久久国产一区 | 黄视频免费 | 久久国产精品一区二区三区 | 国产欧美精品区一区二区三区 | 6080亚洲精品一区二区 | 在线观看黄色大片 | 日韩国产欧美在线观看 | 人人看人人草 | 久久精品国产亚洲a | 日日草天天干 | 99精品国自产在线观看 | 国产精品视频一区二区三区不卡 | 久久久久久美女 | 亚洲午夜精品 | 天天操天天怕 | 久久精品亚洲 | 亚洲在线中文字幕 | www.日韩欧美| 国产91在线播放 | 日本激情视频网 | 免费久久久 | 一区二区三区回区在观看免费视频 | 久久黄网| 国产一区视频在线 | 日韩精品在线免费 | 国产美女h视频 | 国产成人精品久久 | 国产乱码精品1区2区3区 |