We help IT Professionals succeed at work.

Where Dates are equal

cansevin
cansevin asked
on
I have the below query that I am using. The problem is want it only when DateRecorded is equal to FirstDay. First Day is a date in the form of 01-01-2014 and date recorded is in the form: 8/18/2014 11:55:24 AM. How can I see results were Firsday and DateRecorded are the same date? Thanks!

SELECT qryBookingDayswithYear.CustomerID, qryBookingDayswithYear.FirstDay, Sum([Booking Details Extended].ExtendedPrice) AS SumOfExtendedPrice, [Booking Details Extended].DateRecorded
FROM qryBookingDayswithYear INNER JOIN [Booking Details Extended] ON qryBookingDayswithYear.CustomerID = [Booking Details Extended].CustomerID
GROUP BY qryBookingDayswithYear.CustomerID, qryBookingDayswithYear.FirstDay, [Booking Details Extended].DateRecorded;

Open in new window

Comment
Watch Question

Top Expert 2013

Commented:
Is the qryBookingDayswithYear.FirstDay column type TEXT or DATE? Your query may have just the "format" setting to show it like this. The actual data maybe DATE.
If the column is indeed the date column then this query will do the trick:
SELECT qryBookingDayswithYear.CustomerID, qryBookingDayswithYear.FirstDay, Sum([Booking Details Extended].ExtendedPrice) AS SumOfExtendedPrice, [Booking Details Extended].DateRecorded
FROM qryBookingDayswithYear INNER JOIN [Booking Details Extended] ON qryBookingDayswithYear.CustomerID = [Booking Details Extended].CustomerID
WHERE DateValue(qryBookingDayswithYear.FirstDay) = DateValue([Booking Details Extended].DateRecorded)
GROUP BY qryBookingDayswithYear.CustomerID, qryBookingDayswithYear.FirstDay, [Booking Details Extended].DateRecorded;

Open in new window

Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
This will work:
SELECT 
    qryBookingDayswithYear.CustomerID, 
    qryBookingDayswithYear.FirstDay, 
    Sum([Booking Details Extended].ExtendedPrice) AS SumOfExtendedPrice, 
    [Booking Details Extended].DateRecorded
FROM 
    qryBookingDayswithYear 
INNER JOIN 
    [Booking Details Extended] 
    ON qryBookingDayswithYear.CustomerID = [Booking Details Extended].CustomerID
GROUP BY 
    qryBookingDayswithYear.CustomerID, 
    qryBookingDayswithYear.FirstDay, 
    [Booking Details Extended].DateRecorded
HAVING
    qryBookingDayswithYear.FirstDay = Fix([Booking Details Extended].DateRecorded);

Open in new window

or, if you prefer:
HAVING
    DateDiff("d", qryBookingDayswithYear.FirstDay, [Booking Details Extended].DateRecorded) = 0;

Open in new window

or, if the dates are text:
HAVING
    DateValue(qryBookingDayswithYear.FirstDay) = DateValue([Booking Details Extended].DateRecorded);

Open in new window

/gustav
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Commented:
@Gustav,

"or if the dates are text"?

Why only if the dates are text?  DateValue( ) basically strips the time portion of a Date/Time datatype and can be used to compare simple Date() values with those that include time values.
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Commented:
@cansevin,

Another method, although longer, would be:

WHERE (qryBookingDayswithYear.FirstDay <= [Booking DetailsExtended].DateRecorded)
AND ([Booking DetailsExtended].DateRecorded < qryBookingDayswithYear.FirstDay + 1)

Open in new window


The other examples simply use either the Fix( ) or the DateValue( ) functions to strip the time portion of the [DateRecorded] value and compare the integer portion of those fields.
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
> Why only if the dates are text?

Because [First Date] doesn't have a time part.

/gustav
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Commented:
@Gustav,

I'm not nit-picking here, just trying to understand your logic.
Why only if the dates are text?  the OP indicated that [FirstDay] is a date only value.  The DateValue function works with both text (if formatted properly) and date data types.  You simply would not need to use the DateValue([FirstDay]) since that value is already in the mm/dd/yy format.

In your example:

HAVING
    qryBookingDayswithYear.FirstDay = DateValue([Booking Details Extended].DateRecorded);

Open in new window


And for clarification, why process the criteria after the grouping, with a HAVING clause, instead of before the aggregation, with a WHERE clause?
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
I don't follow ... if FirstDate is of data type Date and has not time part, why would you use DateVaue on this?

Yes, perhaps you can use WHERE in place of HAVING. I just picked what first came to my mind.

/gustav

Author

Commented:
Thanks Guys... Gustav, your's seems to work. Only issue is it is grouping it one to many ways. Any way to get it to group without the DateRecorded. So it sums the FirstDay. As of now each day is seperated by the different DateRecorded entries.

I'd like to see a simple sum for the FirstDay.

Thanks for help, much appreciated!
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Yes, just exclude it from the grouping section
SELECT 
    qryBookingDayswithYear.CustomerID, 
    qryBookingDayswithYear.FirstDay, 
    Sum([Booking Details Extended].ExtendedPrice) AS SumOfExtendedPrice
FROM 
    qryBookingDayswithYear 
INNER JOIN 
    [Booking Details Extended] 
    ON qryBookingDayswithYear.CustomerID = [Booking Details Extended].CustomerID
GROUP BY 
    qryBookingDayswithYear.CustomerID, 
    qryBookingDayswithYear.FirstDay
HAVING
    qryBookingDayswithYear.FirstDay = Fix([Booking Details Extended].DateRecorded);

Open in new window

/gustav

Author

Commented:
Thanks Gustav... unfortunately it comes up an error. See attached

Error-0414.pdf
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
Yes, it should use WHERE now:
SELECT 
    qryBookingDayswithYear.CustomerID, 
    qryBookingDayswithYear.FirstDay, 
    Sum([Booking Details Extended].ExtendedPrice) AS SumOfExtendedPrice
FROM 
    qryBookingDayswithYear 
INNER JOIN 
    [Booking Details Extended] 
    ON qryBookingDayswithYear.CustomerID = [Booking Details Extended].CustomerID
WHERE
    qryBookingDayswithYear.FirstDay = Fix([Booking Details Extended].DateRecorded)
GROUP BY 
    qryBookingDayswithYear.CustomerID, 
    qryBookingDayswithYear.FirstDay;

Open in new window

/gustav

Author

Commented:
Thanks! It worked... now I changed some things and have another problem. I'll post another question. I appreciate your help.