[英]How do I get the count of distinct values in the first row of the result
我想檢查最高金額的文件的顏色和城市是否是多重的。 如果是,我想設置一點為1,如果不是,它應該是0
樣本數據:
Code doc year amount colour city
AB 123 2021 485 Red Paris
AB 123 2021 416 Red Paris
AB 123 2021 729 Red London
AB 123 2021 645 Red Bengaluru
預期 output:我想要 output 在一行
Code Doc Year Amount Colour City Col_Mul City_Mul
AB 123 2021 729 Red London 0 1
數量、顏色和城市應該是最大值。
我嘗試了什么:為了獲取一行中的數據,我使用了行號並按最大數量排序,並選擇了行號為 1 的數據。 但在那之后,我對顏色和城市列使用了密集排名。 但我沒有得到預期的 output。
您可以使用 CROSS APPLY 並獲取數據,如下所示:
感謝@Gayani 提供測試數據。
select TOPROW.*,case when T1.colorcount > 1 THEN 1 else 0 end as Multi_color,
case when T2.citycount > 1 THEN 1 else 0 end as Multi_city
from
(SELECT TOP 1 * FROM tes_firstRow
order by amount desc) as toprow
cross apply
(
SELECT count(distinct color) from tes_firstrow WHERE doc = toprow.doc
) as t1(colorcount)
cross apply
(
SELECT count(distinct city) from tes_firstrow WHERE doc = toprow.doc
) as t2(citycount)
代碼 | 文檔 | 年 | 數量 | 顏色 | 城市 | 多色的 | 多城市 |
---|---|---|---|---|---|---|---|
AB | 123 | 2021 | 729 | 紅色的 | 倫敦 | 0 | 1 |
我希望這個代碼示例能幫助你。 請嘗試以下代碼,如果它對您的需要有幫助,請告訴我。 我在這里使用了臨時表。 您可以使用任何技術來構建邏輯。 CTE(公用表表達式)或派生表。
CREATE TABLE tes_firstRow
(
Code varchar(100)
, doc int
, [year] int
, amount int
, color varchar(100)
, City varchar(100)
)
insert into tes_firstRow values ('AB', 123,2021,485,'RED','PARIS')
insert into tes_firstRow values ('AB', 123,2021,416,'RED','PARIS')
insert into tes_firstRow values ('AB', 123,2021,729,'RED','LONDON')
insert into tes_firstRow values ('AB', 123,2021,645,'RED','BENGALURU')
SELECT
RANK() OVER (PARTITION BY Code, doc,[year] ORDER BY amount DESC) AS [rank]
,Code
,doc
,[year]
,amount
,color
,City
INTO #temp_1
FROM tes_firstRow
SELECT
[#temp_1].[Code]
,[#temp_1].[doc]
,[#temp_1].[year]
,[#temp_1].[amount]
,[#temp_1].[color]
,[#temp_1].[City]
, (Select COUNT(Distinct [#temp_1].[color] ) where [#temp_1].[rank] = 1 ) as Col_Mul
, (Select COUNT(Distinct [#temp_1].[City]) where [#temp_1].[rank] = 1) as City_Mul
,1 as City_Mul
FROM #temp_1
WHERE #temp_1.[rank] = 1
group by [#temp_1].[Code]
,[#temp_1].[doc]
,[#temp_1].[year]
,[#temp_1].[amount]
,[#temp_1].[color]
,[#temp_1].[City]
,[#temp_1].[rank]
DROP TABLE #temp_1
Result:
那里有 go。 奇怪的要求...
SELECT T.Code, Doc, Year, MAX(T.Amount) Amount,
(SELECT TOP 1 Colour FROM T as X WHERE Amount = MAX(T.Amount)) Colour,
(SELECT TOP 1 City FROM T as X WHERE Amount = MAX(T.Amount)) City,
CASE WHEN COUNT(DISTINCT T.Colour) > 1 THEN 1 ELSE 0 END as Col_Mul,
CASE WHEN COUNT(DISTINCT T.City) > 1 THEN 1 ELSE 0 END as City_Mul
FROM T
GROUP BY T.Code, Doc, Year
我想你只想要 window 函數與條件聚合相結合:
select code, doc, year, max(amount),
max(case when seqnum = 1 then color end) as color,
max(case when seqnum = 1 then city end) as city,
(case when seqnum = 1 and color_count > 1 then 1 else 0 end) as color_dup,
(case when seqnum = 1 and city_count > 1 then 1 else 0 end) as city_dup,
from (select t.*,
row_number() over (partition by code, doc, year order by amount desc) as seqnum,
count(*) over (partition by code, doc, year, color) as color_count,
count(*) over (partition by code, doc, year, city) as city_count
from t
) t
group by code, doc, year;
我實際上不確定在值是否重復時是否需要1
,因此這些值可能是倒退的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.