2014-04-17 64 views
0

我需要每分鐘檢查用戶的最小值和最大值。我試圖找到這樣做,但我失敗了。SQL服務器的最小值和最大值

SELECT CONVERT(VARCHAR(10), checktime, 120), 
     CONVERT(VARCHAR(8), checktime, 108) Time, 
     userid, 
     Max(checktime)      CheckTimeEvent, 
     checktype 
FROM checkinout, 
     workdateview 
WHERE CONVERT(VARCHAR(10), checktime, 120) = workdateview.workdate 
     AND userid = 2 
     AND checktype = 'O' 
GROUP BY CONVERT(VARCHAR(10), checktime, 120), 
      CONVERT(VARCHAR(8), checktime, 108), 
      userid, 
      checktype 
UNION 
SELECT CONVERT(VARCHAR(10), checktime, 120), 
     CONVERT(VARCHAR(8), checktime, 108) Time, 
     userid, 
     Min(checktime)      CheckTimeEvent, 
     checktype 
FROM checkinout, 
     workdateview 
WHERE CONVERT(VARCHAR(10), checktime, 120) = workdateview.workdate 
     AND userid = 2 
     AND checktype = 'I' 
GROUP BY CONVERT(VARCHAR(10), checktime, 120), 
      CONVERT(VARCHAR(8), checktime, 108), 
      userid, 
      checktype 
+1

什麼** datatype **是那些列?!?!?! –

回答

0
SELECT CONVERT(VARCHAR(10), Max(checktime), 120), 
     CONVERT(VARCHAR(8), Max(checktime), 108) Time, 
     userid, 
     Max(checktime) CheckTimeEvent, 
     checktype 
FROM checkinout, 
     workdateview 
WHERE CONVERT(VARCHAR(10), checktime, 120) = workdateview.workdate 
     AND userid = 2 
     AND checktype = 'O' 
GROUP BY userid, 
      checktype 
UNION 
SELECT CONVERT(VARCHAR(10), Min(checktime) , 120), 
     CONVERT(VARCHAR(8), Min(checktime) , 108) Time, 
     userid, 
     Min(checktime)      CheckTimeEvent, 
     checktype 
FROM checkinout, 
     workdateview 
WHERE CONVERT(VARCHAR(10), checktime, 120) = workdateview.workdate 
     AND userid = 2 
     AND checktype = 'I' 
GROUP BY userid, 
      checktype 

嘗試這個

+0

非常感謝你的工作 – user3544536

0

你不應該由時間組,因爲你正在努力尋找其最大/最小值。只是按日期分組:

SELECT CONVERT(VARCHAR(10), checktime, 120), 
     CONVERT(VARCHAR(8), checktime, 108) Time, 
     userid, 
     Max(checktime)      CheckTimeEvent, 
     checktype 
FROM checkinout, 
     workdateview 
WHERE CONVERT(VARCHAR(10), checktime, 120) = workdateview.workdate 
     AND userid = 2 
     AND checktype = 'O' 
GROUP BY CONVERT(VARCHAR(10), checktime, 120), 
      userid, 
      checktype 
UNION 
SELECT CONVERT(VARCHAR(10), checktime, 120), 
     CONVERT(VARCHAR(8), checktime, 108) Time, 
     userid, 
     Min(checktime)      CheckTimeEvent, 
     checktype 
FROM checkinout, 
     workdateview 
WHERE CONVERT(VARCHAR(10), checktime, 120) = workdateview.workdate 
     AND userid = 2 
     AND checktype = 'I' 
GROUP BY CONVERT(VARCHAR(10), checktime, 120), 
      userid, 
      checktype 
+0

非常感謝你的工作 – user3544536