Select top three pairs of numbers that appear grouped together in oracle db2019 Community Moderator ElectionHow do I limit the number of rows returned by an Oracle query after ordering?Oracle SELECT TOP 10 recordsHow do I do top 1 in Oracle?Numbering of groups in select (Oracle)Oracle subqueries with Join - how to refer to subquery column?Combining two tuples into one in oracle dbOracle ListaGG, Top 3 most frequent values, given in one column, grouped by IDSQL Select Query with Join: How to Count a Certain Number of Occurences on non-primary Attribute without a Foreign KeyCombine three queries' results and use them in one another querySubquery with TOP 1 returns more than one row in Oracle NetSuite
Decoding assembly instructions in a Game Boy disassembler
Plywood subfloor won't screw down in a trailer home
Latest web browser compatible with Windows 98
Running a subshell from the middle of the current command
Should QA ask requirements to developers?
Co-worker team leader wants to inject the crap software product of his friends into our development. What should I say to our common boss?
Is it illegal in Germany to take sick leave if you caused your own illness with food?
Why would a jet engine that runs at temps excess of 2000°C burn when it crashes?
Want to switch to tankless, but can I use my existing wiring?
What does it mean when multiple 々 marks follow a 、?
Do I need to leave some extra space available on the disk which my database log files reside, for log backup operations to successfully occur?
My story is written in English, but is set in my home country. What language should I use for the dialogue?
How could a female member of a species produce eggs unto death?
Who is our nearest neighbor
Rejected in 4th interview round citing insufficient years of experience
When two POV characters meet
Excess Zinc in garden soil
Provisioning profile doesn't include the application-identifier and keychain-access-groups entitlements
Can "semicircle" be used to refer to a part-circle that is not a exact half-circle?
Humans have energy, but not water. What happens?
Is going from continuous data to categorical always wrong?
Does Linux have system calls to access all the features of the file systems it supports?
validation vs test vs training accuracy, which one to compare for claiming overfit?
Is all copper pipe pretty much the same?
Select top three pairs of numbers that appear grouped together in oracle db
2019 Community Moderator ElectionHow do I limit the number of rows returned by an Oracle query after ordering?Oracle SELECT TOP 10 recordsHow do I do top 1 in Oracle?Numbering of groups in select (Oracle)Oracle subqueries with Join - how to refer to subquery column?Combining two tuples into one in oracle dbOracle ListaGG, Top 3 most frequent values, given in one column, grouped by IDSQL Select Query with Join: How to Count a Certain Number of Occurences on non-primary Attribute without a Foreign KeyCombine three queries' results and use them in one another querySubquery with TOP 1 returns more than one row in Oracle NetSuite
Given a table A
, columns in the following order (item_id, order_id, product_id)
set up like so:
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
Select the top three pairs of product_ids
with same order_id
using only oracle sql. I have tried self joins, subqueries and everything else but cannot seem to get anything working... If someone can get this you will be my programming idol.
So the output would be
Most occurred pair: 7 and 5 because they occur together three times second most occurred: 3,10 because they occur together twice third most occurred: 1,2 because they occur together twice
So, logically, going down the list when order_id = 1 the product_ids = 1 and 2 (so one occurrence) they occur together again when order_id = 4 When order_id = 5,6, or 7 they have 5 and 7 as product_id's so they occur 3 times... and so on
database oracle
|
show 1 more comment
Given a table A
, columns in the following order (item_id, order_id, product_id)
set up like so:
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
Select the top three pairs of product_ids
with same order_id
using only oracle sql. I have tried self joins, subqueries and everything else but cannot seem to get anything working... If someone can get this you will be my programming idol.
So the output would be
Most occurred pair: 7 and 5 because they occur together three times second most occurred: 3,10 because they occur together twice third most occurred: 1,2 because they occur together twice
So, logically, going down the list when order_id = 1 the product_ids = 1 and 2 (so one occurrence) they occur together again when order_id = 4 When order_id = 5,6, or 7 they have 5 and 7 as product_id's so they occur 3 times... and so on
database oracle
1
Please give sample output for the above data
– Utsav
Sep 29 '15 at 7:18
2
What does "top three pairs of product_ids with same order_id " even mean? Can you show the required output for this input and explain how you got it?
– Mureinik
Sep 29 '15 at 7:20
We can see that in your requirement. What we need is your expected output with data.top three pairs of product_ids with same order_id
doesn't make any sense
– Utsav
Sep 29 '15 at 7:34
Top three pairs ascending or descending?
– RubahMalam
Sep 29 '15 at 7:36
RubahMalam doesn't matter if they are ascending or descending
– user2942951
Sep 29 '15 at 7:42
|
show 1 more comment
Given a table A
, columns in the following order (item_id, order_id, product_id)
set up like so:
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
Select the top three pairs of product_ids
with same order_id
using only oracle sql. I have tried self joins, subqueries and everything else but cannot seem to get anything working... If someone can get this you will be my programming idol.
So the output would be
Most occurred pair: 7 and 5 because they occur together three times second most occurred: 3,10 because they occur together twice third most occurred: 1,2 because they occur together twice
So, logically, going down the list when order_id = 1 the product_ids = 1 and 2 (so one occurrence) they occur together again when order_id = 4 When order_id = 5,6, or 7 they have 5 and 7 as product_id's so they occur 3 times... and so on
database oracle
Given a table A
, columns in the following order (item_id, order_id, product_id)
set up like so:
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
Select the top three pairs of product_ids
with same order_id
using only oracle sql. I have tried self joins, subqueries and everything else but cannot seem to get anything working... If someone can get this you will be my programming idol.
So the output would be
Most occurred pair: 7 and 5 because they occur together three times second most occurred: 3,10 because they occur together twice third most occurred: 1,2 because they occur together twice
So, logically, going down the list when order_id = 1 the product_ids = 1 and 2 (so one occurrence) they occur together again when order_id = 4 When order_id = 5,6, or 7 they have 5 and 7 as product_id's so they occur 3 times... and so on
database oracle
database oracle
edited Mar 6 at 17:36
Brian Tompsett - 汤莱恩
4,2421338102
4,2421338102
asked Sep 29 '15 at 7:15
user2942951user2942951
32
32
1
Please give sample output for the above data
– Utsav
Sep 29 '15 at 7:18
2
What does "top three pairs of product_ids with same order_id " even mean? Can you show the required output for this input and explain how you got it?
– Mureinik
Sep 29 '15 at 7:20
We can see that in your requirement. What we need is your expected output with data.top three pairs of product_ids with same order_id
doesn't make any sense
– Utsav
Sep 29 '15 at 7:34
Top three pairs ascending or descending?
– RubahMalam
Sep 29 '15 at 7:36
RubahMalam doesn't matter if they are ascending or descending
– user2942951
Sep 29 '15 at 7:42
|
show 1 more comment
1
Please give sample output for the above data
– Utsav
Sep 29 '15 at 7:18
2
What does "top three pairs of product_ids with same order_id " even mean? Can you show the required output for this input and explain how you got it?
– Mureinik
Sep 29 '15 at 7:20
We can see that in your requirement. What we need is your expected output with data.top three pairs of product_ids with same order_id
doesn't make any sense
– Utsav
Sep 29 '15 at 7:34
Top three pairs ascending or descending?
– RubahMalam
Sep 29 '15 at 7:36
RubahMalam doesn't matter if they are ascending or descending
– user2942951
Sep 29 '15 at 7:42
1
1
Please give sample output for the above data
– Utsav
Sep 29 '15 at 7:18
Please give sample output for the above data
– Utsav
Sep 29 '15 at 7:18
2
2
What does "top three pairs of product_ids with same order_id " even mean? Can you show the required output for this input and explain how you got it?
– Mureinik
Sep 29 '15 at 7:20
What does "top three pairs of product_ids with same order_id " even mean? Can you show the required output for this input and explain how you got it?
– Mureinik
Sep 29 '15 at 7:20
We can see that in your requirement. What we need is your expected output with data.
top three pairs of product_ids with same order_id
doesn't make any sense– Utsav
Sep 29 '15 at 7:34
We can see that in your requirement. What we need is your expected output with data.
top three pairs of product_ids with same order_id
doesn't make any sense– Utsav
Sep 29 '15 at 7:34
Top three pairs ascending or descending?
– RubahMalam
Sep 29 '15 at 7:36
Top three pairs ascending or descending?
– RubahMalam
Sep 29 '15 at 7:36
RubahMalam doesn't matter if they are ascending or descending
– user2942951
Sep 29 '15 at 7:42
RubahMalam doesn't matter if they are ascending or descending
– user2942951
Sep 29 '15 at 7:42
|
show 1 more comment
3 Answers
3
active
oldest
votes
Two different queries (one hierarchical and the other with a self join) - both give the same answer (go for whichever is more performant):
SQL Fiddle
Oracle 11g R2 Schema Setup:
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
Query 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 |
Query 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 |
Damn.... You are good
– user2942951
Sep 29 '15 at 10:14
add a comment |
This could be what you want :
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
or without using HAVING
clause
select item_id,order_id,product_id
from tableA
where order_id = product_id
order by order_id asc
limit 3
Think of it as consolidating the table into order_id's being unique... So when order_id = 1 all the product_id values are 1 and 2 so that is one occurrence of 1 and 2. When you consolidate order_id = 5 the product_id's would be 5 and 7 so that is one occurrence of 5 and 7. That make more sense?
– user2942951
Sep 29 '15 at 7:58
@user2942951 Yes, I was missunderstood your question. Why don't put desired output in question instead of put them in comment?
– RubahMalam
Sep 29 '15 at 8:04
add a comment |
If that's what you want to achieve,you can try this:
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
the output will be order_id with the most occurences at the product_id column and its rank
This is close but not order_id and item_id pair. You want the top three occurring pairs of product_id's.
– user2942951
Sep 29 '15 at 7:54
@user2942951 I updated my answer :)
– Vance
Sep 29 '15 at 9:20
Wow you are so close!!! But two things: 1. The query you wrote returns product_id = 2 as having counted 3 of them when only 2 is given. 2. You have to find a way to only count the product_id occurrence if it shows up with the other number (for example: you count product_id occurrence even if it isn't with its other pair. Think of the pair that share order_id as a single count of that pair not as individuals) Keep trying you are on the verge!! :D
– user2942951
Sep 29 '15 at 9:54
and when product_id =1 you return 1 but it should return 2(product_id = 2 occurs 4 times but its highest pair is with 2 and theyoccur together 2times
– user2942951
Sep 29 '15 at 10:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32837892%2fselect-top-three-pairs-of-numbers-that-appear-grouped-together-in-oracle-db%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Two different queries (one hierarchical and the other with a self join) - both give the same answer (go for whichever is more performant):
SQL Fiddle
Oracle 11g R2 Schema Setup:
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
Query 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 |
Query 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 |
Damn.... You are good
– user2942951
Sep 29 '15 at 10:14
add a comment |
Two different queries (one hierarchical and the other with a self join) - both give the same answer (go for whichever is more performant):
SQL Fiddle
Oracle 11g R2 Schema Setup:
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
Query 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 |
Query 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 |
Damn.... You are good
– user2942951
Sep 29 '15 at 10:14
add a comment |
Two different queries (one hierarchical and the other with a self join) - both give the same answer (go for whichever is more performant):
SQL Fiddle
Oracle 11g R2 Schema Setup:
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
Query 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 |
Query 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 |
Two different queries (one hierarchical and the other with a self join) - both give the same answer (go for whichever is more performant):
SQL Fiddle
Oracle 11g R2 Schema Setup:
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
Query 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 |
Query 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 |
edited Sep 29 '15 at 10:29
answered Sep 29 '15 at 10:07
MT0MT0
53.5k52756
53.5k52756
Damn.... You are good
– user2942951
Sep 29 '15 at 10:14
add a comment |
Damn.... You are good
– user2942951
Sep 29 '15 at 10:14
Damn.... You are good
– user2942951
Sep 29 '15 at 10:14
Damn.... You are good
– user2942951
Sep 29 '15 at 10:14
add a comment |
This could be what you want :
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
or without using HAVING
clause
select item_id,order_id,product_id
from tableA
where order_id = product_id
order by order_id asc
limit 3
Think of it as consolidating the table into order_id's being unique... So when order_id = 1 all the product_id values are 1 and 2 so that is one occurrence of 1 and 2. When you consolidate order_id = 5 the product_id's would be 5 and 7 so that is one occurrence of 5 and 7. That make more sense?
– user2942951
Sep 29 '15 at 7:58
@user2942951 Yes, I was missunderstood your question. Why don't put desired output in question instead of put them in comment?
– RubahMalam
Sep 29 '15 at 8:04
add a comment |
This could be what you want :
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
or without using HAVING
clause
select item_id,order_id,product_id
from tableA
where order_id = product_id
order by order_id asc
limit 3
Think of it as consolidating the table into order_id's being unique... So when order_id = 1 all the product_id values are 1 and 2 so that is one occurrence of 1 and 2. When you consolidate order_id = 5 the product_id's would be 5 and 7 so that is one occurrence of 5 and 7. That make more sense?
– user2942951
Sep 29 '15 at 7:58
@user2942951 Yes, I was missunderstood your question. Why don't put desired output in question instead of put them in comment?
– RubahMalam
Sep 29 '15 at 8:04
add a comment |
This could be what you want :
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
or without using HAVING
clause
select item_id,order_id,product_id
from tableA
where order_id = product_id
order by order_id asc
limit 3
This could be what you want :
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
or without using HAVING
clause
select item_id,order_id,product_id
from tableA
where order_id = product_id
order by order_id asc
limit 3
edited Sep 29 '15 at 7:44
answered Sep 29 '15 at 7:39
RubahMalamRubahMalam
1,7872919
1,7872919
Think of it as consolidating the table into order_id's being unique... So when order_id = 1 all the product_id values are 1 and 2 so that is one occurrence of 1 and 2. When you consolidate order_id = 5 the product_id's would be 5 and 7 so that is one occurrence of 5 and 7. That make more sense?
– user2942951
Sep 29 '15 at 7:58
@user2942951 Yes, I was missunderstood your question. Why don't put desired output in question instead of put them in comment?
– RubahMalam
Sep 29 '15 at 8:04
add a comment |
Think of it as consolidating the table into order_id's being unique... So when order_id = 1 all the product_id values are 1 and 2 so that is one occurrence of 1 and 2. When you consolidate order_id = 5 the product_id's would be 5 and 7 so that is one occurrence of 5 and 7. That make more sense?
– user2942951
Sep 29 '15 at 7:58
@user2942951 Yes, I was missunderstood your question. Why don't put desired output in question instead of put them in comment?
– RubahMalam
Sep 29 '15 at 8:04
Think of it as consolidating the table into order_id's being unique... So when order_id = 1 all the product_id values are 1 and 2 so that is one occurrence of 1 and 2. When you consolidate order_id = 5 the product_id's would be 5 and 7 so that is one occurrence of 5 and 7. That make more sense?
– user2942951
Sep 29 '15 at 7:58
Think of it as consolidating the table into order_id's being unique... So when order_id = 1 all the product_id values are 1 and 2 so that is one occurrence of 1 and 2. When you consolidate order_id = 5 the product_id's would be 5 and 7 so that is one occurrence of 5 and 7. That make more sense?
– user2942951
Sep 29 '15 at 7:58
@user2942951 Yes, I was missunderstood your question. Why don't put desired output in question instead of put them in comment?
– RubahMalam
Sep 29 '15 at 8:04
@user2942951 Yes, I was missunderstood your question. Why don't put desired output in question instead of put them in comment?
– RubahMalam
Sep 29 '15 at 8:04
add a comment |
If that's what you want to achieve,you can try this:
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
the output will be order_id with the most occurences at the product_id column and its rank
This is close but not order_id and item_id pair. You want the top three occurring pairs of product_id's.
– user2942951
Sep 29 '15 at 7:54
@user2942951 I updated my answer :)
– Vance
Sep 29 '15 at 9:20
Wow you are so close!!! But two things: 1. The query you wrote returns product_id = 2 as having counted 3 of them when only 2 is given. 2. You have to find a way to only count the product_id occurrence if it shows up with the other number (for example: you count product_id occurrence even if it isn't with its other pair. Think of the pair that share order_id as a single count of that pair not as individuals) Keep trying you are on the verge!! :D
– user2942951
Sep 29 '15 at 9:54
and when product_id =1 you return 1 but it should return 2(product_id = 2 occurs 4 times but its highest pair is with 2 and theyoccur together 2times
– user2942951
Sep 29 '15 at 10:00
add a comment |
If that's what you want to achieve,you can try this:
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
the output will be order_id with the most occurences at the product_id column and its rank
This is close but not order_id and item_id pair. You want the top three occurring pairs of product_id's.
– user2942951
Sep 29 '15 at 7:54
@user2942951 I updated my answer :)
– Vance
Sep 29 '15 at 9:20
Wow you are so close!!! But two things: 1. The query you wrote returns product_id = 2 as having counted 3 of them when only 2 is given. 2. You have to find a way to only count the product_id occurrence if it shows up with the other number (for example: you count product_id occurrence even if it isn't with its other pair. Think of the pair that share order_id as a single count of that pair not as individuals) Keep trying you are on the verge!! :D
– user2942951
Sep 29 '15 at 9:54
and when product_id =1 you return 1 but it should return 2(product_id = 2 occurs 4 times but its highest pair is with 2 and theyoccur together 2times
– user2942951
Sep 29 '15 at 10:00
add a comment |
If that's what you want to achieve,you can try this:
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
the output will be order_id with the most occurences at the product_id column and its rank
If that's what you want to achieve,you can try this:
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
the output will be order_id with the most occurences at the product_id column and its rank
edited Sep 29 '15 at 9:18
answered Sep 29 '15 at 7:37
VanceVance
83749
83749
This is close but not order_id and item_id pair. You want the top three occurring pairs of product_id's.
– user2942951
Sep 29 '15 at 7:54
@user2942951 I updated my answer :)
– Vance
Sep 29 '15 at 9:20
Wow you are so close!!! But two things: 1. The query you wrote returns product_id = 2 as having counted 3 of them when only 2 is given. 2. You have to find a way to only count the product_id occurrence if it shows up with the other number (for example: you count product_id occurrence even if it isn't with its other pair. Think of the pair that share order_id as a single count of that pair not as individuals) Keep trying you are on the verge!! :D
– user2942951
Sep 29 '15 at 9:54
and when product_id =1 you return 1 but it should return 2(product_id = 2 occurs 4 times but its highest pair is with 2 and theyoccur together 2times
– user2942951
Sep 29 '15 at 10:00
add a comment |
This is close but not order_id and item_id pair. You want the top three occurring pairs of product_id's.
– user2942951
Sep 29 '15 at 7:54
@user2942951 I updated my answer :)
– Vance
Sep 29 '15 at 9:20
Wow you are so close!!! But two things: 1. The query you wrote returns product_id = 2 as having counted 3 of them when only 2 is given. 2. You have to find a way to only count the product_id occurrence if it shows up with the other number (for example: you count product_id occurrence even if it isn't with its other pair. Think of the pair that share order_id as a single count of that pair not as individuals) Keep trying you are on the verge!! :D
– user2942951
Sep 29 '15 at 9:54
and when product_id =1 you return 1 but it should return 2(product_id = 2 occurs 4 times but its highest pair is with 2 and theyoccur together 2times
– user2942951
Sep 29 '15 at 10:00
This is close but not order_id and item_id pair. You want the top three occurring pairs of product_id's.
– user2942951
Sep 29 '15 at 7:54
This is close but not order_id and item_id pair. You want the top three occurring pairs of product_id's.
– user2942951
Sep 29 '15 at 7:54
@user2942951 I updated my answer :)
– Vance
Sep 29 '15 at 9:20
@user2942951 I updated my answer :)
– Vance
Sep 29 '15 at 9:20
Wow you are so close!!! But two things: 1. The query you wrote returns product_id = 2 as having counted 3 of them when only 2 is given. 2. You have to find a way to only count the product_id occurrence if it shows up with the other number (for example: you count product_id occurrence even if it isn't with its other pair. Think of the pair that share order_id as a single count of that pair not as individuals) Keep trying you are on the verge!! :D
– user2942951
Sep 29 '15 at 9:54
Wow you are so close!!! But two things: 1. The query you wrote returns product_id = 2 as having counted 3 of them when only 2 is given. 2. You have to find a way to only count the product_id occurrence if it shows up with the other number (for example: you count product_id occurrence even if it isn't with its other pair. Think of the pair that share order_id as a single count of that pair not as individuals) Keep trying you are on the verge!! :D
– user2942951
Sep 29 '15 at 9:54
and when product_id =1 you return 1 but it should return 2(product_id = 2 occurs 4 times but its highest pair is with 2 and theyoccur together 2times
– user2942951
Sep 29 '15 at 10:00
and when product_id =1 you return 1 but it should return 2(product_id = 2 occurs 4 times but its highest pair is with 2 and theyoccur together 2times
– user2942951
Sep 29 '15 at 10:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32837892%2fselect-top-three-pairs-of-numbers-that-appear-grouped-together-in-oracle-db%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please give sample output for the above data
– Utsav
Sep 29 '15 at 7:18
2
What does "top three pairs of product_ids with same order_id " even mean? Can you show the required output for this input and explain how you got it?
– Mureinik
Sep 29 '15 at 7:20
We can see that in your requirement. What we need is your expected output with data.
top three pairs of product_ids with same order_id
doesn't make any sense– Utsav
Sep 29 '15 at 7:34
Top three pairs ascending or descending?
– RubahMalam
Sep 29 '15 at 7:36
RubahMalam doesn't matter if they are ascending or descending
– user2942951
Sep 29 '15 at 7:42