Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SELECT and GROUP BY part of a date/time field?

Status
Not open for further replies.

lwales

Programmer
Nov 11, 2002
2
GB
Is it possible to select only the date part of a date time field and also use it to group by and order by? It's the bookings.booked_date_time field. (I think I'm now at the point of completely over complicating it - no doubt there's a simpler solution)

SELECT Count(bookings.id) AS CountOfid, bookings.offer_id, Sum(bookings.num_seats_booked) AS CountOfseats, bookings.booked_date_time, offer_timeslots.timefrom, offer_timeslots.timeto, Sum(bookings.noofpromos) AS CountOfpromobooked
FROM (bookings INNER JOIN offers on offers.id = bookings.offer_id) INNER JOIN offer_timeslots on offer_timeslots.id = bookings.timeslot_id
WHERE bookings.enabled=1 AND offers.id=offerid
GROUP BY bookings.booked_date_time, bookings.offer_id, offer_timeslots.timefrom, offer_timeslots.timeto
ORDER BY bookings.booked_date_time DESC

 
Is it possible to select only the date part of a date time field and also use it to group by and order by?

yes

you did not say which database you're using, but i'm going to take an educated guess based on the parentheses you have around your join tables and say it's access

select otherstuff
, Format(bookings.booked_date_time,"yyyy/mm/dd")
from ...
where ...
group by otherstuff
, Format(bookings.booked_date_time,"yyyy/mm/dd")
order by otherstuff
, Format(bookings.booked_date_time,"yyyy/mm/dd")

by the way, you have a join condition in your WHERE clause, it's not a good idea to do that if you're going to use JOIN syntax

rudy
 
thanks very much rudy, I'll give it a go
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top