dbTalk Databases Forums  

newbie : writing an efficient query

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


Discuss newbie : writing an efficient query in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
dn.perl (AT) gmail (DOT) com
 
Posts: n/a

Default newbie : writing an efficient query - 08-03-2006 , 06:49 PM







I have a table t1 : time1 unique, qty
Entries : (1, 10) (2, 20) (3,30) (32,20)

I want to find out the time1 values where qty is the same.

select a1.time1, a2.time1
from t1 a1, t1 a2
where a1.qty = a2.qty and a1.time1 <> a2.time1 -- and a1.time1 >
a2.time1

produces output :
-------
32 2
2 32

But I want just one line in the output. Uncommenting the clause
achieves the effect but somehow it seems a contrived way of
getting the desired result. Besides if unique key is on (date + time),
it would make the task more difficult. I think a more logical way
would help me write the query efficiently whether the unique
key was on a single field or multiple fields.

Please advise about a logical way of writing the query.


Reply With Quote
  #2  
Old   
DA Morgan
 
Posts: n/a

Default Re: newbie : writing an efficient query - 08-03-2006 , 06:57 PM






dn.perl (AT) gmail (DOT) com wrote:
Quote:
I have a table t1 : time1 unique, qty
Entries : (1, 10) (2, 20) (3,30) (32,20)

I want to find out the time1 values where qty is the same.

select a1.time1, a2.time1
from t1 a1, t1 a2
where a1.qty = a2.qty and a1.time1 <> a2.time1 -- and a1.time1
a2.time1

produces output :
-------
32 2
2 32

But I want just one line in the output. Uncommenting the clause
achieves the effect but somehow it seems a contrived way of
getting the desired result. Besides if unique key is on (date + time),
it would make the task more difficult. I think a more logical way
would help me write the query efficiently whether the unique
key was on a single field or multiple fields.

Please advise about a logical way of writing the query.
Look at using DECODE or CASE to perform crosstabulation.

Demo in Morgan's Library at www.psoug.org under DECODE.
--
Daniel A. Morgan
University of Washington
damorgan@x.washington.edu
(replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org


Reply With Quote
  #3  
Old   
Ed Prochak
 
Posts: n/a

Default Re: newbie : writing an efficient query - 08-04-2006 , 01:35 PM




dn.perl (AT) gmail (DOT) com wrote:
Quote:
I have a table t1 : time1 unique, qty
Entries : (1, 10) (2, 20) (3,30) (32,20)

I want to find out the time1 values where qty is the same.

select a1.time1, a2.time1
from t1 a1, t1 a2
where a1.qty = a2.qty and a1.time1 <> a2.time1 -- and a1.time1
a2.time1

produces output :
-------
32 2
2 32

But I want just one line in the output. Uncommenting the clause
achieves the effect but somehow it seems a contrived way of
getting the desired result. Besides if unique key is on (date + time),
it would make the task more difficult. I think a more logical way
would help me write the query efficiently whether the unique
key was on a single field or multiple fields.

Please advise about a logical way of writing the query.
It is not contrived. it enforces the rule that time runs forward. It
gets you the right result
so put it back in and remove the <> condition like this:
Quote:
select a1.time1, a2.time1
from t1 a1, t1 a2
where a1.qty = a2.qty and a1.time1 > a2.time1 ;
HTH,
Ed



Reply With Quote
  #4  
Old   
dn.perl (AT) gmail (DOT) com
 
Posts: n/a

Default Re: newbie : writing an efficient query - 08-04-2006 , 05:38 PM



One more point which I have been curious about .
Why doesn't 'distinct' work on composite fields?

Table t1 : a1 number, a2 number.
Entries : (1,11) (2,21), (2,22) (2,22) (2,23) (2,23)
(3,33) (3, 33)

select distinct(a1, a2) from t1 ... does not work.
How do I write that query?


Reply With Quote
  #5  
Old   
Eddie Awad
 
Posts: n/a

Default Re: newbie : writing an efficient query - 08-04-2006 , 06:28 PM




dn.perl (AT) gmail (DOT) com wrote:
Quote:
One more point which I have been curious about .
Why doesn't 'distinct' work on composite fields?

Table t1 : a1 number, a2 number.
Entries : (1,11) (2,21), (2,22) (2,22) (2,23) (2,23)
(3,33) (3, 33)

select distinct(a1, a2) from t1 ... does not work.
How do I write that query?
Remove the parentheses:

select distinct a1, a2 from t1

Documentation: http://snipurl.com/uf81



Reply With Quote
  #6  
Old   
dn.perl (AT) gmail (DOT) com
 
Posts: n/a

Default Re: newbie : writing an efficient query - 08-04-2006 , 06:36 PM




Quote:
select distinct(a1, a2) from t1 ... does not work.
How do I write that query?

Jeez, I tried out the query without parantheses, and it worked!!
This parantheses thing is a bit confusing.
create table t1(a1 number, a2 number)
create table t2(a1 number, a2 number)

insert into t2 select (a1, a2) from t1 : does not work.
Why? Does (a1, a2) return only one value (prob a1),
when the insert stmt expects 2 values?

And why does 'primary key' construct require parantheses
around the field even when the key is on just one field?
create table t1(aa number, primary key aa) : does not work.

On second thought the reason is not difficult to figure out
but sometimes it does get confusing as to why a simple
command is not working. Particularly the syntax requiring
absence of parantheses in : select distinct(aa, bb) from t1
is quite confusing.



Reply With Quote
  #7  
Old   
DA Morgan
 
Posts: n/a

Default Re: newbie : writing an efficient query - 08-04-2006 , 06:44 PM



dn.perl (AT) gmail (DOT) com wrote:
Quote:
One more point which I have been curious about .
Why doesn't 'distinct' work on composite fields?

Table t1 : a1 number, a2 number.
Entries : (1,11) (2,21), (2,22) (2,22) (2,23) (2,23)
(3,33) (3, 33)

select distinct(a1, a2) from t1 ... does not work.
How do I write that query?
Do yo see a problem here?

SQL> select a1, a2 from t1;

A1 A2
---------- ----------
1 11
2 21
2 22
2 22
2 23
3 33
3 33

7 rows selected.

SQL> select distinct a1, a2 from t1;

A1 A2
---------- ----------
3 33
2 21
2 23
1 11
2 22

I don't.
--
Daniel A. Morgan
University of Washington
damorgan@x.washington.edu
(replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org


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.