有COUNT()没有给出预期的结果

时间:2015-05-04 12:23:59

标签: sql oracle count having-clause

尝试使用distinct,having子句获取produce.prod_id的重复值。但是没有给出预期的结果。

这是我的数据:

PRODUCE
--------- -----------
 PROD_ID   PROD_NAME
--------- -----------
  1         APPLES
  2         PEARS
  3         ORANGES
  4         BANANAS
  5         PEACHES


BUYERS
---------- ------------
 BUYER_ID   BUYER_NAME
---------- --------------
  1         ADAM BARR
  2         SEAN CHAI
  3         EVA CORETS
  4         ERIN O`MELIA


SALES
---------- --------- ------
 BUYER_ID   PROD_ID   QTY
---------- --------- ------
  1          2          15
  1          3           5
  4          1          37
  3          5          11
  4          3        1005

这是我的代码:

select produce.prod_name,
       buyer.BUYER_NAME,
       SALES.PROD_ID
  from produce
 inner join SALES on produce.PROD_ID = SALES.PROD_ID
 inner join buyer on SALES.BUYER_ID = buyer.BUYER_ID
 group by produce.prod_name,
          buyer.BUYER_NAME,
          SALES.PROD_ID
 having count(SALES.PROD_ID) > 1;

预期结果:

 PROD_Name   Buyer_Name
----------- --------------
 Oranges     ADAM BARR
 Oranges     ERIN O`MELIA

3 个答案:

答案 0 :(得分:1)

您需要根据您的要求使用分析功能

WITH PRODUCE(PROD_ID, PROD_NAME) AS (
 SELECT 1, 'APPLES' from dual union all
 select 2, 'PEARS' from dual union all
 select 3, 'ORANGES' from dual union all
 select 4, 'BANANAS' from dual union all
 select 5, 'PEACHES' from dual),
BUYERS (BUYER_ID, BUYER_NAME) as (
 select 1, 'ADAM BARR' from dual union all
 select 2, 'SEAN CHAI' from dual union all
 select 3, 'EVA CORETS' from dual union all
 select 4, 'ERIN O`MELIA' from dual),
SALES(BUYER_ID, PROD_ID, QTY) as (
 select 1, 2, 15 from dual union all
 select 1, 3, 5 from dual union all
 select 4, 1, 37 from dual union all
 select 3, 5, 11 from dual union all
 select 4, 3, 1005 from dual),
-- End of data preparation
TABLE_ AS (
  SELECT produce.prod_name,
         buyers.buyer_name,
         sales.prod_id,
         COUNT(1) OVER (PARTITION BY sales.prod_id) p_count
    FROM produce
   INNER JOIN sales
      ON produce.prod_id = sales.prod_id
   INNER JOIN buyers
      ON sales.buyer_id = buyers.buyer_id)
SELECT prod_name, buyer_name, prod_id
  FROM table_ 
 WHERE p_count > 1;

输出:

| PROD_NAME |   BUYER_NAME | PROD_ID |
|-----------|--------------|---------|
|   ORANGES | ERIN O`MELIA |       3 |
|   ORANGES |    ADAM BARR |       3 |

更新:您简化的查询将是:

With TABLE_ AS (
  SELECT produce.prod_name,
         buyers.buyer_name,
         sales.prod_id,
         COUNT(1) OVER (PARTITION BY sales.prod_id) p_count
    FROM produce
   INNER JOIN sales
      ON produce.prod_id = sales.prod_id
   INNER JOIN buyers
      ON sales.buyer_id = buyers.buyer_id)
SELECT prod_name, buyer_name, prod_id
  FROM table_ 
 WHERE p_count > 1;

答案 1 :(得分:0)

你不需要与众不同。你只需要分组和拥有。你也无法按Buyer_name分组来计算> 1.这就是问题所在,您可以将其作为一组嵌套查询来执行此操作

select dupes.prod_name, 
buyers.buyer_name 
from (

    select produce.prod_name,
        SALES.PROD_ID,
        count(SALES.PROD_ID)
    from produce 
    inner join SALES on produce.PROD_ID=SALES.PROD_ID
    group by produce.prod_name, 
        SALES.PROD_ID
    having count(*)>1 
) as dupes
inner join sales on sales.prod_id = dupes.prod_id
inner join buyers on buyers.buyer_id = sales.buyer_id

答案 2 :(得分:0)

这是我希望的简单答案。

这里也是我的SQL Fiddle便笺簿。 http://sqlfiddle.com/#!9/d80f6/3

select P.Prod_Name, Buyer_Name
from Sales S
join
   (select prod_id, count(prod_id) as ActSales
   from Sales group by prod_id having count(prod_id) > 1) SQ
on S.Prod_Id = SQ.Prod_Id
join Produce P
on S.Prod_Id = P.Prod_Id
join Buyers B
on S.Buyer_Id = B.Buyer_Id