-1

我正在寻找 namecounter = 3 的行。我尝试使用 where 和 have 但都失败了,或者我做错了什么?

这是我当前的 SQL 代码:

SELECT refnam, tstring,(SELECT COUNT('refnam') FROM refdev rc
                        WHERE rc.refnam = r.refnam
                        ) as namecounter
FROM refdev r, OUTER texte t
WHERE r.sigtnr = t.textnr
ORDER BY refnam

这是我目前的输出示例:

AAU01   AANGEVRAAGD                                                                                             4
AAU01   ACTIEF                                                                                                  4
AAU01   UIT                                                                                                     4
AAU01   AANGEVRAAGD EN ACTIEF                                                                                   4
AB001   UIT                                                                                                     2
4

3 回答 3

3
SELECT r.refnam, 
       t.tstring,
       rc.cnt namecounter
FROM refdev r
INNER JOIN (SELECT refnam, 
                   COUNT(*) cnt
              FROM refdev rc
          GROUP BY refnam
           ) rc 
        ON rc.refnam = r.refnam
LEFT OUTER JOIN texte t
             ON r.sigtnr = t.textnr
WHERE rc.cnt = 3
ORDER BY r.refnam
于 2013-10-25T09:57:25.297 回答
1

由于子查询返回查询的列,因此 WHERE 是正确的。我不理解 ypercube 评论的“外部”事物。

这应该起作用:

SELECT refnam, tstring, COUNT(*) as namecounter
FROM refdev r, texte t
WHERE r.sigtnr = t.textnr
GROUP BY refnam, tstring
HAVING COUNT(*)=3
ORDER BY refnam
于 2013-10-25T09:53:12.370 回答
0
SELECT r.refnam, 
       t.tstring,
       rc.cnt namecounter,
       d.alarmzu
FROM refdev r
INNER JOIN (SELECT refnam, 
                   COUNT(*) cnt
              FROM refdev rc
          GROUP BY refnam
           ) rc 
        ON rc.refnam = r.refnam
LEFT OUTER JOIN texte t
             ON r.sigtnr = t.textnr
JOIN devtzu d ON d.zustnr = r.zustnr
LEFT OUTER JOIN refdev_DEF RD ON d.dvtypnr = RD.dvtypnr
WHERE r.refnam = RD.refnam
AND rc.cnt = 3
ORDER BY r.refnam
于 2013-10-29T15:48:58.133 回答