Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 显示具有特定次数的值的行_Sql_Oracle - Fatal编程技术网

Sql 显示具有特定次数的值的行

Sql 显示具有特定次数的值的行,sql,oracle,Sql,Oracle,我试图显示一个字段出现一定次数的行。假设下表:活动 +------------+-------------+--------------+---------------+ | account_id | campaign_id | account_name | account_users | +------------+-------------+--------------+---------------+ | 85 | 21 | A

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

+------------+-------------+--------------+---------------+ | 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
中显示所有
活动
表列,其中(
join
account\u code
,只有那些具有
account\u id
的账户在表
活动
中出现两次或两次以上。因此,结果应该是:

+------------+-------------+--------------+---------------+--------------+ | 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 | +------------+-------------+--------------+---------------+--------------+
我在这里遗漏了什么吗?

您正在使用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;

您正在使用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;

您的
分组依据
声明:

按c.account\u id、c.campaign\u id、c.account\u name、c.account\u用户分组,

让它不按照你的期望工作。您应该按
帐户id分组,因为您需要帐户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 )

您的
分组依据
声明:

按c.account\u id、c.campaign\u id、c.account\u name、c.account\u用户分组,

让它不按照你的期望工作。您应该按
帐户id分组,因为您需要帐户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 )
尝试一下:

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
)
试试看:

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
)

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

INNER JOIN account a  


(也可以考虑将<代码>计数(*)<代码> >到您的选择列表,如果只用于健全性检查)

,您的表结构还有很多要讨论的,但是既然您问了一个特定的问题,那么简短的答案就是替换

INNER JOIN account a  


(也可以考虑将代码> >计数(*)<代码>到您的选择列表中,如果仅用于健全性检查)

尝试这个查询,我认为这是您想要的:

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;

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

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;

这本应该得到一张赞成票。只有第一句话是值得的:)这是我的,我应该投赞成票。只有第一句话是值得的:)这是我的