我在Sql server中没有太多经验。我过去的经验是在MySql上。现在,我有用Mysql编写的查询。查询如下:
SELECT *
FROM (
SELECT *
FROM table_a
WHERE
ref='123-ABC'
AND eap='Y'
AND year >= 2017
AND deleted_at IS NULL
ORDER BY year, resit desc
) x
GROUP BY
year,
MID,
(eap='Y' && resit='Y'),
(eap ='Y' && resit='F')
ORDER BY
year,
resit asc
现在,项目正在继续在Sql服务器上运行。并且需要在Sql Server的语法中转换上述查询。
谁能请用Sql Server语法转换上面的查询?我们正在使用Sql Server 2017版本。
这是您的评论查询:
SELECT
year, --ONLY values from GROUP BY or aggregations
MID,
CASE WHEN eap='Y' AND resit='Y' THEN 1 ELSE 0 END SomeFlag,
CASE WHEN eap='Y' AND resit='F' THEN 1 ELSE 0 END OtherFlag,
MAX(resit) resit --Must aggregate, take max value from group
FROM (
SELECT *
FROM table_a
WHERE ref='123-ABC'
AND eap='Y'
AND year >= 2017
AND deleted_at IS NULL
--ORDER BY year, resit desc - DELETE that, outer ordering is applied
) x
GROUP BY
year,
MID,
CASE WHEN eap='Y' AND resit='Y' THEN 1 ELSE 0 END, --CONVERT boolean to int to allow grouping
CASE WHEN eap='Y' AND resit='F' THEN 1 ELSE 0 END --Note && is replaced with AND
ORDER BY
year,
resit asc --must be in aggregate or in GROUP BY