我可以对组中除返回类别之外的所有元素使用 SQL to Sum() and/or count() 吗?
Can I use SQL to Sum() and/or count() for all elements in the group except for the returned category?
我有 table 的数据。第 1 列是类别列表,第 2 列是布尔值。我有 N 个类别,每个类别有 N 行。
我想 return 一个 table 数据按类别分组,每个类别的行数汇总,以及布尔列的总和(行数与值 = 1).
我还想 return 总结:(Sum(BooleanField)/Count(BooleanField))/(Sum(BooleanField)/Count(BooleanField)),其中分子不包括行我的分组依据函数 return 对应的类别 (Category_name),分母是包罗万象的(所有类别)。
到目前为止,我有代码
SELECT(Category_name),
COUNT(BooleanField),
SUM(BooleanField),
SUM(BooleanField)/COUNT(BooleanField) -- this is % True for each category
-- some logic that takes the % true for all categories except the category
-- that we are grouping by later / by the % true overall (all observations)
FROM Data.Source
GROUP BY Category_Name
到目前为止,这段代码只是探索性的。
“幻数”列解释了我接下来要寻找的内容,其他列表示到目前为止我的代码return编辑的内容:https://docs.google.com/spreadsheets/d/17oienILCeATmH-kNzBZqz0s0Bj9ptjKZ9HfcQJCvAdA/edit#gid=0
感谢您的帮助。
Sample Data:
Category BooleanField
Cat1 0
cat1 1
cat2 1
cat2 1
cat2 1
Cat2 0
Cat2 0
Cat2 1
Cat2 1
Cat2 1
Cat3 0
Cat3 0
Cat3 0
Cat3 1
Cat4 1
Cat4 0
Cat4 0
Cat4 0
Cat4 0
Cat4 1
想要的结果
Category Percent True Sum Count Magic Number
Cat1 50.00% 1 2 1.0000
Cat2 60.00% 6 8 0.6667
Cat3 25.00% 1 4 1.1250
Cat4 33.33% 2 6 1.1429
幻数列是我要找的麻烦。我需要找到这个幻数列,以便我可以确定哪些类别最能降低总体真实百分比。这样,删除最具影响力的负面类别将最大程度地增加整体 %T。
您可以使用 OVER()
window 函数对所有值求和,然后减去当前行。
with data as (
select num, str
from (values (1, 'one'), (2, 'two'), (3, 'three'))
)
select num
, sum(num) over() everything_added
, sum(num) over() - num everything_but_this_row
from data;
使用这些基本组件,您现在可以构建任何所需的公式,例如“其中分子不包含具有类别的行”。
您可以使用 window 函数。我认为你想要的逻辑是:
select category,
avg(cast(booleanField as int)) as percent_true,
sum(cast(booleanField as int)) as total,
count(*) cnt,
(sum(sum(cast(booleanField as int))) over() - sum(cast(booleanField as int)))
/ (sum(count(*)) over() - count(*))
/ avg(cast(booleanField as int))
as magic_number
from mytable
group by category
order by category
我有 table 的数据。第 1 列是类别列表,第 2 列是布尔值。我有 N 个类别,每个类别有 N 行。
我想 return 一个 table 数据按类别分组,每个类别的行数汇总,以及布尔列的总和(行数与值 = 1).
我还想 return 总结:(Sum(BooleanField)/Count(BooleanField))/(Sum(BooleanField)/Count(BooleanField)),其中分子不包括行我的分组依据函数 return 对应的类别 (Category_name),分母是包罗万象的(所有类别)。
到目前为止,我有代码
SELECT(Category_name),
COUNT(BooleanField),
SUM(BooleanField),
SUM(BooleanField)/COUNT(BooleanField) -- this is % True for each category
-- some logic that takes the % true for all categories except the category
-- that we are grouping by later / by the % true overall (all observations)
FROM Data.Source
GROUP BY Category_Name
到目前为止,这段代码只是探索性的。
“幻数”列解释了我接下来要寻找的内容,其他列表示到目前为止我的代码return编辑的内容:https://docs.google.com/spreadsheets/d/17oienILCeATmH-kNzBZqz0s0Bj9ptjKZ9HfcQJCvAdA/edit#gid=0
感谢您的帮助。
Sample Data:
Category BooleanField
Cat1 0
cat1 1
cat2 1
cat2 1
cat2 1
Cat2 0
Cat2 0
Cat2 1
Cat2 1
Cat2 1
Cat3 0
Cat3 0
Cat3 0
Cat3 1
Cat4 1
Cat4 0
Cat4 0
Cat4 0
Cat4 0
Cat4 1
想要的结果
Category Percent True Sum Count Magic Number
Cat1 50.00% 1 2 1.0000
Cat2 60.00% 6 8 0.6667
Cat3 25.00% 1 4 1.1250
Cat4 33.33% 2 6 1.1429
幻数列是我要找的麻烦。我需要找到这个幻数列,以便我可以确定哪些类别最能降低总体真实百分比。这样,删除最具影响力的负面类别将最大程度地增加整体 %T。
您可以使用 OVER()
window 函数对所有值求和,然后减去当前行。
with data as (
select num, str
from (values (1, 'one'), (2, 'two'), (3, 'three'))
)
select num
, sum(num) over() everything_added
, sum(num) over() - num everything_but_this_row
from data;
使用这些基本组件,您现在可以构建任何所需的公式,例如“其中分子不包含具有类别的行”。
您可以使用 window 函数。我认为你想要的逻辑是:
select category,
avg(cast(booleanField as int)) as percent_true,
sum(cast(booleanField as int)) as total,
count(*) cnt,
(sum(sum(cast(booleanField as int))) over() - sum(cast(booleanField as int)))
/ (sum(count(*)) over() - count(*))
/ avg(cast(booleanField as int))
as magic_number
from mytable
group by category
order by category