我將無法做我想做的事情:(,我解釋說:
我有兩張桌子:
表格1
表2
我正在尋找可以得到 FALSE/(FALSE TRUE) 的查詢。
到目前為止,我設法通過以下方式完成了總數:
select
(select count(*) from (select Drink from Table1
where Drink=False)) as F,
(select count(*) from (select Drink from Table1
where Drink=True)) as T,
(F/(F T)) as total
有了它,我可以得到我想要的列的總數,如果我愿意,我只需要改變,Drink-->Food。
但現在我想要按 Country 的最終結果,即類似于以下內容:
預期結果
為了不違反論壇規則,我將留下到目前為止我嘗試過的內容:
select * from (select distinct t2.Country_Name, count(t1.Drink)
from Table 1 t1
left join Table 2 t2 on t2.Country_ID=t1.Country_ID
where Drink=False
group by t2.Country_Name) as F
union
select * from (select distinct t2.Country_Name, count(t1.Drink)
from Table 1 t1
left join Table 2 t2 on t2.Country_ID=t1.Country_ID
where Drink=True
group by t2.Country_Name) as T
但它甚至沒有給我帶來我所期望的:(
我不知道它是否有很大幫助,但我正在使用 Snowflake
歡迎任何幫助。謝謝!
uj5u.com熱心網友回復:
第一個查詢可以使用 COUNT_IF 來簡化:
SELECT COUNT_IF(Drink=False)/COUNT(*)
FROM Table1;
按國家分組:
SELECT t2.Country_Name, COUNT_IF(t1.Drink=False)/COUNT(*)
FROM Table1 AS t1
JOIN Table2 AS t2
ON t2.Country_ID=t1.Country_ID
GROUP BY t2.Country_Name;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/517929.html
標籤:sql雪花云数据平台
上一篇:SQL中任何月份的第一個日期
下一篇:SQL:將陣列回傳值指定為元組
