dbTalk Databases Forums  

column filter

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss column filter in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Carl Forsman
 
Posts: n/a

Default column filter - 02-27-2009 , 01:30 PM






The following query will return 3 word in 3 "TABLE COLUMN", How can I
modify the query to return "NO 3 words are the same"?

the following Table will have duplicated as both row has the same 3
words, I only need "a, young, woman" is enough. I do not need "a,
woman, young"
=======================
a, young, woman <-- both row has same 3 word, I only need 1 row
a, woman, young
=======================
(word1)(word2) (word3)
a, young, woman
a, woman, young
a, fish, dive
.....

=======================
SELECT DISTINCT A.word As word1, B.word AS word2, C.word AS word3

FROM Data AS A

JOIN Data AS B

ON A.document = B.document

AND A.position < B.position

AND ABS(A.position - B.position) < 3

LEFT JOIN Data AS C

ON A.document = C.document

AND A.position <> C.position

AND C.position <> B.position

AND (ABS(A.position - C.position) < 3

OR ABS(B.position - C.position) < 3) order by word1;

Reply With Quote
  #2  
Old   
Michel Cadot
 
Posts: n/a

Default Re: column filter - 02-27-2009 , 01:37 PM







"Carl Forsman" <fatwallet951 (AT) yahoo (DOT) com> a écrit dans le message de news: 2dfgq4lnlp186te0dcb3hoduur8fu7va2b (AT) 4ax (DOT) com...
Quote:
The following query will return 3 word in 3 "TABLE COLUMN", How can I
modify the query to return "NO 3 words are the same"?

the following Table will have duplicated as both row has the same 3
words, I only need "a, young, woman" is enough. I do not need "a,
woman, young"
=======================
a, young, woman <-- both row has same 3 word, I only need 1 row
a, woman, young
=======================
(word1)(word2) (word3)
a, young, woman
a, woman, young
a, fish, dive
....

=======================
SELECT DISTINCT A.word As word1, B.word AS word2, C.word AS word3

FROM Data AS A

JOIN Data AS B

ON A.document = B.document

AND A.position < B.position

AND ABS(A.position - B.position) < 3

LEFT JOIN Data AS C

ON A.document = C.document

AND A.position <> C.position

AND C.position <> B.position

AND (ABS(A.position - C.position) < 3

OR ABS(B.position - C.position) < 3) order by word1;
Use greatest and least functions.

Regards
Michel




Reply With Quote
  #3  
Old   
Carl Forsman
 
Posts: n/a

Default Re: column filter - 02-27-2009 , 02:12 PM



On Fri, 27 Feb 2009 20:37:54 +0100, "Michel Cadot"
<micadot{at}altern{dot}org> wrote:

Quote:
"Carl Forsman" <fatwallet951 (AT) yahoo (DOT) com> a ?crit dans le message de news: 2dfgq4lnlp186te0dcb3hoduur8fu7va2b (AT) 4ax (DOT) com...
| The following query will return 3 word in 3 "TABLE COLUMN", How can I
| modify the query to return "NO 3 words are the same"?
|
| the following Table will have duplicated as both row has the same 3
| words, I only need "a, young, woman" is enough. I do not need "a,
| woman, young"
| =======================
| a, young, woman <-- both row has same 3 word, I only need 1 row
| a, woman, young
| =======================
| (word1)(word2) (word3)
| a, young, woman
| a, woman, young
| a, fish, dive
| ....
|
| =======================
| SELECT DISTINCT A.word As word1, B.word AS word2, C.word AS word3
|
| FROM Data AS A
|
| JOIN Data AS B
|
| ON A.document = B.document
|
| AND A.position < B.position
|
| AND ABS(A.position - B.position) < 3
|
| LEFT JOIN Data AS C
|
| ON A.document = C.document
|
| AND A.position <> C.position
|
| AND C.position <> B.position
|
| AND (ABS(A.position - C.position) < 3
|
| OR ABS(B.position - C.position) < 3) order by word1;

Use greatest and least functions.

Regards
Michel

but the query returns 2 row has the same word

(word1)(word2) (word3)
a, young, woman
a, woman, young
a, fish, dive

===================
since "a,young,woman" and "a,woman,young" are duplicated
I only want query return
(word1)(word2) (word3)
a, young, woman
a, fish, dive

-OR-

(word1)(word2) (word3)
a, woman, young
a, fish, dive


Reply With Quote
  #4  
Old   
Michel Cadot
 
Posts: n/a

Default Re: column filter - 02-27-2009 , 02:32 PM




"Carl Forsman" <fatwallet951 (AT) yahoo (DOT) com> a écrit dans le message de news: v8igq45d4jo0q81lvr0ja14mv4etd45e9a (AT) 4ax (DOT) com...
Quote:
On Fri, 27 Feb 2009 20:37:54 +0100, "Michel Cadot"
micadot{at}altern{dot}org> wrote:


"Carl Forsman" <fatwallet951 (AT) yahoo (DOT) com> a ?crit dans le message de news: 2dfgq4lnlp186te0dcb3hoduur8fu7va2b (AT) 4ax (DOT) com...
| The following query will return 3 word in 3 "TABLE COLUMN", How can I
| modify the query to return "NO 3 words are the same"?
|
| the following Table will have duplicated as both row has the same 3
| words, I only need "a, young, woman" is enough. I do not need "a,
| woman, young"
| =======================
| a, young, woman <-- both row has same 3 word, I only need 1 row
| a, woman, young
| =======================
| (word1)(word2) (word3)
| a, young, woman
| a, woman, young
| a, fish, dive
| ....
|
| =======================
| SELECT DISTINCT A.word As word1, B.word AS word2, C.word AS word3
|
| FROM Data AS A
|
| JOIN Data AS B
|
| ON A.document = B.document
|
| AND A.position < B.position
|
| AND ABS(A.position - B.position) < 3
|
| LEFT JOIN Data AS C
|
| ON A.document = C.document
|
| AND A.position <> C.position
|
| AND C.position <> B.position
|
| AND (ABS(A.position - C.position) < 3
|
| OR ABS(B.position - C.position) < 3) order by word1;

Use greatest and least functions.

Regards
Michel


but the query returns 2 row has the same word

(word1)(word2) (word3)
a, young, woman
a, woman, young
a, fish, dive

===================
since "a,young,woman" and "a,woman,young" are duplicated
I only want query return
(word1)(word2) (word3)
a, young, woman
a, fish, dive

-OR-

(word1)(word2) (word3)
a, woman, young
a, fish, dive
Yes and try what I have said.
And if you want an example you MUST post a test case: create table and insert statements.

Regards
Michel




Reply With Quote
  #5  
Old   
Michael Austin
 
Posts: n/a

Default Re: column filter - 02-27-2009 , 04:54 PM



Carl Forsman wrote:
Quote:
The following query will return 3 word in 3 "TABLE COLUMN", How can I
modify the query to return "NO 3 words are the same"?

the following Table will have duplicated as both row has the same 3
words, I only need "a, young, woman" is enough. I do not need "a,
woman, young"
=======================
a, young, woman <-- both row has same 3 word, I only need 1 row
a, woman, young
=======================
(word1)(word2) (word3)
a, young, woman
a, woman, young
a, fish, dive
....

=======================
SELECT DISTINCT A.word As word1, B.word AS word2, C.word AS word3

FROM Data AS A

JOIN Data AS B

ON A.document = B.document

AND A.position < B.position

AND ABS(A.position - B.position) < 3

LEFT JOIN Data AS C

ON A.document = C.document

AND A.position <> C.position

AND C.position <> B.position

AND (ABS(A.position - C.position) < 3

OR ABS(B.position - C.position) < 3) order by word1;

Don;t multi-post use cross-post instead. same question is in the MySQL
NG - are you using Oracle or MySQL - your answer could be different.

sounds like homework to me...


Reply With Quote
  #6  
Old   
Maxim Demenko
 
Posts: n/a

Default Re: column filter - 02-27-2009 , 10:53 PM



Carl Forsman schrieb:
Quote:
On Fri, 27 Feb 2009 20:37:54 +0100, "Michel Cadot"
micadot{at}altern{dot}org> wrote:

"Carl Forsman" <fatwallet951 (AT) yahoo (DOT) com> a ?crit dans le message de news: 2dfgq4lnlp186te0dcb3hoduur8fu7va2b (AT) 4ax (DOT) com...

Quote:
Use greatest and least functions.

Regards
Michel


but the query returns 2 row has the same word

(word1)(word2) (word3)
a, young, woman
a, woman, young
a, fish, dive

===================
since "a,young,woman" and "a,woman,young" are duplicated
I only want query return
(word1)(word2) (word3)
a, young, woman
a, fish, dive

-OR-

(word1)(word2) (word3)
a, woman, young
a, fish, dive
Your rows are not duplicated. They could be it, if you were able to
rearrange words within a row in some order. And Michel already told you,
how can it be done...

SQL> with t as (
2 select 'a' word1,'young' word2,'woman' word3 from dual union all
3 select 'a','woman','young' from dual union all
4 select 'young','woman','a' from dual union all
5 select 'a','fish','dive' from dual union all
6 select 'young','woman','b' from dual union all
7 select 'woman', 'b','young' from dual union all
8 select 'b','fish','dive' from dual
9 )
10 -- End test data
11 select word1, word2, word3, number_of_sets
12 from (
13 select word1,
14 word2,
15 word3,
16 least(word1, word2, word3) first,
17 least(greatest(word1, word2),
18 greatest(word1, word3),
19 greatest(word2, word3)) second,
20 greatest(word1, word2, word3) third,
21 row_number() over(partition by
22 least(word1, word2, word3),
23 least(greatest(word1, word2),
24 greatest(word1, word3),
25 greatest(word2, word3)),
26 greatest(word1, word2, word3) order
by null) sentence,
27 count(*) over(partition by
28 least(word1, word2, word3),
29 least(greatest(word1, word2),
30 greatest(word1, word3),
31 greatest(word2, word3)),
32 greatest(word1, word2, word3) order
by null) number_of_sets
33 from t)
34 where sentence = 1
35 ;

WORD1 WORD2 WORD3 NUMBER_OF_SETS
----- ----- ----- --------------
a fish dive 1
a woman young 3
b fish dive 1
young woman b 2

Best regards

Maxim


Reply With Quote
  #7  
Old   
Michael Austin
 
Posts: n/a

Default Re: column filter - 02-27-2009 , 11:06 PM



Maxim Demenko wrote:
Quote:
Carl Forsman schrieb:
On Fri, 27 Feb 2009 20:37:54 +0100, "Michel Cadot"
micadot{at}altern{dot}org> wrote:

"Carl Forsman" <fatwallet951 (AT) yahoo (DOT) com> a ?crit dans le message de
news: 2dfgq4lnlp186te0dcb3hoduur8fu7va2b (AT) 4ax (DOT) com...


Use greatest and least functions.

Regards
Michel


but the query returns 2 row has the same word

(word1)(word2) (word3)
a, young, woman
a, woman, young
a, fish, dive

===================
since "a,young,woman" and "a,woman,young" are duplicated
I only want query return
(word1)(word2) (word3)
a, young, woman
a, fish, dive

-OR-

(word1)(word2) (word3)
a, woman, young
a, fish, dive

Your rows are not duplicated. They could be it, if you were able to
rearrange words within a row in some order. And Michel already told you,
how can it be done...

SQL> with t as (
2 select 'a' word1,'young' word2,'woman' word3 from dual union all
3 select 'a','woman','young' from dual union all
4 select 'young','woman','a' from dual union all
5 select 'a','fish','dive' from dual union all
6 select 'young','woman','b' from dual union all
7 select 'woman', 'b','young' from dual union all
8 select 'b','fish','dive' from dual
9 )
10 -- End test data
11 select word1, word2, word3, number_of_sets
12 from (
13 select word1,
14 word2,
15 word3,
16 least(word1, word2, word3) first,
17 least(greatest(word1, word2),
18 greatest(word1, word3),
19 greatest(word2, word3)) second,
20 greatest(word1, word2, word3) third,
21 row_number() over(partition by
22 least(word1, word2, word3),
23 least(greatest(word1, word2),
24 greatest(word1, word3),
25 greatest(word2, word3)),
26 greatest(word1, word2, word3) order by
null) sentence,
27 count(*) over(partition by
28 least(word1, word2, word3),
29 least(greatest(word1, word2),
30 greatest(word1, word3),
31 greatest(word2, word3)),
32 greatest(word1, word2, word3) order by
null) number_of_sets
33 from t)
34 where sentence = 1
35 ;

WORD1 WORD2 WORD3 NUMBER_OF_SETS
----- ----- ----- --------------
a fish dive 1
a woman young 3
b fish dive 1
young woman b 2

Best regards

Maxim

Boy, I would hate to see that run over a table with 1M+ of rows...


Reply With Quote
  #8  
Old   
Maxim Demenko
 
Posts: n/a

Default Re: column filter - 02-28-2009 , 04:29 AM



Michael Austin schrieb:
Quote:
Maxim Demenko wrote:
Carl Forsman schrieb:
On Fri, 27 Feb 2009 20:37:54 +0100, "Michel Cadot"
micadot{at}altern{dot}org> wrote:

"Carl Forsman" <fatwallet951 (AT) yahoo (DOT) com> a ?crit dans le message de
news: 2dfgq4lnlp186te0dcb3hoduur8fu7va2b (AT) 4ax (DOT) com...


Use greatest and least functions.

Regards
Michel


but the query returns 2 row has the same word

(word1)(word2) (word3)
a, young, woman
a, woman, young
a, fish, dive

===================
since "a,young,woman" and "a,woman,young" are duplicated
I only want query return
(word1)(word2) (word3)
a, young, woman
a, fish, dive

-OR-

(word1)(word2) (word3)
a, woman, young
a, fish, dive

Your rows are not duplicated. They could be it, if you were able to
rearrange words within a row in some order. And Michel already told
you, how can it be done...

SQL> with t as (
2 select 'a' word1,'young' word2,'woman' word3 from dual union all
3 select 'a','woman','young' from dual union all
4 select 'young','woman','a' from dual union all
5 select 'a','fish','dive' from dual union all
6 select 'young','woman','b' from dual union all
7 select 'woman', 'b','young' from dual union all
8 select 'b','fish','dive' from dual
9 )
10 -- End test data
11 select word1, word2, word3, number_of_sets
12 from (
13 select word1,
14 word2,
15 word3,
16 least(word1, word2, word3) first,
17 least(greatest(word1, word2),
18 greatest(word1, word3),
19 greatest(word2, word3)) second,
20 greatest(word1, word2, word3) third,
21 row_number() over(partition by
22 least(word1, word2, word3),
23 least(greatest(word1, word2),
24 greatest(word1, word3),
25 greatest(word2, word3)),
26 greatest(word1, word2, word3) order
by null) sentence,
27 count(*) over(partition by
28 least(word1, word2, word3),
29 least(greatest(word1, word2),
30 greatest(word1, word3),
31 greatest(word2, word3)),
32 greatest(word1, word2, word3) order
by null) number_of_sets
33 from t)
34 where sentence = 1
35 ;

WORD1 WORD2 WORD3 NUMBER_OF_SETS
----- ----- ----- --------------
a fish dive 1
a woman young 3
b fish dive 1
young woman b 2

Best regards

Maxim


Boy, I would hate to see that run over a table with 1M+ of rows...
I feel with you ...
Though, this particular example with 1M+ rows and average rowlength of
11 Byte would mean nearly 10Mb data to read, you wouldn't probably ever
notice a trace of load ... Even worse, one can consider to run it on
MySQL (ISAM) - may be a tick faster than Oracle :-)

Seriously, though this may be interesting puzzle to solve in sql, it
doesn't look like properly designed structure for relational database.

Best regards

Maxim


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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.