我有这个sql命令:
SELECT RecordedBy as 'Recorded By', COUNT(*) Total
FROM mytable
WHERE
StartedDate BETWEEN DATEADD(day, DATEDIFF(day, 0, CURRENT_TIMESTAMP), '00:01AM')
AND DATEADD(day, DATEDIFF(day, 0, CURRENT_TIMESTAMP), '11:59PM')
group by RecordedBy order by Total desc
我想要完成的是通过将此sql嵌入另一个而不更改原始sql 来进一步优化此查询的结果。
例如,我想通过RecordedBy子句缩小原始sql的范围:我要做的是:
select * from mytable in
(
SELECT RecordedBy as 'Recorded By', COUNT(*) Total FROM mytable
WHERE StartedDate BETWEEN
DATEADD(day, DATEDIFF(day, 0, CURRENT_TIMESTAMP),'00:01AM')
AND DATEADD(day, DATEDIFF(day, 0, CURRENT_TIMESTAMP),'11:59PM')
group by RecordedBy order by Total desc
)
但当然这会导致语法错误。 谁能想到我现在想念的东西?
答案 0 :(得分:0)
你可以试试这个
select * from
(
SELECT RecordedBy as 'Recorded By',
COUNT(*) Total
FROM mytable
WHERE StartedDate
BETWEEN
DATEADD(day, DATEDIFF(day, 0, CURRENT_TIMESTAMP),'00:01AM')
AND
DATEADD(day, DATEDIFF(day, 0, CURRENT_TIMESTAMP), '11:59PM')
) as TAB
group by TAB.RecordedBy
order by TAB.Total desc
答案 1 :(得分:0)
正确的语法是:
select * from
(
SELECT RecordedBy as 'Recorded By', COUNT(*) Total FROM mytable
WHERE StartedDate BETWEEN
DATEADD(day, DATEDIFF(day, 0, CURRENT_TIMESTAMP),'00:01AM')
AND DATEADD(day, DATEDIFF(day, 0, CURRENT_TIMESTAMP),'11:59PM')
group by RecordedBy order by Total desc
) mytable
试试吧。