特定の日付範囲に複数のカテゴリのそれぞれからいくつのアイテムが表示されるかを判断するために、MySQL クエリを作成しています。私の最初の試みは次のようになりました:
select Title,
(select count(*) from entries where CategoryID=1
and Date >= @StartDate and Date <= @EndDate) as Cat1,
(select count(*) from entries where CategoryID=2
and Date >= @StartDate and Date <= @EndDate) as Cat2,
(select count(*) from entries where CategoryID is null
and Date >= @StartDate and Date <= @EndDate) as UnkownCategory
from entries
where Date >= @StartDate and Date <= @EndDate
テーブルは非常に大きく、クエリをリファクタリングして高速化したいと考えていますが、方法がわかりません.GROUP BY/HAVINGステートメントを使用してこれを書き直すことができますか、それとも別の方法がありませんか?
編集:サンプル結果セット - 次のようなもの:
Title | Category 1 Total | Category 2 Total | Unknown Category Total
ABC 1 3 0
DEF 2 7 2