Go Back   CodingForums.com > :: Server side development > MySQL > Other Databases

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-12-2007, 09:30 AM   PM User | #1
newbie2006
New to the CF scene

 
Join Date: Jun 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
newbie2006 is an unknown quantity at this point
How to get Min and Max from Resultset

Hi there

I am using MS Access and am trying to extract the region information from my vendorquery and find the earliest period and latest period for each. My raw data looks like (RID=regionID):

RID Year Quarter
1 1997 2
1 1998 2
1 1999 1
1 2006 1
2 1990 3

I have used the following sql:

SELECT Region_id, VendorQuery.Year AS BeginningYear
FROM VendorQuery
WHERE (((VendorQuery.Year)=(Select min(year) from vendorquery as subquery where subquery.year = vendorquery.year)));

I manage to get the region and beginning year using the above SQL but now i need to extend the query into "quarter" e.g. i should retrieve 1997 qtr 2 as my last record for region 1:

RID Year Quarter
1 1997 2

How do i do that?
newbie2006 is offline   Reply With Quote
Old 07-12-2007, 01:13 PM   PM User | #2
Daemonspyre
Regular Coder

 
Join Date: Mar 2007
Posts: 505
Thanks: 1
Thanked 19 Times in 19 Posts
Daemonspyre is on a distinguished road
Try this article from the MySQL Moderator Guelphdad.

It explains how to get what you are looking for:

http://guelphdad.wefixtech.co.uk/sql...romgroup.shtml
__________________
Quote:
To say my fate is not tied to your fate is like saying, 'Your end of the boat is sinking.' -- Hugh Downs
Please, if you found my post helpful, pay it forward. Go and help someone else today.
Daemonspyre is offline   Reply With Quote
Old 07-12-2007, 03:23 PM   PM User | #3
newbie2006
New to the CF scene

 
Join Date: Jun 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
newbie2006 is an unknown quantity at this point
Hi there

i did what was suggested. However i need to do this at 2 levels:

1) between RID and Year
2) between Year and Quarter

This is to select the minimum year for that Region_id
SELECT vendorquery.Region_ID, Min(vendorquery.Year) AS MinOfYear
FROM vendorquery
GROUP BY vendorquery.Region_ID;

and this is to select the min quarter for that Year
SELECT vendorquery.Year, Min(vendorquery.Quarter) AS MinOfQuarter
FROM vendorquery
GROUP BY vendorquery.Year;

I tried to combine them

SELECT vendorquery.Region_ID, Min(vendorquery.Year) AS MinOfYear, Quarter
FROM vendorquery
where Quarter=(SELECT vendorquery.Year, Min(vendorquery.Quarter) AS MinOfQuarter
FROM vendorquery
)
GROUP BY vendorquery.Region_ID;

the error given by MS Access is:
"You have written a subquery that can return more than one field without using "exists" in the main query "from" clause

I am really new in SQL and will appreciate any help.
newbie2006 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:41 PM.


Advertisement
Log in to turn off these ads.