• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 83
  • Last Modified:

Where Dates are equal

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

0
cansevin
Asked:
cansevin
  • 5
  • 3
  • 3
  • +1
1 Solution
 
chaauCommented:
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

0
 
Gustav BrockCIOCommented:
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
0
 
Dale FyeCommented:
@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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Dale FyeCommented:
@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.
0
 
Gustav BrockCIOCommented:
> Why only if the dates are text?

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

/gustav
0
 
Dale FyeCommented:
@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?
0
 
Gustav BrockCIOCommented:
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
0
 
cansevinAuthor 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!
0
 
Gustav BrockCIOCommented:
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
0
 
cansevinAuthor Commented:
Thanks Gustav... unfortunately it comes up an error. See attached

Error-0414.pdf
0
 
Gustav BrockCIOCommented:
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
0
 
cansevinAuthor Commented:
Thanks! It worked... now I changed some things and have another problem. I'll post another question. I appreciate your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 5
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now