# How find last date in a query

This would be a simple solution IF the table I'm working with had been designed differently.  But here goes...
I have a query that gets data from a table that has fileds named "Date 1", "Drop 1 Billed", "Date 2", "Drop 2 Billed", "Date 3", "Drop 3 Billed", "Date 4", "Drop 4 Billed", "Date 5", "Drop 5 Billed", and "Date 6", "Drop 6 Billed".  I've attached a screen grab of the resulting query so you can see what I'm talking about.  I need to add a field to this query to show the last date where the associated "billed" date is true.  In other words, using the attached example, in the last column of my query I want 7/21/2010 to be the result because 8/5/2010 is not true yet.
What would be syntax be for this last column?
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Owner, Dev-Soln LLCCommented:
you forgot the screen shot
Commented:
Screen shot and existing qyery please.
Owner, Dev-Soln LLCCommented:
You are either going to need a function or a union query.  My guess is that the function will be the way to go.

Public Function fnLastBilledDate(Paramarray arBilling() as Variant) as Date

Dim intLoop as integer
Dim dtLast as date

For intLoop = lbound(arBilling) to Ubound(arBilling) Step 2
if arBilling(intLoop + 1) = False Then
fnLastBilledDate = dtLast
Exit Function
else
dtLast = arBilling(intLoop)
endif
Next

End Function

Then, in your query you would call this:

SELECT field1, field2, fnLastBilledDate([Date 1], [Drop 1 Billed], [Date 2], [Drop 2 Billed], [Date 3], [Drop 3 Billed], ...) as LastBilledDate
FROM yourTable

Commented:
SELECT MAX(a.MaxDate)
FROM
(
select max("Date 1") as MaxDate from table_name where "Drop 1 Billed" = 1
union
select max("Date 2") as MaxDate from table_name where "Drop 2 Billed" = 1
union
select max("Date 3") as MaxDate from table_name where "Drop 3 Billed" = 1
union
select max("Date 4") as MaxDate from table_name where "Drop 4 Billed" = 1
union
select max("Date 5") as MaxDate from table_name where "Drop 5 Billed" = 1
union
select max("Date 6") as MaxDate from table_name where "Drop 6 Billed" = 1
) AS a
Author Commented:
I did forget the screen shot.  Here it is.

DateExample.jpg
Author Commented:
Here is my SQL code so far without the last column.  What do I need to do to it to include the last column?

SELECT tblProject.LogicJobN, tblProject.Date1, tblProject.Drop1Billed, tblProject.Date2, tblProject.Drop2Billed, tblProject.Date3, tblProject.Drop3Billed, tblProject.Date4, tblProject.Drop4Billed, tblProject.Date5, tblProject.Drop5Billed, tblProject.Date6, tblProject.Drop6Billed
FROM tblProject;
Commented:
If we can assume the following:1) The dates always increase as we move to the "next" drop number, and2) There is never an instance where Drop[N]Billed is true but Drop[N-1]Billed is false...SELECT tblProject.LogicJobN, tblProject.Date1, tblProject.Drop1Billed, tblProject.Date2, tblProject.Drop2Billed, tblProject.Date3, tblProject.Drop3Billed, tblProject.Date4, tblProject.Drop4Billed, tblProject.Date5, tblProject.Drop5Billed, tblProject.Date6, tblProject.Drop6Billed,Switch(Drop6Billed, Date6, Drop5Billed, Date5, Drop4Billed, Date4, Drop3Billed, Date3, Drop2Billed, Date2, Drop1Billed, Date1, True, Null) AS LastDateFROM tblProjectNote that returns a Null for "none of the above"--is that what you would want?

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
I think you can do it with a CASE.

SELECT CASE WHEN "Drop 6 Billed" = -1 Then "Date 6"
WHEN "Drop 5 Billed" = -1 Then "Date 5"
WHEN "Drop 4 Billed" = -1 Then "Date 4"
WHEN "Drop 3 Billed" = -1 Then "Date 3"
WHEN "Drop 2 Billed" = -1 Then "Date 2"
WHEN "Drop 1 Billed" = -1 Then "Date 1"
ELSE NULL
END AS SelectedDate
Commented:
8080_Diver,This is Access :)Patrick
Commented:
Sorry, forgot Access has limitations (although, I thought I had used a CASE in Access before).
I have provided the equivalent ACCESS IIF statement.

``````IIF(("Drop 6 Billed" = -1), "Date 6",
IIF("Drop 5 Billed" = -1), "Date 5",
IIF(("Drop 4 Billed" = -1), "Date 4",
IIF("Drop 3 Billed" = -1), "Date 3",
IIF(("Drop 2 Billed" = -1), "Date 2",
IIF(("Drop 1 Billed" = -1), "Date 1",NULL)
)
)
)
)
``````
Commented:
>>although, I thought I had used a CASE in Access beforeVBA has a Select Case logical structure, so you may have been thinking about that :)
Commented:
Indeed, I had gone back and looked at the database/app I was thinking of and that is exactly what I used.
I tend to only prototype in Access and then I switch to SQL Server and, even if I keep the Access front end, I use a SQL Server backend (and a lot fo stored procs ;-).
Owner, Dev-Soln LLCCommented:
I gave you the answer in my first post, but I like MatthewsPatrick's use of Switch even better, since Switch is already optimized to do exactly what the function I gave you would do.
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.