0

我有这个 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
)

但这当然会导致语法错误。谁能想到我现在缺少的东西?

4

2 回答 2

0

The correct syntax would be:

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

Try it.

于 2013-03-12T10:26:16.410 回答
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
于 2013-03-12T10:19:57.303 回答