Unix Technical Forum

Select some record

This is a discussion on Select some record within the Oracle Database forums, part of the Database Server Software category; --> Dear All, I have problem, Please help. There is table called t: with t as ( select 1 Col1, ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-26-2008, 07:49 AM
nova1427@gmail.com
 
Posts: n/a
Default Select some record

Dear All,

I have problem, Please help.


There is table called t:

with t as (
select 1 Col1, 1 Col2, 1 Col3 from dual union all
select 1, 1, 2 from dual union all
select 1, 2, 1 from dual union all
select 1, 2, 2 from dual union all
select 2, 1, 1 from dual union all
select 2, 1, 2 from dual union all
select 2, 2, 1 from dual union all
select 2, 2, 2 from dual union all
select 2, 1, 1 from dual union all
select 2, 1, 2 from dual union all
select 2, 2, 1 from dual union all
select 2, 2, 2 from dual union all
select NULL, NULL, NULL from dual union all
select 3, 3, NULL from dual union all
select 4, NULL, NULL from dual union all
select 5, 5, NULL from dual union all
select 5, 6, NULL from dual union all
select 6, 5, NULL from dual union all
select 6, 6, NULL from dual
)


Table ( t )
Col1 Col2 Col3
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
2 2 2
2 1 1
2 1 2
2 2 1
2 2 2

3 3
4
5 5
5 6
6 5
6 6


Question: how can do like this result?

The result should be like this:

Table ( result )
Col1 Col2 Col3
1 1 1
2 2 2

3 3
4
5 5
6 6


Note: Make sure the empty cells are included.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-26-2008, 07:49 AM
nova1427@gmail.com
 
Posts: n/a
Default Re: Select some record

On Dec 29, 12:24*pm, nova1...@gmail.com wrote:
> Dear All,
>
> I have problem, Please help.
>
> There is table called t:
>
> with t as (
> * * * * select 1 Col1, 1 Col2, 1 Col3 from dual union all
> * * * * select 1, 1, 2 from dual union all
> * * * * select 1, 2, 1 from dual union all
> * * * * select 1, 2, 2 from dual union all
> * * * * select 2, 1, 1 from dual union all
> * * * * select 2, 1, 2 from dual union all
> * * * * select 2, 2, 1 from dual union all
> * * * * select 2, 2, 2 from dual union all
> * * * * select 2, 1, 1 from dual union all
> * * * * select 2, 1, 2 from dual union all
> * * * * select 2, 2, 1 from dual union all
> * * * * select 2, 2, 2 from dual union all
> * * * * select NULL, NULL, NULL from dual union all
> * * * * select 3, 3, NULL from dual union all
> * * * * select 4, NULL, NULL from dual union all
> * * * * select 5, 5, NULL from dual union all
> * * * * select 5, 6, NULL from dual union all
> * * * * select 6, 5, NULL from dual union all
> * * * * select 6, 6, NULL from dual
> )
>
> Table ( *t *)
> Col1 * *Col2 * *Col3
> 1 * * * 1 * * * 1
> 1 * * * 1 * * * 2
> 1 * * * 2 * * * 1
> 1 * * * 2 * * * 2
> 2 * * * 1 * * * 1
> 2 * * * 1 * * * 2
> 2 * * * 2 * * * 1
> 2 * * * 2 * * * 2
> 2 * * * 1 * * * 1
> 2 * * * 1 * * * 2
> 2 * * * 2 * * * 1
> 2 * * * 2 * * * 2
>
> 3 * * * 3
> 4
> 5 * * * 5
> 5 * * * 6
> 6 * * * 5
> 6 * * * 6
>
> Question: how can do like this result?
>
> The result should be like this:
>
> Table ( *result *)
> Col1 * *Col2 * *Col3
> 1 * * * 1 * * * 1
> 2 * * * 2 * * * 2
>
> 3 * * * 3
> 4
> 5 * * * 5
> 6 * * * 6
>
> Note: Make sure the empty cells are included.


Sorry, there is mistake in table t

with t as (
select 1 Col1, 1 Col2, 1 Col3 from dual union all
select 1, 1, 2 from dual union all
select 1, 2, 1 from dual union all
select 1, 2, 2 from dual union all
select 2, 1, 1 from dual union all
select 2, 1, 2 from dual union all
select 2, 2, 1 from dual union all
select 2, 2, 2 from dual union all
select NULL, NULL, NULL from dual union all
select 3, 3, NULL from dual union all
select 4, NULL, NULL from dual union all
select 5, 5, NULL from dual union all
select 5, 6, NULL from dual union all
select 6, 5, NULL from dual union all
select 6, 6, NULL from dual
)


Table ( t )
COL1 COL2 COL3
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
2 2 2

3 3
4
5 5
5 6
6 5
6 6
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-26-2008, 07:49 AM
Mark D Powell
 
Posts: n/a
Default Re: Select some record

On Dec 29, 4:54*am, nova1...@gmail.com wrote:
> On Dec 29, 12:24*pm, nova1...@gmail.com wrote:
>
>
>
>
>
> > Dear All,

>
> > I have problem, Please help.

>
> > There is table called t:

>
> > with t as (
> > * * * * select 1 Col1, 1 Col2, 1 Col3 from dual union all
> > * * * * select 1, 1, 2 from dual union all
> > * * * * select 1, 2, 1 from dual union all
> > * * * * select 1, 2, 2 from dual union all
> > * * * * select 2, 1, 1 from dual union all
> > * * * * select 2, 1, 2 from dual union all
> > * * * * select 2, 2, 1 from dual union all
> > * * * * select 2, 2, 2 from dual union all
> > * * * * select 2, 1, 1 from dual union all
> > * * * * select 2, 1, 2 from dual union all
> > * * * * select 2, 2, 1 from dual union all
> > * * * * select 2, 2, 2 from dual union all
> > * * * * select NULL, NULL, NULL from dual union all
> > * * * * select 3, 3, NULL from dual union all
> > * * * * select 4, NULL, NULL from dual union all
> > * * * * select 5, 5, NULL from dual union all
> > * * * * select 5, 6, NULL from dual union all
> > * * * * select 6, 5, NULL from dual union all
> > * * * * select 6, 6, NULL from dual
> > )

>
> > Table ( *t *)
> > Col1 * *Col2 * *Col3
> > 1 * * * 1 * * * 1
> > 1 * * * 1 * * * 2
> > 1 * * * 2 * * * 1
> > 1 * * * 2 * * * 2
> > 2 * * * 1 * * * 1
> > 2 * * * 1 * * * 2
> > 2 * * * 2 * * * 1
> > 2 * * * 2 * * * 2
> > 2 * * * 1 * * * 1
> > 2 * * * 1 * * * 2
> > 2 * * * 2 * * * 1
> > 2 * * * 2 * * * 2

>
> > 3 * * * 3
> > 4
> > 5 * * * 5
> > 5 * * * 6
> > 6 * * * 5
> > 6 * * * 6

>
> > Question: how can do like this result?

>
> > The result should be like this:

>
> > Table ( *result *)
> > Col1 * *Col2 * *Col3
> > 1 * * * 1 * * * 1
> > 2 * * * 2 * * * 2

>
> > 3 * * * 3
> > 4
> > 5 * * * 5
> > 6 * * * 6

>
> > Note: Make sure the empty cells are included.

>
> Sorry, there is mistake in table t
>
> with t as (
> * * * * select 1 Col1, 1 Col2, 1 Col3 from dual union all
> * * * * select 1, 1, 2 from dual union all
> * * * * select 1, 2, 1 from dual union all
> * * * * select 1, 2, 2 from dual union all
> * * * * select 2, 1, 1 from dual union all
> * * * * select 2, 1, 2 from dual union all
> * * * * select 2, 2, 1 from dual union all
> * * * * select 2, 2, 2 from dual union all
> * * * * select NULL, NULL, NULL from dual union all
> * * * * select 3, 3, NULL from dual union all
> * * * * select 4, NULL, NULL from dual union all
> * * * * select 5, 5, NULL from dual union all
> * * * * select 5, 6, NULL from dual union all
> * * * * select 6, 5, NULL from dual union all
> * * * * select 6, 6, NULL from dual
> )
>
> Table ( *t *)
> COL1 * *COL2 * *COL3
> 1 * * * 1 * * * 1
> 1 * * * 1 * * * 2
> 1 * * * 2 * * * 1
> 1 * * * 2 * * * 2
> 2 * * * 1 * * * 1
> 2 * * * 1 * * * 2
> 2 * * * 2 * * * 1
> 2 * * * 2 * * * 2
>
> 3 * * * 3
> 4
> 5 * * * 5
> 5 * * * 6
> 6 * * * 5
> 6 * * * 6- Hide quoted text -
>
> - Show quoted text -


I for one am not particularly good at recognizing requirements from
just a listing of the desired result set. It might lead someone to
post if you listed the requirements in words to go along with the
example.

-- Mark D Powell --
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-26-2008, 07:49 AM
Charles Hooper
 
Posts: n/a
Default Re: Select some record

On Dec 29, 4:24*am, nova1...@gmail.com wrote:
> Dear All,
>
> I have problem, Please help.
>
> There is table called t:
>
> with t as (
> * * * * select 1 Col1, 1 Col2, 1 Col3 from dual union all
> * * * * select 1, 1, 2 from dual union all
> * * * * select 1, 2, 1 from dual union all
> * * * * select 1, 2, 2 from dual union all
> * * * * select 2, 1, 1 from dual union all
> * * * * select 2, 1, 2 from dual union all
> * * * * select 2, 2, 1 from dual union all
> * * * * select 2, 2, 2 from dual union all
> * * * * select 2, 1, 1 from dual union all
> * * * * select 2, 1, 2 from dual union all
> * * * * select 2, 2, 1 from dual union all
> * * * * select 2, 2, 2 from dual union all
> * * * * select NULL, NULL, NULL from dual union all
> * * * * select 3, 3, NULL from dual union all
> * * * * select 4, NULL, NULL from dual union all
> * * * * select 5, 5, NULL from dual union all
> * * * * select 5, 6, NULL from dual union all
> * * * * select 6, 5, NULL from dual union all
> * * * * select 6, 6, NULL from dual
> )
>
> Table ( *t *)
> Col1 * *Col2 * *Col3
> 1 * * * 1 * * * 1
> 1 * * * 1 * * * 2
> 1 * * * 2 * * * 1
> 1 * * * 2 * * * 2
> 2 * * * 1 * * * 1
> 2 * * * 1 * * * 2
> 2 * * * 2 * * * 1
> 2 * * * 2 * * * 2
> 2 * * * 1 * * * 1
> 2 * * * 1 * * * 2
> 2 * * * 2 * * * 1
> 2 * * * 2 * * * 2
>
> 3 * * * 3
> 4
> 5 * * * 5
> 5 * * * 6
> 6 * * * 5
> 6 * * * 6
>
> Question: how can do like this result?
>
> The result should be like this:
>
> Table ( *result *)
> Col1 * *Col2 * *Col3
> 1 * * * 1 * * * 1
> 2 * * * 2 * * * 2
>
> 3 * * * 3
> 4
> 5 * * * 5
> 6 * * * 6
>
> Note: Make sure the empty cells are included.


Since there is no response to Mark's request for clarification, it
appears to me that the OP would like to include those rows where the
value in COL1 is the same as the values in COL2 and COL3, and allowing
nulls in the last column, the last two columns, or all three columns.
If we translate NULL values to an unlikely value, such as -999999,
using NVL, a simple WHERE clause should be sufficient. For example:
with t as (
select 1 Col1, 1 Col2, 1 Col3 from dual union all
select 1, 1, 2 from dual union all
select 1, 2, 1 from dual union all
select 1, 2, 2 from dual union all
select 2, 1, 1 from dual union all
select 2, 1, 2 from dual union all
select 2, 2, 1 from dual union all
select 2, 2, 2 from dual union all
select NULL, NULL, NULL from dual union all
select 3, 3, NULL from dual union all
select 4, NULL, NULL from dual union all
select 5, 5, NULL from dual union all
select 5, 6, NULL from dual union all
select 6, 5, NULL from dual union all
select 6, 6, NULL from dual
)
SELECT
COL1,
COL2,
COL3
FROM
T
WHERE
NVL(COL1,-999999)=NVL(COL2,NVL(COL1,-999999))
AND
NVL(COL2,NVL(COL1,-999999))=NVL(COL3,NVL(COL2,NVL(COL1,-999999)));

COL1 COL2 COL3
---------- ---------- ----------
1 1 1
2 2 2

3 3
4
5 5
6 6

7 rows selected.

Charles Hooper
IT Manager/Oracle DBA
K&M Machine-Fabricating, Inc.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:35 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com