选择在oracle db中显示为组合在一起的前三对数字

时间:2015-09-29 07:15:12

标签: database oracle

根据table A,按以下顺序设置列(item_id, order_id, product_id),如下所示:

1   1   2
2   2   4
3   3   3
4   3   6
5   4   2
6   5   5
7   6   5
8   7   1
9   7   7
10  7   9
11  8   10
12  9   1
13  5   7
14  7   5
15  6   7
16  1   1
17  4   1
18  8   3
19  3   10

仅使用oracle sql选择具有相同product_ids的前三对order_id。我尝试过自联接,子查询和其他所有东西,但似乎无法正常工作......如果有人能够得到这个,那么你将成为我的编程偶像。

所以输出将是

大多数发生对:7和5因为它们一起发生三次最多发生次数:3,10因为它们一起发生两次第三次发生最多:1,2因为它们一起发生两次

所以,从逻辑上讲,当order_id = 1时,product_ids = 1和2(所以一次出现)时,当order_id = 4时,它们会再次出现在order_id = 5,6或7时,它们有5和7作为product_id& #39; s所以他们出现了3次......依此类推

3 个答案:

答案 0 :(得分:0)

如果这是你想要达到的目的,你可以试试这个:

     select product_id,myranking from 
       (select product_id, dense_rank() over(order by mycount desc) myranking from 
         (select product_id,count(*) over (partition by product_id) mycount from mytable)) 
           where myranking<4  group by product_id,myranking

输出将是order_id,其中product_id列及其等级出现次数最多

答案 1 :(得分:0)

这可能是你想要的:

select item_id,order_id,product_id,
(case when order_id = product_id then 'Yes' else 'No' end) as pairs
from tableA
having pairs = 'Yes'
order by order_id asc
limit 3

或不使用HAVING子句

select item_id,order_id,product_id
from tableA
where order_id = product_id
order by order_id asc
limit 3

答案 2 :(得分:0)

两个不同的查询(一个是层次结构,另一个是自我联接) - 两者都给出了相同的答案(针对性能更高的任何一个):

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE A (item_id, order_id, product_id) AS
          SELECT 1, 1, 2 FROM DUAL
UNION ALL SELECT 2, 2, 4 FROM DUAL
UNION ALL SELECT 3, 3, 3 FROM DUAL
UNION ALL SELECT 4, 3, 6 FROM DUAL
UNION ALL SELECT 5, 4, 2 FROM DUAL
UNION ALL SELECT 6, 5, 5 FROM DUAL
UNION ALL SELECT 7, 6, 5 FROM DUAL
UNION ALL SELECT 8, 7, 1 FROM DUAL
UNION ALL SELECT 9, 7, 7 FROM DUAL
UNION ALL SELECT 10, 7, 9 FROM DUAL
UNION ALL SELECT 11, 8, 10 FROM DUAL
UNION ALL SELECT 12, 9, 1 FROM DUAL
UNION ALL SELECT 13, 5, 7 FROM DUAL
UNION ALL SELECT 14, 7, 5 FROM DUAL
UNION ALL SELECT 15, 6, 7 FROM DUAL
UNION ALL SELECT 16, 1, 1 FROM DUAL
UNION ALL SELECT 17, 4, 1 FROM DUAL
UNION ALL SELECT 18, 8, 3 FROM DUAL
UNION ALL SELECT 19, 3, 10 FROM DUAL

查询1

SELECT *
FROM (
  SELECT lower_product_id,
         upper_product_id,
         COUNT(1) AS frequency,
         LISTAGG( order_id, ', ' ) WITHIN GROUP ( ORDER BY order_id ) AS order_ids
  FROM (
    SELECT order_id,
           lower_product_id,
           upper_product_id
    FROM (
      SELECT order_id,
             PRIOR product_id AS lower_product_id,
             product_id       AS upper_product_id,
             LEVEL            AS lvl
      FROM   A
      CONNECT BY
             PRIOR order_id = order_id
      AND    PRIOR product_id < product_id
    )
    WHERE  lvl = 2
  )
  GROUP BY lower_product_id, upper_product_id
  ORDER BY frequency DESC, lower_product_id, upper_product_id
)
WHERE ROWNUM <= 3

<强> Results

| LOWER_PRODUCT_ID | UPPER_PRODUCT_ID | FREQUENCY | ORDER_IDS |
|------------------|------------------|-----------|-----------|
|                5 |                7 |         3 |   5, 6, 7 |
|                1 |                2 |         2 |      1, 4 |
|                3 |               10 |         2 |      3, 8 |

查询2

SELECT *
FROM (
  SELECT A.product_id AS lower_product_id,
         B.product_id AS upper_product_id,
         COUNT(1) AS frequency,
         LISTAGG( A.order_id, ', ' ) WITHIN GROUP ( ORDER BY A.order_id ) AS order_ids
  FROM   A
         INNER JOIN A B
         ON (    A.order_id = B.order_id
             AND A.product_id < B.product_id )
  GROUP BY A.product_id, B.product_id
  ORDER BY frequency DESC, lower_product_id, upper_product_id
)
WHERE ROWNUM <= 3

<强> Results

| LOWER_PRODUCT_ID | UPPER_PRODUCT_ID | FREQUENCY | ORDER_IDS |
|------------------|------------------|-----------|-----------|
|                5 |                7 |         3 |   5, 6, 7 |
|                1 |                2 |         2 |      1, 4 |
|                3 |               10 |         2 |      3, 8 |