レポート用にこのクエリを書きました。しかし、私は元々、すべてのサブクエリで日付範囲フィルターなしで書いていました。しかし、それはうまくいきませんでした。そのため、各サブクエリに追加しました。それはうまくいきましたが、毎回それを繰り返さなければならないのは本当に好きではありません.同じことをより簡単にする構文はありますか?
SELECT Count(r.id) AS cnt_total,
(SELECT Count(r1.entity_id)
FROM auto_reminders_members r1
WHERE r1.reminder_id = r.reminder_id
AND r1.date_last_reminder BETWEEN CONVERT(DATETIME, '03/28/2013',
101)
AND
CONVERT(DATETIME,
'03/28/2013' + ' 23:59:59.997 ', 101)
AND r1.action = 'notnow') AS cnt_notnow,
(SELECT Count(r1.entity_id)
FROM auto_reminders_members r1
WHERE r1.reminder_id = r.reminder_id
AND r1.date_last_reminder BETWEEN CONVERT(DATETIME, '03/28/2013',
101)
AND
CONVERT(DATETIME,
'03/28/2013' + ' 23:59:59.997 ', 101)
AND r1.action = 'insert') AS cnt_insert,
(SELECT Count(r1.entity_id)
FROM auto_reminders_members r1
WHERE r1.reminder_id = r.reminder_id
AND r1.date_last_reminder BETWEEN CONVERT(DATETIME, '03/28/2013',
101)
AND
CONVERT(DATETIME,
'03/28/2013' + ' 23:59:59.997 ', 101)
AND r1.action = 'update') AS cnt_update,
(SELECT Count(r1.entity_id)
FROM auto_reminders_members r1
WHERE r1.reminder_id = r.reminder_id
AND r1.date_last_reminder BETWEEN CONVERT(DATETIME, '03/28/2013',
101)
AND
CONVERT(DATETIME,
'03/28/2013' + ' 23:59:59.997 ', 101)
AND r1.action = 'verify') AS cnt_verify
FROM auto_reminders_members r
WHERE r.reminder_id = 1
AND r.date_last_reminder BETWEEN CONVERT(DATETIME, '03/28/2013', 101) AND
CONVERT(DATETIME,
'03/28/2013' + ' 23:59:59.997 ', 101
)
GROUP BY r.reminder_id