2014-02-11 97 views
0

我有以下MySQL查詢:MySQL查詢MS SQL

SELECT date, time, custom_id, Number 
FROM traffic t 
WHERE CONCAT(date, " ", time) BETWEEN "01/06/2014 00:00" AND "01/10/2014 23:00" 
AND Number = 
(SELECT MAX(CAST(Number as SIGNED)) FROM traffic WHERE t.date = date and t.custom_id = custom_id) 
ORDER BY date, CAST(Number as SIGNED) DESC, custom_id 

這是SQLFiddle在MySQL(工作):http://sqlfiddle.com/#!2/6e1248/3

我怎麼能轉換/翻譯成MS SQL? SQLFiddle(不工作):提前http://sqlfiddle.com/#!6/6e124/1

感謝

+0

爲什麼日期和時間分隔列?除非日期格式適合DATE,否則這在MySQL中也不起作用! – Strawberry

+0

由於日期時間格式,查詢可能在MySQL中不起作用。跨越月份邊界時,「between」不起作用。 –

+0

http://sqlfiddle.com/#!6/6e124/21 – Mihai

回答

2
SELECT date, time, custom_id, Number 
FROM traffic t 
WHERE CONCAT(date, ' ', time) BETWEEN '01/06/2014 00:00' AND '01/10/2014 23:00' 
AND Number = 
(SELECT MAX(CAST(Number as smallint)) FROM traffic WHERE t.date = date and t.custom_id = custom_id) 
ORDER BY date, CAST(Number as smallint) DESC, custom_id 
+0

完美工作。完全相同的結果,謝謝! – user3288852

+0

感謝你,與SQLFiddle的鏈接更簡單! – bdn02

1

的主要問題是,SQL Server不喜歡"附上的字符串,這些都將得到處理作爲列名除去這些(和CAST S)作品:

SELECT date, time, custom_id, Number 
FROM traffic t 
WHERE CONCAT(date, ' ', time) BETWEEN '01/06/2014 00:00' AND '01/10/2014 23:00' 
AND Number = 
(SELECT MAX(Number) FROM traffic WHERE t.date = date and t.custom_id = custom_id) 
ORDER BY date, Number DESC, custom_id 

編輯:哎呀,只是注意到演員是需要的。以@ bdn02的答案爲例。

+0

這就是爲什麼我沒有得到我期待的結果。謝謝! – user3288852