Вопрос

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