![](/img/trans.png)
[英]SQL: Count distinct values from one column based on multiple criteria in other columns
[英]Count distinct one column based on criteria on another few columns
我有樣本數據:: Data-Sample
Country Year Month CustID Sales Point TestSUM OrderType
Singapore 2018 10 AAA 623.96 520 1143.96 1
Singapore 2018 10 AAA 92.16 0 92.16 3
Singapore 2018 11 AAA 1168.28 1002.86 2171.14 1
Singapore 2018 11 BBB 118.58 103.25 221.83 1
Singapore 2018 10 CCC 118.88 103.25 222.13 1
Singapore 2018 11 CCC 278.67 217.75 496.42 1
Singapore 2018 11 CCC -108.72 -94.75 -203.47 2
Singapore 2018 11 CCC 0 0 0 3
Singapore 2018 10 DDD 446.14 385.9 832.04 1
Singapore 2018 11 DDD 138.95 121 259.95 1
Singapore 2018 11 DDD -228.07 0 -228.07 2
Singapore 2018 10 EEE 905.32 796.5 1701.82 1
Singapore 2018 10 EEE -405.75 -357.75 -763.5 2
Singapore 2018 10 EEE 29.66 0 29.66 3
Singapore 2018 11 EEE 147.8 127 274.8 1
Singapore 2018 10 FFF 0 0 0 3
Singapore 2018 10 GGG 0 0 0 3
Singapore 2018 10 HHH 120.57 104.5 225.07 1
Singapore 2018 10 HHH 0 0 0 3
Singapore 2018 11 HHH 189.59 165.25 354.84 1
Singapore 2018 10 JJJ 117.12 100.25 217.37 1
Singapore 2018 10 JJJ 0 0 0 3
Singapore 2018 11 JJJ 291.53 254.25 545.78 1
我試圖根據3個列中的2個標准計算不同的'CustID':
因此,我創建了一個額外的列'TestSUM',它將'Sales'和'Points'相加,它們不等於0,以滿足標准#1
使用下面的代碼,我可以根據我的標准#1執行不同的CustID計數時排除CustID FFF和GGG:
Select
Country
, o.Year
, o.Month
, sum(o.Sales)
, sum(o.Points)
, sum(o.Sales + o.Points) as 'TestSUM'
, o.OrderType
From FactOrders O
Join CustTable as C on o.CustID = c.CustID
and o.OrderType in (1,2,3)
Group by
Country
, o.Year
, o.Month
, o.OrderType
, c.CustID
having sum(o.Sales + o.Points) <> 0
Order by
c.CustID
但是,我希望我的結果看起來像是這樣的:
Country Year Month Distinct Count Sales
Singapore 2018 10 6 2048.06
Singapore 2018 11 7 1996.61
如何修改我的腳本以刪除“CustID”,“Point”,“TestSUM”,“OrderType”列,但仍然保持我的獨特計數反映標准
having sum(o.Sales + o.Points) <> 0
評論詳情:
我希望每個客戶應用sales + points <> 0條件,但當我從group中刪除CustID,Point,TestSum和OrderType並且選擇時,結果返回到CustID不顯示行,sales + points <> 0條件沒有不再適用於每個客戶...... CustID的獨特數量將包括銷售額+點數> 0的行數
可能你可以像下面這樣做。 請注意,我將JOIN刪除為CustTable,因為它沒有被使用。 如果FactOrders和CustTable之間沒有參照完整性,您可以將其保留。
即使他們有一行0和0,我也會把你的東西搬到你計算顧客的地方。
Select
Country=o.Country
,Year= o.Year
,Month= o.Month
,[Distinct Count]=Count(distinct custid)
,Sales= sum(o.Sales)
From FactOrders O
where
o.OrderType in (1,2,3) and (o.Sales<>0 or o.Points<>0)
Group by
o.Country, o.Year, o.Month
這會產生您的演示結果。
Select
Country=o.Country
,Year= o.Year
,Month= o.Month
,[Distinct Count]=Count(distinct custid)
,Sales= sum(o.Sales)
From FactOrders O
where
o.OrderType in (1,2,3)
and o.Sales + o.Points<>0
Group by
o.Country, o.Year, o.Month
但是,如果Sales和-10不能同時為0,則不滿足您的規定要求。如果Sales = -10且Points = 10,即使它們都不等於0,也會加起來為0。
以下應該是您正在尋找的:
Select
Country=o.Country
,Year= o.Year
,Month= o.Month
,[Distinct Count]=Count(distinct custid)
,Sales= sum(o.Sales)
From FactOrders O
where
o.OrderType in (1,2,3)
and o.Sales <> 0 and o.Points <> 0 -- because, -10 + 10 = 0
Group by
o.Country, o.Year, o.Month
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.