我刚刚为报告编写了此查询。但是我最初在每个子查询上都没有使用日期范围过滤器来编写它。但这没有用。所以我将它添加到每个子查询中。这很有效,但我真的不喜欢每次都重复它,是否有语法可以更简单地做同样的事情?
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