(My)SQL Group by

Hi Experts,

Can anyone give me quick refresher on group by?

I am writing a reservations program and I have the following fields

pickup time
group name

As a 1 driver can pickup multiple of groups from a single location, I need to group my results as such. In other words, 1 group is distinguished by time/location/driver.

How can I achieve this?

Thank you
APD TorontoSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

dsackerContract ERP Admin/ConsultantCommented:
Can you show your raw data, then show what it should look like after you query?

Based on what you said, you may be heading in this direction, but not sure (need more info):

SELECT  driver,
        COUNT(distinct groupname) AS groupcount,
        MIN(pickuptime) AS mintime,
        MAX(pickuptime) AS maxtime
FROM    YourTable
GROUP BY driver, location

Open in new window

APD TorontoSoftware DeveloperAuthor Commented:
Base on your code I came up with the following:
SELECT `user_initials`, `id`, pickup_date, pickup_time, COUNT(id) as reservation_count,
	CONCAT('LOCATION ', pickup_location, ' DRIVER ', driver_id) AS loc_driver_group

FROM `reservations` 
group BY pickup_location, driver_id

HAVING pickup_date = '2014-07-06'
ORDER BY pickup_time, loc_driver_group

Open in new window

This gives me the attached data. This is 80% what I need, but as you will see that first group has 4 reservations, the second 240, and so on. I need these reservations also listed. I know that last 4 columns will be repetitive, but the reason why I need this is because I need to produce a PHP(HTML) report as such.

6:30 at LOCATION 1 DRIVER 13
   CN 3653 Whatever
   AB 3674 Whatever
   WB 3677 Whatever
   CN 3453 Whatever

Open in new window

dsackerContract ERP Admin/ConsultantCommented:
You probably don't need a GROUP BY. The following query will put your heading info in the first column and the unique information in the remaining columns:

SELECT  CONCAT(DATE_FORMAT(pickup_time, '%h:%i'), ' at LOCATION ', pickup_location, ' DRIVER ', driver_id) AS time_loc_driver_group,
FROM    reservations
WHERE   pickup_date = '2014-07-06'
ORDER BY pickup_time, pickup_location, driver_id

Open in new window

IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

APD TorontoSoftware DeveloperAuthor Commented:
I am at 95%, but I also need to add count (sorry I forgot to mention this), so it should be

6:30, 4 pickups at LOCATION 1 DRIVER 13
   CN 3653 Whatever
   AB 3674 Whatever
   WB 3677 Whatever
   CN 3453 Whatever

Open in new window

APD TorontoSoftware DeveloperAuthor Commented:
Or for this I need 2 queries?
dsackerContract ERP Admin/ConsultantCommented:
You need to join a subquery that is like the previous query you posted where you selected the count. I'm away and out of the house at the moment, but you may be able to figure that out. If not, I'll code it up when I return.

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial
APD TorontoSoftware DeveloperAuthor Commented:
Thank you!
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
Query Syntax

From novice to tech pro — start learning today.