If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > multiple-column subquery

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-09-03, 09:18
Sporkish Sporkish is offline
Registered User
 
Join Date: Jul 2003
Posts: 3
multiple-column subquery

Hello all, I've got this table in a database at work, and unfortunately I'm just a temp hire so there's no persuading them to switch off access 2002 databases and move to something like sql2000 or mysql. but this is irrelevant.


I've got a table like so:

r=region
s=sku
p=period
a=amount

r s p a
1 1 2 1
1 1 1 3
1 1 3 2
1 2 3 2
1 2 2 5
1 2 3 4
2 1 .....

what I need is for each region+sku combination (so like

"SELECT DISTINCT region, sku FROM table GROUP BY region, sku"

but this is not actually my code) I need to find the record that shipped most recently (max(period)) and then within that, I need to find the most shipment (max(amount))

the question is how do I do this? I can first do a query to

"SELECT DISTINCT region, sku, max(period) FROM table GROUP BY region, sku"

and this gets my max(period), but I have to drop the amount column for this to work, because puttingin max(amount) would pull out the max amount for ALL region+sku combinations, not the max(amount) within the max(period).

I can include amount in my field selection but sql requires for a group by that if I'm not using an aggregate function on a column, but I want to select it, it has to be listed in the group by fields. This can't happen because grouping by amount prevent me from selecting the max period within the region+sku combination.

So then I could do it like so:

SELECT DISTINCT region,sku, max(amount), period FROM table GROUP BY region, sku, period

and this is my best so far, because it gets me the distinct periods within the region+sku combination and the max amount FOR each period ...

this is where I'm lost

I tried a parent query to select from this current subquery the max(period), but for obvious reasons I then have to drop my amount column again, because again group by requires I include amount in either an aggregate function or the group by conditions, and grouping by amount eliminates my ability to select the max(period)

I've had other ideas, but I've written enough, and I imagine someone should have a clue for me.

thanks so much, I'm very willing to provide more information

john h.
Reply With Quote
  #2 (permalink)  
Old 07-09-03, 09:52
Sporkish Sporkish is offline
Registered User
 
Join Date: Jul 2003
Posts: 3
Re: multiple-column subquery

nevermind, I got it! right when I hit submit it came to me ....


select the distinct region+sku combos, and find the max period for that ... so we'd have one record per region+sku and the only other field would be the maxperiod ... then inner join that query with the original table ON r=r, s=s, and p=p .... this will then pull all the amounts for that max period, and we can just do the max amount for that distinct region+sku+period combo, and that leaves us with the max of the original four fields, and I can then do one more inner join with my original table where r=r, s=s, p=p, and a=a to pull all the other fields I didnt' tell you all about :-P

thanks to whoever wasted their time reading me blabber!
Reply With Quote
  #3 (permalink)  
Old 07-09-03, 10:22
gannet gannet is offline
Registered User
 
Join Date: Oct 2002
Location: Plymouth UK
Posts: 116
This should do it

SELECT p.region, p.sku, p.period, a.amount
FROM
(SELECT region, sku, MAX(period) period
FROM table
GROUP BY region, sku) p,
(SELECT region, sku, period, MAX(amount) amount
FROM table
GROUP BY region, sku, period) a
WHERE p.region = a.region
AND p.sku = a.sku
AND p.period = a.period;

Incidentally you don't need the distinct statments because the group by part returns distinct records.

Last edited by gannet; 07-09-03 at 10:31.
Reply With Quote
  #4 (permalink)  
Old 07-09-03, 10:24
gannet gannet is offline
Registered User
 
Join Date: Oct 2002
Location: Plymouth UK
Posts: 116
Looks we crossed in the post. Hope that helped anyway.
Reply With Quote
  #5 (permalink)  
Old 07-09-03, 11:19
Sporkish Sporkish is offline
Registered User
 
Join Date: Jul 2003
Posts: 3
brilliant, your code was a lot better ... mine was ... so many nested sql statements!

oh well, that's what happens when you have brain farts


thanks for your help, although ... when I read your post I'd already used my code to then insert the 9014 records either statement would produce into a different table ....

who says one-time-use code has to be efficient, right?

cheers, thanks
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On