显示具有值的行会出现一定次数

时间:2013-10-09 05:56:33

标签: sql oracle

我正在尝试显示一个字段出现一定次数的行。假设下表:Campaign

+------------+-------------+--------------+---------------+  
| account_id | campaign_id | account_name | account_users |  
+------------+-------------+--------------+---------------+
|         85 |          21 | A            |            36 |  
|         45 |          69 | B            |            82 |  
|         78 |          78 | C            |            28 |  
|         69 |          65 | D            |            25 |  
|         85 |          87 | E            |            96 |  
|         78 |          12 | F            |            63 |  
|         78 |          78 | G            |            96 |  
+------------+-------------+--------------+---------------+

表格Account

+------------+--------------+
| account_id | account_code |
+------------+--------------+
|         85 | AAA          |
|         45 | ABC          |
|         78 | RTY          |
|         69 | WER          |
|         85 | AAA          |
|         78 | RTY          |
|         78 | RTY          |
+------------+--------------+

我需要在表Campaign中显示所有join表格列(account_codeaccount,仅针对那些account_id出现两个或在campaign的桌子上多次。因此,结果应该是:

+------------+-------------+--------------+---------------+--------------+
| account_id | campaign_id | account_name | account_users | account_code |
+------------+-------------+--------------+---------------+--------------+
|         85 |          21 | A            |            36 | AAA          |
|         78 |          78 | C            |            28 | RTY          |
|         85 |          87 | E            |            96 | AAA          |
|         78 |          12 | F            |            63 | RTY          |
|         78 |          78 | G            |            96 | RTY          |
+------------+-------------+--------------+---------------+--------------+

我尝试使用GROUP BYHAVING子句,但似乎无效:

SELECT c.account_id ,c.campaign_id ,c.account_name ,c.account_users ,a.account_code  
FROM campaign c  
INNER JOIN account a  
GROUP BY c.account_id, c.campaign_id, c.account_name, c.account_users, a.account_code
  HAVING count(*) >= 2;

我在这里错过了什么吗?

5 个答案:

答案 0 :(得分:1)

您正在使用COUNT作为聚合函数。尝试将其用作分析功能。

select account_id ,campaign_id ,account_name ,account_users ,account_code,
from
   (SELECT c.account_id ,c.campaign_id ,c.account_name ,c.account_users ,a.account_code,
   count(1) over (partition by c.account_id) as c
   FROM campaign c
   INNER JOIN account a on c.account_id = a.account_id)
where c >= 2;

答案 1 :(得分:1)

您的GROUP BY声明:

GROUP BY c.account_id, c.campaign_id, c.account_name, c.account_users,

让它不像你期望的那样工作。您应该GROUP BY account_id ,因为您需要 account_id 在表格广告系列

上出现两次或更多次

这样的东西
SELECT c.account_id ,c.campaign_id ,c.account_name ,c.account_users ,a.account_code
FROM campaign c
INNER JOIN account a
where c.account_id in (select account_id
            FROM campaign
            GROUP BY account_id
            HAVING COUNT(account_id) >= 2 )

答案 2 :(得分:1)

尝试一下:

SELECT DISTINCT c.*, a.account_code FROM campaign c
JOIN account a ON c.account_id = a.account_id
WHERE c.account_id IN (
  SELECT account_id FROM campaign
  GROUP BY account_id
  HAVING count(*) >= 2
)

答案 3 :(得分:1)

关于你的表格结构还有很多讨论,但是既然你问了一个具体的问题,那么简短的答案就是替换

INNER JOIN account a  

INNER JOIN (select distinct account_id,account_code from account) a 
  on (a.account_id=c.account_id)

(另外,考虑将COUNT(*)添加到您的选择列表中,仅用于进行健全性检查)

答案 4 :(得分:0)

尝试此查询我认为这是你想要的:

select q.* from
  (select t3.*,count(t3.account_id) as counter,t4.account_code
  from Campaign t3,Account t4
  where t3.id = t4.id
  group by 
  t3.account_id,t3.campaign_id,t3.account_name,t3.account_users,t4.account_code) q
where q.counter>1;