谁能在MySQL服务器查询中转换Mysql查询?

时间:2018-09-18 12:34:12

标签: sql-server sql-server-2017

我在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版本。

1 个答案:

答案 0 :(得分:1)

这是您的带有注释的查询:

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