문제

I have the following MySQL Query:

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

This is the SQLFiddle in MySQL (working): http://sqlfiddle.com/#!2/6e1248/3

How can I convert/translate it to MS SQL? SQLFiddle (not working): http://sqlfiddle.com/#!6/6e124/1

Thanks in advance

도움이 되었습니까?

해결책

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

다른 팁

The main issue is that SQL server doesn't like " to enclose strings, these will get treated as a column name removing these (and the CASTs) works:

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

Edit: Whoops, just noticed cast is needed. See the answer by @bdn02 for an example.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top