DMin, Dmax

J

JimP

How can I structure code or a query to find the minimum and maximum [Year]
and [MthNbr] for each [CustID], one record per [CustID], e.g.

Table1
[CustID] [Year] [MthNbr]
1 2004 7
1 2005 9
1 2006 3
2 2005 2
2 2006 8
2 2007 4

Query Results
[CustID] [MinYear] [MinMthNbrForMinYear] [MaxYear]
[MaxMthNbrForMaxYear]
1 2004 7
2006 3
2 2005 2
2007 4
 
D

Dale Fye

Jim,

How about starting with something like:

SELECT CustID, Min(Dateserial([Year], [MthNbr], 1), Max(DateSerial([Year],
[MthNbr] + 1, 0)
FROM yourTable
GROUP BY CustID

Then take that and parse out the year and month
SELECT CutsID,
Year(Min(Dateserial([Year], [MthNbr], 1)) as MinYr,
Month(Min(Dateserial([Year], [MthNbr], 1)) as MinMthNbrForMinYr,
Year(Max(DateSerial([Year], [MthNbr] + 1, 0)) as MaxYr,
Month(Max(DateSerial([Year], [MthNbr] + 1, 0)) as
MaxMthNbrForMaxYr
FROM yourTable
GROUP BY CustID

HTH
Dale
 
S

Stefan Hoffmann

hi Jim,
How can I structure code or a query to find the minimum and maximum [Year]
and [MthNbr] for each [CustID], one record per [CustID], e.g.

SELECT CustID,
DMin("[Year]","", "CustID=" & CustID),
..
FROM Table1
GROUP BY CustID



mfG
--> stefan <--
 
S

Stefan Hoffmann

hi Jim,
I'm not sure this is going to work.I'm trying to get the minimum and maximum
values in a single record. e.g. the following code,

SELECT
CustID,
DMin("[Year]","Table1","CustID=" & [CustID]) AS MinYear,
DMin("[Nbr]","Table1","[Year]=" & DMin("[Year]","Table1") & "
And CustID=" & [CustID]) AS MinYearNbr,
DMax("[Year]","Table1","CustID=" & [CustID]) AS MaxYear,
DMax("[Nbr]","Table1","[Year]=" & DMax("[Year]","Table1") & "
And CustID=" & [CustID]) AS MaxYearNbr
FROM Table1
GROUP BY CustID;

yields the following incomplete results
The inner DMin/DMax must also be filtered on CustID.


mfG
--> stefan <--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Subquery 2
Keep unique values 1
Query Help 9
query flat file data for most recent data 3
Arriving at totals 2
Grouping query help 3
Help with DMin? 3
Paragraph numbering in Word 1

Top