久久久久久久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)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 黄色免费一级片 | 97成人免费视频 | 久久精品2 | 国产精品羞羞答答 | 五月婷婷六月激情 | 波多野结衣在线观看一区二区 | 五月天婷婷激情网 | 国产福利网站 | 日韩精品一区二区三区免费视频 | 免费av播放 | 免费观看黄色片 | 91一级片 | 91av免费在线观看 | 天天操夜夜摸 | 日韩欧美国产高清91 | 日韩欧美三区 | 中文字幕在线看 | 欧美综合视频 | 天天久久 | 欧美美女性生活 | h片在线 | 欧美日韩国产中文 | 欧美激情一区二区 | 三级在线看 | 69久久久 | 国产黄色大片 | 国产成人亚洲精品自产在线 | 久久小视频 | 欧美福利在线 | 国产一区久久 | 高潮一区二区三区乱码 | 免费黄色大片 | 五月精品| 中国黄色一级片 | 久久精品久久久精品美女 | 久久日av| 免费黄网站在线观看 | 久久国产精品免费视频 | 国产成人亚洲精品自产在线 | 中文有码在线观看 | 天天爽天天爽 |