我有以下 SQL 查询:
SELECT
kvknum.cd_hfdrelnr,
kvknum.cd_kvkanum,
relName.cd_hfdrelnr
FROM
(
SELECT
cd_hfdrelnr,
cd_kvkanum
FROM er_105
WHERE cd_kvkanum IN
(
SELECT cd_kvkanum
FROM er_105
GROUP BY cd_kvkanum
HAVING COUNT(*) > 1
)
AND cd_kvkanum != ''
ORDER BY cd_kvkanum
) AS kvknum
LEFT OUTER JOIN
(
SELECT
cd_hfdrelnr,
cd_relnaam
FROM er_101
) AS relName
ON kvknum.cd_hfdrelnr = relName.cd_hfdrelnr
该GROUP BY
功能是不允许的,并且需要它,因此相同的cd_kvkanum
值在彼此下方一起显示,是否有解决此问题的方法或如何实现?
随之而来的错误如下:
“消息 1033,级别 15,状态 1,第 21 行 ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效,除非还指定了 TOP 或 FOR XML。消息 156,级别 15,状态1,第 28 行关键字 'AS' 附近的语法不正确。”
当我运行以下查询时:
SELECT
cd_hfdrelnr,
cd_kvkanum
FROM er_105
WHERE cd_kvkanum IN
(
SELECT cd_kvkanum
FROM er_105
GROUP BY cd_kvkanum
HAVING COUNT(*) > 1
)
AND cd_kvkanum != ''
ORDER BY cd_kvkanum
(连接的第一个子查询)结果如下:
1235 - 123
4652 - 123
8569 - 1234
4985 - 1234
虽然我想将 cd_relnaam 添加到结果列表中,但是当在查询上使用 JOIN 时,我的结果是空白的......
有人知道我做错了什么吗?