Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

Oracle SQL Help Again Please

Thread Tools
 
Search this Thread
 
Old 29 September 2005, 10:01 AM
  #1  
DJ Dunk
Moderator
Support Scoobynet!
Thread Starter
iTrader: (5)
 
DJ Dunk's Avatar
 
Join Date: Nov 2001
Location: Not all those who wander are lost
Posts: 17,863
Received 0 Likes on 0 Posts
Question Oracle SQL Help Again Please

Okay, I'm stuggling again, I think I may be over simplifying this one . . . still an Oracle n00b I'm afraid.

My current script is kind of like this:

select b.field1,
a.field2,
a.field3,
a.field4,
a.field5,
sum(decode(a.field7, 'S', 1, 0) SINGLE,
sum(decode(a.field7, 'M', 1, 0) MULT,
sum(decode(a.field7, null, 1, 0) BLANK,
from a.asset, b.company
where a.field6 = b.field6
group by b.field1, a.field2, a.field3, a.field4, a.field5
order by b.field1, a.field2, a.field3, a.field4, a.field5

Basically, I'm trying to summarise the contents of the fields with counts of the S's, M's and nulls in field 7 at the end of each unique row. My sums are way off. Any idea what I'm doing wrong ?
Old 29 September 2005, 10:43 PM
  #2  
GCollier
Scooby Regular
 
GCollier's Avatar
 
Join Date: Jun 1998
Posts: 1,198
Likes: 0
Received 0 Likes on 0 Posts
Default

I'd start by taking a look at your join condition:

a.field6 = b.field6

If there is more than one row in table b which satisfies the join for the value of a.field6, then the sums you'll end up with will be a multiple of the values you're expecting.

Gary.
Old 30 September 2005, 09:44 AM
  #3  
DJ Dunk
Moderator
Support Scoobynet!
Thread Starter
iTrader: (5)
 
DJ Dunk's Avatar
 
Join Date: Nov 2001
Location: Not all those who wander are lost
Posts: 17,863
Received 0 Likes on 0 Posts
Default

Thanks for the advice.

Bearing in mind that there are over 10 million records in the asset table, could I get away with no 'where' clause ? I don't mind if it takes a couple of hours to run.
Old 30 September 2005, 09:59 AM
  #4  
jpor
Scooby Regular
iTrader: (1)
 
jpor's Avatar
 
Join Date: Sep 2003
Posts: 3,109
Likes: 0
Received 0 Likes on 0 Posts
Default

Not sure if Oracle uses it but in Informix you can use the 'DISTINCT' clause in your SELECT to only bring back one of a kind if you have multiples.
Old 30 September 2005, 05:34 PM
  #5  
J4CKO
Scooby Regular
iTrader: (1)
 
J4CKO's Avatar
 
Join Date: Jan 2003
Posts: 19,384
Likes: 0
Received 1 Like on 1 Post
Default

Perhaps if you posted the table descriptions and a sample of the data ?
Old 30 September 2005, 06:28 PM
  #6  
IanJ
Scooby Regular
 
IanJ's Avatar
 
Join Date: Mar 2003
Posts: 157
Likes: 0
Received 0 Likes on 0 Posts
Default

Originally Posted by J4CKO
Perhaps if you posted the table descriptions and a sample of the data ?
The above would help. But perhaps you could try

select distinct b.field1,
a.field2,
a.field3,
a.field4,
a.field5,
sum(decode(a.field7, 'S', 1, 0) SINGLE,
sum(decode(a.field7, 'M', 1, 0) MULT,
sum(decode(a.field7, null, 1, 0) BLANK,
from a.asset, b.company
where a.field6 = b.field6
group by b.field1, a.field2, a.field3, a.field4, a.field5
order by b.field1, a.field2, a.field3, a.field4, a.field5

I would suggest for performance you might want to look at those order by and group by's

HTH,
Ian
Old 30 September 2005, 07:46 PM
  #7  
DJ Dunk
Moderator
Support Scoobynet!
Thread Starter
iTrader: (5)
 
DJ Dunk's Avatar
 
Join Date: Nov 2001
Location: Not all those who wander are lost
Posts: 17,863
Received 0 Likes on 0 Posts
Default

Thanks, I'll give it a go. The actual tables contain sensitive data which is why I'm a little vague, sorry.

Basically, 'field7' will have just S's, M's or nulls. Table 'b.company' has many rows returned per company name, which looks like the problem, so your distinct solution should do the trick
Old 30 September 2005, 08:22 PM
  #8  
jpor
Scooby Regular
iTrader: (1)
 
jpor's Avatar
 
Join Date: Sep 2003
Posts: 3,109
Likes: 0
Received 0 Likes on 0 Posts
Default

Originally Posted by DJ Dunk
Thanks, I'll give it a go. The actual tables contain sensitive data which is why I'm a little vague, sorry.

Basically, 'field7' will have just S's, M's or nulls. Table 'b.company' has many rows returned per company name, which looks like the problem, so your distinct solution should do the trick
No worries.
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
Scumbag
Computer & Technology Related
2
18 December 2002 01:44 PM
GaryK
Computer & Technology Related
35
08 October 2002 02:33 PM
DJ Dunk
Computer & Technology Related
4
27 August 2002 03:17 PM
DJ Dunk
Computer & Technology Related
9
09 August 2002 04:26 PM



Quick Reply: Oracle SQL Help Again Please



All times are GMT +1. The time now is 05:07 AM.