A Microsoft Office (Excel, Word) forum. OfficeFrustration

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   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Date Query



 
 
Thread Tools Display Modes
  #1  
Old October 19th, 2007, 07:37 AM posted to microsoft.public.access.queries
Scottie
external usenet poster
 
Posts: 36
Default Date Query

I have created a table with the following fields

ID (autonumber)
RID (Lookup)
Weight (number,Single, 1 decimal place)
Date of (Date)

I need to create a query with:

1. requested input value for year
2. an expression that will tell me the FIRST Weight in January (of the year
provided by user input)

I can use the january expression as a template to create the rest of the
months of the year


I know you guys can help me...you always do....so whatcha think? Thank you
very much in advance.

Thanks,
Scottie
  #2  
Old October 19th, 2007, 10:23 AM posted to microsoft.public.access.queries
Scottie
external usenet poster
 
Posts: 36
Default Date Query

Looks like I forgot to mention that the query should be generated for each
person in the Resident Table(RID)...
  #3  
Old October 19th, 2007, 01:52 PM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 7,815
Default Date Query

Query one to get FirstDate in Month for each RID
SELECT RID
, Month([DateOf]) as MonthNum
, Min([DateOf]) as FirstInMonth
FROM YourTable
WHERE [DateOf] Between DateSerial([What Year?],1,1) and DateSerial([What
Year?],12,31)
GROUP BY RID, Month([DateOf])

Using that query along with the original table to get the results

SELECT YourTable.*
FROM YourTable INNER JOIN QueryOne
ON YourTable.RID = QueryOne.RID
AND YourTable.[DateOf] = QueryOne.FirstInMonth

You can do this all in one correlated query. It will probably be a lot
slower than the two query solution
SELECT YourTable.*
FROM YourTable
WHERE [DateOf] =
(SELECT Min([DateOf])
FROM YourTable as YT2
WHERE YT2.RID = YourTable.RID
AND Month(YT2.[DateOf]) = Month(YourTable.[DateOf]) )
WHERE YourTable.[DateOf] Between DateSerial([What Year?],1,1) and
DateSerial([What Year?],12,31)

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..

"Scottie" wrote in message
...
I have created a table with the following fields

ID (autonumber)
RID (Lookup)
Weight (number,Single, 1 decimal place)
Date of (Date)

I need to create a query with:

1. requested input value for year
2. an expression that will tell me the FIRST Weight in January (of the
year
provided by user input)

I can use the january expression as a template to create the rest of the
months of the year


I know you guys can help me...you always do....so whatcha think? Thank
you
very much in advance.

Thanks,
Scottie



  #4  
Old October 19th, 2007, 09:54 PM posted to microsoft.public.access.queries
Scottie
external usenet poster
 
Posts: 36
Default Date Query

John,

I used your sql code to create the following

weightqry1

SELECT RID, Month([DateOf]) AS MonthNum, Min([DateOf]) AS FirstInMonth
FROM WeightTbl
WHERE [DateOf] Between DateSerial([What Year?],1,1) and DateSerial([What
Year?],12,31)
GROUP BY RID, Month([DateOf]);

weightqry2

SELECT WeightTbl.*
FROM WeightTbl INNER JOIN weightqry1 ON (WeightTbl.Dateof =
weightqry1.FirstInMonth) AND (WeightTbl.RID = weightqry1.RID);


I am still having an issue. right now the data is returning the following
in weightqry2

RID Weight Dateof
King, H 138.8 1/1/2007
King, H 134.4 2/1/2007
King, H 131.6 3/1/2007
King, H 136.8 4/1/2007
King, H 138.4 5/1/2007
King, H 139.6 6/1/2007
King, H 144 7/1/2007
King, H 145.2 8/1/2007
King, H 148.2 9/1/2007
King, H 142.4 10/1/2007

I would like it to return as:

RID January February March .....ect...
King, H 138.8 134.4 131.6

Thanks a lot for the help....looking forward to your solution

Scottie
  #5  
Old October 20th, 2007, 02:08 AM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 2,364
Default Date Query

Use weightqry2 as the source for a crosstab query.

TRANSFORM First(Weight)
SELECT RID
FROM WeightQry2
PIVOT Format(DateOf,"mmmm")
IN ("January","February",...,"November","December" )

'================================================= ===
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
'================================================= ===


Scottie wrote:
John,

I used your sql code to create the following

weightqry1

SELECT RID, Month([DateOf]) AS MonthNum, Min([DateOf]) AS FirstInMonth
FROM WeightTbl
WHERE [DateOf] Between DateSerial([What Year?],1,1) and DateSerial([What
Year?],12,31)
GROUP BY RID, Month([DateOf]);

weightqry2

SELECT WeightTbl.*
FROM WeightTbl INNER JOIN weightqry1 ON (WeightTbl.Dateof =
weightqry1.FirstInMonth) AND (WeightTbl.RID = weightqry1.RID);


I am still having an issue. right now the data is returning the following
in weightqry2

RID Weight Dateof
King, H 138.8 1/1/2007
King, H 134.4 2/1/2007
King, H 131.6 3/1/2007
King, H 136.8 4/1/2007
King, H 138.4 5/1/2007
King, H 139.6 6/1/2007
King, H 144 7/1/2007
King, H 145.2 8/1/2007
King, H 148.2 9/1/2007
King, H 142.4 10/1/2007

I would like it to return as:

RID January February March .....ect...
King, H 138.8 134.4 131.6

Thanks a lot for the help....looking forward to your solution

Scottie

 




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


All times are GMT +1. The time now is 11:55 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.