1
我要尋找的改進,下面查詢,任意輸入感激地接受優化SQL,現有的使用兩個全表查詢掃描CTE的
with cteA as (
select name, count(1) as "A"
from mytable
where y="A"
group by name
),
cteB as (
select name, count(1) as "B"
from mytable
where y="B"
group by name
)
SELECT cteA.name as 'name',
cteA.A as 'count x when A',
isnull(cteB.B as 'count x when B',0)
FROM
cteOne
LEFT OUTER JOIN
cteTwo
on cteA.Name = cteB.Name
order by 1
+1 - 很好! (15chars) – JNK 2010-11-04 14:26:20
當然,如果字段Y和名稱被索引,這也會很有幫助。 – HLGEM 2010-11-04 14:43:03