Oracle SQL Help Again Please
#1
Moderator
Thread Starter
iTrader: (5)
Join Date: Nov 2001
Location: Not all those who wander are lost
Posts: 17,863
Received 0 Likes
on
0 Posts
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 ?
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 ?
#2
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.
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.
#3
Moderator
Thread Starter
iTrader: (5)
Join Date: Nov 2001
Location: Not all those who wander are lost
Posts: 17,863
Received 0 Likes
on
0 Posts
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.
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.
#6
Originally Posted by J4CKO
Perhaps if you posted the table descriptions and a sample of the data ?
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
#7
Moderator
Thread Starter
iTrader: (5)
Join Date: Nov 2001
Location: Not all those who wander are lost
Posts: 17,863
Received 0 Likes
on
0 Posts
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
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
Trending Topics
#8
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
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
Thread
Thread Starter
Forum
Replies
Last Post