(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
location
driver
group name
passengers

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 TorontoAsked:
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.

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,
        location,
        COUNT(distinct groupname) AS groupcount,
        MIN(pickuptime) AS mintime,
        MAX(pickuptime) AS maxtime
FROM    YourTable
GROUP BY driver, location

Open in new window

0
APD TorontoAuthor 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

SAMPLE.csv
0
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,
        user_initials,
        id,
        'Whatever'
FROM    reservations
WHERE   pickup_date = '2014-07-06'
ORDER BY pickup_time, pickup_location, driver_id

Open in new window

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

APD TorontoAuthor 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

0
APD TorontoAuthor Commented:
Or for this I need 2 queries?
0
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.
0

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 TorontoAuthor Commented:
Thank you!
0
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.

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.