1

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

+------------+-------------+--------------+-------- --------+  
| account_id | 活动 ID | 帐户名 | 帐户用户 |  
+------------+-------------+--------------+-------- --------+
| 85 | 21 | 一个 | 36 |  
| 45 | 69 | 乙| 82 |  
| 78 | 78 | C | 28 |  
| 69 | 65 | D | 25 |  
| 85 | 87 | E | 96 |  
| 78 | 12 | F | 63 |  
| 78 | 78 | 克| 96 |  
+------------+-------------+--------------+-------- --------+

还有桌子Account

+------------+--------------+
| account_id | 帐户代码 |
+------------+--------------+
| 85 | AAA |
| 45 | 美国广播公司 |
| 78 | RTY |
| 69 | WER |
| 85 | AAA |
| 78 | RTY |
| 78 | RTY |
+------------+--------------+

我需要显示表中所有Campaign带有 ( join)account_code的表列account,仅用于那些在表中account_id出现两次或多次的帐户campaign。因此,结果应该是:

+------------+-------------+--------------+-------- ------+--------------+
| account_id | 活动 ID | 帐户名 | 帐户用户 | 帐户代码 |
+------------+-------------+--------------+-------- ------+--------------+
| 85 | 21 | 一个 | 36 | AAA |
| 78 | 78 | C | 28 | RTY |
| 85 | 87 | E | 96 | AAA |
| 78 | 12 | F | 63 | RTY |
| 78 | 78 | 克| 96 | RTY |
+------------+-------------+--------------+-------- ------+--------------+

我尝试使用GROUP BYandHAVING子句,但它似乎不起作用:

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;

我在这里错过了什么吗?

4

5 回答 5

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;
于 2013-10-09T06:02:12.697 回答
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 )
于 2013-10-09T06:11:41.853 回答
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
)

于 2013-10-09T06:14:51.580 回答
1

关于您的表结构还有很多要讨论的内容,但是由于您提出了一个特定的问题,因此简短的回答是替换

INNER JOIN account a  

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

(另外,考虑添加COUNT(*)到您的选择列表中,如果只是为了进行完整性检查)

于 2013-10-09T06:14:59.957 回答
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;
于 2013-10-09T13:49:16.757 回答