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 を別の sql に埋め込むことによって、このクエリの結果をさらに改良することです。

たとえば、元の sql を RecordedBy 句で絞り込みたいと思います。

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

あなたはこれを試すことができます

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 に答える
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

それを試してみてください。

于 2013-03-12T10:26:16.410 に答える