[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

query to produce 10 rows, half of col= A and other half of col=B

Posted on 2011-03-24
13
Medium Priority
?
560 Views
Last Modified: 2013-12-07
I have a table my_table
I want a query which selects 5 rows where col='A' and 5 rows where col='B'
so i should have a total of 10 rows.

but the following query does not work as intended.

select col
from my_table
where col= 'A'
and rownum<=5
union
select col
from my_table
where col= 'B'
and rownum<=5

0
Comment
Question by:gram77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
13 Comments
 
LVL 1

Expert Comment

by:data_n_poker
ID: 35208225
Try using union all instead of just union
0
 

Author Comment

by:gram77
ID: 35208254
union all and union work same.
also i only see col=A rows, col=B rows do not append
0
 
LVL 1

Expert Comment

by:data_n_poker
ID: 35208268
Try this:

Select *
From 
(select col
from my_table
where col= 'A'
and rownum<=5) colA,
(select col
from my_table
where col= 'B'
and rownum<=5) colB
ORDER BY Col

Open in new window

0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 74

Expert Comment

by:sdstuber
ID: 35208343
you can also try this



SELECT col
  FROM (SELECT col, ROW_NUMBER() OVER (PARTITION BY col ORDER BY 1) rn
          FROM my_table
         WHERE col IN ('A', 'B'))
 WHERE rn <= 5
0
 

Author Comment

by:gram77
ID: 35208357
above query returns no rows.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35208389
are you sure you're explaining your setup correctly?

the union all suggestion and my row_number suggestion both work when I tested them.

if they don't work for you, please post structure and sample data that illustrates the problem
0
 
LVL 1

Accepted Solution

by:
data_n_poker earned 2000 total points
ID: 35208415
I have create a table in a oracle 11 database.  The table has one column, with A and B entries, using this query:
select col
from my_table
where col= 'A'
and rownum <=5
union all
select col
from my_table
where col='B'
and rownum <=5

Open in new window

I got these results
Col
A
A
A
A
A
B
B
B
B
B

Open in new window

0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35208589
gram77,

please explain your choice.
the accepted answer does not produce the requested results of 10 rows.

It will produce a cartesian product based on the results of two subqueries.
Assuming each subquery returns 5 rows, the result will be 25 rows for the entire query
Also, it won't return a single column it will return 2 columns
and lastly,  the order by clause in that post is syntactically incorrect because "col" is ambiguous
0
 
LVL 1

Expert Comment

by:data_n_poker
ID: 35208605
I have the same question, as i posted that before i tried it, and it is very much incorrect(syntactically and based on what is asked for in the question).  Thought the union all and sdstuber's query provided the exact results that were requested.
0
 

Author Comment

by:gram77
ID: 35220620
that is an error on my part.
by mistake i accepted the wrong solution,
the correct one is
ID: 35208415
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35231708
sure,  UNION ALL was the very first post and from the same author
http:#a35208415 repeats it but with an example.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question