优化 SQL 查询给定的 id 列表
Refine SQL Query given list of ids
鉴于 运行 需要一段时间,我正在尝试改进此查询。困难在于数据来自一个大 table,我需要聚合一些东西。首先,我需要定义要为其获取数据的 ID。然后我需要汇总总销售额。然后我需要找到一些个人销售的指标。这是最终的 table 应该是这样的:
ID | Product Type | % of Call Sales | % of In Person Sales | Avg Price | Avg Cost | Avg Discount
A | prod 1 | 50 | 25 | 10 | 7 | 1
A | prod 2 | 50 | 75 | 11 | 4 | 2
因此,每个产品和 ID 的电话销售百分比总计为 100。列总和为 100,而不是行总和。对于个人销售额的百分比也是如此。我需要单独定义 ID,因为我需要它与区域无关。有人可以在区域 A 或区域 B 进行销售,但这并不重要。我们希望跨区域聚合。通过聚合子查询并使用 where 子句获取正确的 ID,它应该减少所需的内存。
ID查询
select distinct ids from tableA as t where year>=2021 and team = 'Sales'
这应该是唯一的 ID 列表
电话销售和人员销售的总和
select ids
,sum(case when sale = 'call' then 1 else 0 end) as call_sales
,sum(case when sale = 'person' then 1 else 0 end) as person_sales
from tableA
where
ids in t.ids
group by ids
这将与唯一 ID 如下所示,但总销售额来自 table 中的所有内容,基本上忽略了第一个查询中的 where 子句。
ids| call_sales | person_sales
A | 100 | 50
B | 60 | 80
C | 100 | 200
主要Table如上图
select ids
,prod_type
,cast(sum(case when sale = 'call' then 1 else 0 end)/CAST(call_sales AS DECIMAL(10, 2)) * 100 as DECIMAL(10,2)) as call_sales_percentage
,cast(sum(case when sale = 'person' then 1 else 0 end)/CAST(person_sales AS DECIMAL(10, 2)) * 100 as DECIMAL(10,2)) as person_sales_percentage
,mean(price) as price
,mean(cost) as cost
,mean(discount) as discount
from tableA as A
where
...conditions...
group by
...conditions...
您可以将前两个查询合并为:
select ids, sum( sale = 'call') as call_sales,
sum(sale = 'person') as person_sales
from tableA
where
ids in t.ids
group by ids
having sum(year >= 2021 and team = 'Sales') > 0;
我不太确定第三个在做什么,但您可以将上面的作为 CTE 使用,只需将其插入即可。
鉴于 运行 需要一段时间,我正在尝试改进此查询。困难在于数据来自一个大 table,我需要聚合一些东西。首先,我需要定义要为其获取数据的 ID。然后我需要汇总总销售额。然后我需要找到一些个人销售的指标。这是最终的 table 应该是这样的:
ID | Product Type | % of Call Sales | % of In Person Sales | Avg Price | Avg Cost | Avg Discount
A | prod 1 | 50 | 25 | 10 | 7 | 1
A | prod 2 | 50 | 75 | 11 | 4 | 2
因此,每个产品和 ID 的电话销售百分比总计为 100。列总和为 100,而不是行总和。对于个人销售额的百分比也是如此。我需要单独定义 ID,因为我需要它与区域无关。有人可以在区域 A 或区域 B 进行销售,但这并不重要。我们希望跨区域聚合。通过聚合子查询并使用 where 子句获取正确的 ID,它应该减少所需的内存。
ID查询
select distinct ids from tableA as t where year>=2021 and team = 'Sales'
这应该是唯一的 ID 列表
电话销售和人员销售的总和
select ids
,sum(case when sale = 'call' then 1 else 0 end) as call_sales
,sum(case when sale = 'person' then 1 else 0 end) as person_sales
from tableA
where
ids in t.ids
group by ids
这将与唯一 ID 如下所示,但总销售额来自 table 中的所有内容,基本上忽略了第一个查询中的 where 子句。
ids| call_sales | person_sales
A | 100 | 50
B | 60 | 80
C | 100 | 200
主要Table如上图
select ids
,prod_type
,cast(sum(case when sale = 'call' then 1 else 0 end)/CAST(call_sales AS DECIMAL(10, 2)) * 100 as DECIMAL(10,2)) as call_sales_percentage
,cast(sum(case when sale = 'person' then 1 else 0 end)/CAST(person_sales AS DECIMAL(10, 2)) * 100 as DECIMAL(10,2)) as person_sales_percentage
,mean(price) as price
,mean(cost) as cost
,mean(discount) as discount
from tableA as A
where
...conditions...
group by
...conditions...
您可以将前两个查询合并为:
select ids, sum( sale = 'call') as call_sales,
sum(sale = 'person') as person_sales
from tableA
where
ids in t.ids
group by ids
having sum(year >= 2021 and team = 'Sales') > 0;
我不太确定第三个在做什么,但您可以将上面的作为 CTE 使用,只需将其插入即可。