将 MySQL 查询转换为 SQL Server (MSSQL / SQLSRV)(WiTH DISTINCT)

发布于 2024-12-12 01:30:03 字数 430 浏览 0 评论 0原文

我需要做什么才能将此查询转换为在 SQL Server 中工作?

SELECT 
    DISTINCT(map_objetivos.OBJ_RowID) AS test,
    map_objetivos.OBJ_Objetivo
FROM 
    map_admin_caminho_critico_indicadores
INNER JOIN 
    map_admin_caminho_critico ON CCR_RowID = CCI_CaminhoDR
INNER JOIN 
    map_indicadores ON IND_RowID = CCI_IndicadorDR 
INNER JOIN
    map_objetivos ON OBJ_RowID = IND_ObjetivoDR
GROUP BY
    map_objetivos.OBJ_RowID

What do I need to do to convert this query to work within in SQL Server?

SELECT 
    DISTINCT(map_objetivos.OBJ_RowID) AS test,
    map_objetivos.OBJ_Objetivo
FROM 
    map_admin_caminho_critico_indicadores
INNER JOIN 
    map_admin_caminho_critico ON CCR_RowID = CCI_CaminhoDR
INNER JOIN 
    map_indicadores ON IND_RowID = CCI_IndicadorDR 
INNER JOIN
    map_objetivos ON OBJ_RowID = IND_ObjetivoDR
GROUP BY
    map_objetivos.OBJ_RowID

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东京女 2024-12-19 01:30:03

SQL Server 与 MySQL 不同,不允许在选择列表中出现未聚合或分组的字段。另外,您的 DISTINCT 是多余的。试试这个:

SELECT 
    map_objetivos.OBJ_RowID AS test,
    map_objetivos.OBJ_Objetivo
FROM 
    map_admin_caminho_critico_indicadores
INNER JOIN 
    map_admin_caminho_critico ON CCR_RowID = CCI_CaminhoDR
INNER JOIN 
    map_indicadores ON IND_RowID = CCI_IndicadorDR 
INNER JOIN
    map_objetivos ON OBJ_RowID = IND_ObjetivoDR
GROUP BY
    map_objetivos.OBJ_RowID,
    map_objetivos.OBJ_Objetivo

SQL Server, unlike MySQL, won't allow unaggregated or grouped fields in the select list. Also, your DISTINCT is redundant. Try this:

SELECT 
    map_objetivos.OBJ_RowID AS test,
    map_objetivos.OBJ_Objetivo
FROM 
    map_admin_caminho_critico_indicadores
INNER JOIN 
    map_admin_caminho_critico ON CCR_RowID = CCI_CaminhoDR
INNER JOIN 
    map_indicadores ON IND_RowID = CCI_IndicadorDR 
INNER JOIN
    map_objetivos ON OBJ_RowID = IND_ObjetivoDR
GROUP BY
    map_objetivos.OBJ_RowID,
    map_objetivos.OBJ_Objetivo
暖树树初阳… 2024-12-19 01:30:03
SELECT 
    map_objetivos.OBJ_RowID AS test,                 <<-- all non-aggregate fields
                                                     <<-- must be in the group by
    MAX(map_objetivos.OBJ_Objetivo) as OBJ_Objetivo  <<-- put the rest in a dummy
                                                     <<-- aggregate.
FROM 
    map_admin_caminho_critico_indicadores
INNER JOIN 
    map_admin_caminho_critico ON CCR_RowID = CCI_CaminhoDR
INNER JOIN 
    map_indicadores ON IND_RowID = CCI_IndicadorDR 
INNER JOIN
    map_objetivos ON OBJ_RowID = IND_ObjetivoDR
GROUP BY
    map_objetivos.OBJ_RowID  

该查询比在 group by 子句中包含额外字段的查询运行得更快。
如果您希望查询快速运行,则只需将这些字段放入唯一定义所选字段的组中即可。
使用 MIN() 或 MAX() 聚合来让 SQL 服务器停止抱怨。
由于其他选定字段在功能上取决于 group by 子句,因此结果集中的每一行只会有一个值,并且 MAX() 函数不会减慢您的速度。

超长的 group by 子句会减慢查询速度。

请记住,仅当其他字段在功能上依赖于 group by 子句时才使用此技巧

关于 Distinct
此代码:

SELECT 
    DISTINCT(map_objetivos.OBJ_RowID) AS test,
    map_objetivos.OBJ_Objetivo

与此代码相同

SELECT DISTINCT 
    map_objetivos.OBJ_RowID AS test,
    map_objetivos.OBJ_Objetivo

您不能在选择中的单个字段上使用 DISTINCT
如果您想这样做,请在子选择中使用带有单个字段的GROUP BY

SELECT f1,f2,f3           <<-- trick to select the first distinct f1 rows.
FROM t1 WHERE id IN (
  SELECT MIN(id) as FirstID FROM t1 
  GROUP BY f1)
SELECT 
    map_objetivos.OBJ_RowID AS test,                 <<-- all non-aggregate fields
                                                     <<-- must be in the group by
    MAX(map_objetivos.OBJ_Objetivo) as OBJ_Objetivo  <<-- put the rest in a dummy
                                                     <<-- aggregate.
FROM 
    map_admin_caminho_critico_indicadores
INNER JOIN 
    map_admin_caminho_critico ON CCR_RowID = CCI_CaminhoDR
INNER JOIN 
    map_indicadores ON IND_RowID = CCI_IndicadorDR 
INNER JOIN
    map_objetivos ON OBJ_RowID = IND_ObjetivoDR
GROUP BY
    map_objetivos.OBJ_RowID  

This query will run faster than a query with extra fields in the group by clause.
If you want your queries to run fast, you need to only put those fields in the group by that uniquely define the selected fields.
Use a MIN() or MAX() aggregate to get SQL-server to stop complaining.
Because the other selected fields are functionally depended on the group by clause, there will only ever be one value per row in the resultset and the MAX() function will not slow you down.

The extra long group by clause will slow your query down.

Remember to only use this trick if the other fields are functionally dependent on the group by clause

About Distinct
This code:

SELECT 
    DISTINCT(map_objetivos.OBJ_RowID) AS test,
    map_objetivos.OBJ_Objetivo

Is the same as this code

SELECT DISTINCT 
    map_objetivos.OBJ_RowID AS test,
    map_objetivos.OBJ_Objetivo

You cannot use DISTINCT on a single field in the select.
If you want to do that use a GROUP BY with a single field in a subselect.

SELECT f1,f2,f3           <<-- trick to select the first distinct f1 rows.
FROM t1 WHERE id IN (
  SELECT MIN(id) as FirstID FROM t1 
  GROUP BY f1)
可可 2024-12-19 01:30:03

您正在对一个字段进行分组,但第二个字段未用于聚合。尝试以下操作。

SELECT  
    DISTINCT(map_objetivos.OBJ_RowID) AS test, 
    map_objetivos.OBJ_Objetivo 
FROM  
    map_admin_caminho_critico_indicadores 
INNER JOIN  
    map_admin_caminho_critico ON CCR_RowID = CCI_CaminhoDR 
INNER JOIN  
    map_indicadores ON IND_RowID = CCI_IndicadorDR  
INNER JOIN 
    map_objetivos ON OBJ_RowID = IND_ObjetivoDR 
GROUP BY 
    map_objetivos.OBJ_RowID,
    map_objetivos.OBJ_Objetivo

编辑:正如 JNK 提到的 - 在这种情况下不同是多余的。

SELECT  
    map_objetivos.OBJ_RowID AS test, 
    map_objetivos.OBJ_Objetivo 
FROM  
    map_admin_caminho_critico_indicadores 
INNER JOIN  
    map_admin_caminho_critico ON CCR_RowID = CCI_CaminhoDR 
INNER JOIN  
    map_indicadores ON IND_RowID = CCI_IndicadorDR  
INNER JOIN 
    map_objetivos ON OBJ_RowID = IND_ObjetivoDR 
GROUP BY 
    map_objetivos.OBJ_RowID,
    map_objetivos.OBJ_Objetivo

You are grouping on one field but the second field is notused in an aggregate. Try the following.

SELECT  
    DISTINCT(map_objetivos.OBJ_RowID) AS test, 
    map_objetivos.OBJ_Objetivo 
FROM  
    map_admin_caminho_critico_indicadores 
INNER JOIN  
    map_admin_caminho_critico ON CCR_RowID = CCI_CaminhoDR 
INNER JOIN  
    map_indicadores ON IND_RowID = CCI_IndicadorDR  
INNER JOIN 
    map_objetivos ON OBJ_RowID = IND_ObjetivoDR 
GROUP BY 
    map_objetivos.OBJ_RowID,
    map_objetivos.OBJ_Objetivo

Edit: As JNK mentioned - distinct in this case is redundant.

SELECT  
    map_objetivos.OBJ_RowID AS test, 
    map_objetivos.OBJ_Objetivo 
FROM  
    map_admin_caminho_critico_indicadores 
INNER JOIN  
    map_admin_caminho_critico ON CCR_RowID = CCI_CaminhoDR 
INNER JOIN  
    map_indicadores ON IND_RowID = CCI_IndicadorDR  
INNER JOIN 
    map_objetivos ON OBJ_RowID = IND_ObjetivoDR 
GROUP BY 
    map_objetivos.OBJ_RowID,
    map_objetivos.OBJ_Objetivo
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文