我有一个查询,我已经写过两种方式1)在主查询中使用where子句,然后在子查询中使用case语句。我在这里略有差异(<1%)并且希望有人能提供见解吗?
1)我的代码有问题吗? 2)根据我的陈述在哪里汇总这个是否存在差异?
代码#1:子查询和子查询中的分组依据
select Type,sum(a.product)
from
(select a.line, case when c.ID='XX' then 'xx'
when (c.ID='AA' and c.STATE not in('KY')) then 'AA' end as Type
, sum(product_ind) as product
from line_item a
join info c
on a.Dw=c.DW
where 1=1
group by a.line, case when c.ID='XX' then 'xx'
when (c.ID='AA' and c.STATE not in('KY')) then 'AA' end) a
where 1=1
and a.product>0
group by a.type
VS
代码#2:主要条款中的Where子句的子查询
select Type,sum(a.product)
from
(select a.line, c.id, c.state
from line_item a
join info c
on a.Dw=c.DW
where 1=1
group by a.line,c.id,c.state)a
where 1=1
and a.product>0
and a.ID='AA'
and a.STATE not in('KY')
group by a.type