Kumaresan M
Kumaresan M

Reputation: 23

How to use group by and count in mysql

Here I am having 3 table using I have to take the count

trip_details

id    allocationId       tripId

1         7637             aIz2o
2         7626             osseC
3         7536             01LEC
4         7536             78w2w
5         7640             S60zF
6         7548             ruaoR
7         7548             Qse6s

escort_allocation

id       allocationId    escortId

3          7637            1
4          7626            1
5          7627            1
6          7536            1
7          7640            1
7          7548            1

cab_allocation

 allocationId    allocationType

 7637             Daily Trip
 7626             Daily Trip
 7627             Daily Trip
 7536              Adhoc Trip
 7640               Adhoc Trip
 7548               Daily Trip

Using above table I have to get the count, I tried but it is not happening my expected results.

I tried sql query

    SELECT a.`tripId` 
FROM  `trip_details` a
INNER JOIN escort_allocation b ON a.`allocationId` = b.`allocationId` 
GROUP BY a.`allocationId` 
LIMIT 0 , 30

I am getting like this

tripId

01LEC
ruaoR
osseC
aIz2o
S60zF

total 6 tripId i got ,so now I want to take the count so I am using this query

    SELECT COUNT(*)
FROM  `trip_details` a
INNER JOIN escort_allocation b ON a.`allocationId` = b.`allocationId` 
GROUP BY a.`allocationId` 
LIMIT 0 , 30

but this query is not working.I am getting results like below

 2
2
1
1
1

MY MYSQL TABLES AND VALUES LOOK LIKE THIS

    CREATE TABLE `trip_details` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `allocationId` int(11) NOT NULL,
 `tripId` varchar(100) NOT NULL,
 PRIMARY KEY (`id`),
 KEY `tripId` (`tripId`),
 KEY `allocationId` (`allocationId`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 ;

INSERT INTO trip_details
    (id, allocationId, tripId)
VALUES
    (1, 7637, '00SwM'),
    (2, 7626, '00SwM'),
    (3, 7536, '00SwM'),
    (4, 7536, '01hEU'),
    (5, 7640, '01hEU'),
    (6, 7548, 'IRZMS'),
    (7, 7548, 'IRZMS');




CREATE TABLE `escort_allocation` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `allocationId` int(11) NOT NULL,
 `escortId` varchar(100) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 ;

INSERT INTO escort_allocation
    (id, allocationId, escortId)
VALUES
    (1, 7637, 'ssda'),
    (2, 7626, 'adad'),
    (3, 7627, 'sfsaf'),
    (4, 7536, 'ssaf'),
    (5, 7640, 'asf'),
    (6, 7548, 'a3r');



CREATE TABLE `cab_allocation` (
 `allocationId` int(11) NOT NULL AUTO_INCREMENT,
 `allocationType` enum('Daily Trip','Adhoc Trip') NOT NULL,
 PRIMARY KEY (`allocationId`)
) ENGINE=InnoDB AUTO_INCREMENT=7695 DEFAULT CHARSET=latin1;

INSERT INTO cab_allocation
    (allocationId, allocationType)
VALUES
    (7637, 'Daily Trip'),
    (7626, 'Daily Trip'),
    (7627, 'Daily Trip'),
    (7536, 'Adhoc Trip'),
    (7640, 'Adhoc Trip'),
    (7548, 'Daily Trip');

Upvotes: 0

Views: 98

Answers (3)

UkFLSUI
UkFLSUI

Reputation: 5672

You may use:

SELECT COUNT(DISTINCT a.allocationId)
FROM
trip_details a
INNER JOIN
escort_allocation b
ON a.allocationId = b.allocationId

Previously you used COUNT(*) and also used GROUP BY so the counts of rows you were getting are from individual groups.

Update-2:

SELECT 
(
    SELECT COUNT(*) FROM trip_details
) AS Total_Trip_Count,
COUNT(T.tripId) as Escort_Count,
(
    SELECT COUNT(*) FROM 
    (
        SELECT a.allocationId
        FROM escort_allocation a 
        INNER JOIN cab_allocation c ON a.allocationId = c.allocationId 
        WHERE c.allocationType = 'Adhoc Trip' 
        GROUP BY a.allocationId
    ) AS Ad
) AS Adhoc_Trip_Count
FROM 
( 
    SELECT a.tripId FROM 
    trip_details a 
    INNER JOIN 
    escort_allocation b 
    ON a.allocationId = b.allocationId 
    GROUP BY a.allocationId 
) AS T

Upvotes: 0

Eva
Eva

Reputation: 173

With this, you should get the tripid and the amount:

SELECT COUNT(a.tripId) as total, a.tripId as tripId 
FROM trip_details a INNER JOIN escort_allocation b 
ON a.allocationId = b.allocationId 
GROUP BY a.allocationId LIMIT 0 , 30

Upvotes: 1

Jenish
Jenish

Reputation: 535

You can try this

 SELECT COUNT(DISTINCT (a.`tripId`))
 FROM  `trip_details` a
 INNER JOIN escort_allocation b ON a.`allocationId`=b.`allocationId`
 LIMIT 0 , 30

because of GROUP BY there is separate count for all allocationId.

Upvotes: 1

Related Questions