简体   繁体   English

带分组的SQL子查询

[英]SQL sub query with group by

I have the following SQL query: 我有以下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

The GROUP BY function is not allowed and it is needed so the same cd_kvkanum values are shown together under each other, is there a work around for this or how is this possible to achieve? 不允许使用GROUP BY功能,因此需要将相同的cd_kvkanum值一起显示在彼此之下,是否可以解决此问题或如何实现?

Following error comes with it: 以下错误随之而来:

"Msg 1033, Level 15, State 1, Line 21 The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified. Msg 156, Level 15, State 1, Line 28 Incorrect syntax near the keyword 'AS'." “消息1033,级别15,状态1,行21除非还指定了TOP或FOR XML,否则ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效。消息156,级别15,状态1,第28行关键字'AS'附近的语法不正确。“

when I run the following query: 当我运行以下查询时:

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

(First subquery of the join) the results look like: (连接的第一个子查询)结果如下:
1235 - 123 1235 - 123
4652 - 123 4652 - 123
8569 - 1234 8569 - 1234
4985 - 1234 4985 - 1234

Though I want to add cd_relnaam to the result list, when Just use an JOIN on the query my results are blank... 虽然我想将cd_relnaam添加到结果列表,但是当在查询中使用JOIN时我的结果是空白的...

anybody knows what I do wrong? 谁知道我做错了什么?

It's not a complete answer, but, if I'm not mistaken, your query can be written much simpler. 这不是一个完整的答案,但是,如果我没有弄错的话,你的查询可以写得更简单。 90% of mistakes arise when people write very complex queries when they can write simple ones. 当人们在编写简单的查询时编写非常复杂的查询,就会出现90%的错误。 Simplicity is the key to success :) I'll edit the answer when you gave more exact question 简单是成功的关键:)当你提出更确切的问题时,我会编辑答案

SELECT 
    kvknum.cd_hfdrelnr, 
    kvknum.cd_kvkanum,
    relName.cd_hfdrelnr
FROM er_105 as kvknum
    LEFT OUTER JOIN er_101 as relName on relName.cd_hfdrelnr = kvknum.cd_hfdrelnr
where
    kvknum.cd_kvkanum IN
    (
        SELECT cd_kvkanum
        FROM er_105
        GROUP BY cd_kvkanum
        HAVING COUNT(*) > 1 
    ) AND
    cd_kvkanum != ''

The problem here is not so much with the GROUP BY, it's actually with the ORDER BY, you can't put it in a subquery, it has to be on your parent most query. 这里的问题不在于GROUP BY,它实际上是使用ORDER BY,你不能把它放在子查询中,它必须在你的父查询上。 Also my understanding is that you want the records from er_105 where cd_kvkanum occurs more than once and is not blank, but you don't want to aggregate. 另外我的理解是你想要来自er_105的记录,其中cd_kvkanum不止一次出现而且不是空白,但你不想聚合。

SELECT er_105.cd_hfdrelnr, 
       er_105.cd_kvkanum,
       er_101.cd_relnaam
  FROM er_105
  JOIN (SELECT cd_kvkanum,COUNT(cd_kvkanum)
          FROM er_105
         WHERE cd_kvkanum != ''
      GROUP BY cd_kvkanum
        HAVING COUNT(cd_kvkanum) > 1
       ) kvknum ON er_105.cd_kvkanum = kvknum.cd_kvkanum
LEFT JOIN er_101 ON er_105.cd_hfdrelnr = er_101.cd_hfdrelnr
 ORDER BY er_105.cd_kvkanum

Usually it's better when you want to count the occurences of something, to do SELECT something,COUNT(PrimaryKey) rather than COUNT(*). 通常情况下,当您想要计算某事物的出现时,做SELECT事物,COUNT(PrimaryKey)而不是COUNT(*)会更好。 The * Makes SQL do the extra step of figuring out what * stands for. *使SQL做出额外的步骤,找出*代表什么。 And of Course, like with many queries on stackoverflow, There's always a simpler way to write it ;) 当然,就像stackoverflow上的许多查询一样,总是有一种更简单的方法来编写它;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM