I am trying to query the code, name, and the number of rentals of customers who have made at least ten rentals.
My query:
Select client.ClientCode, client.ClientNom, (location.LocationRef)
FROM client
INNER JOIN location ON client.ClientCode = location.ClientCode
WHERE location.LocationRef > 9
GROUP BY location.ClientCode;
Output:
ClientCode | ClientNom | LocationRef
-----------+-----------+------------
12874 | Alex | 10
It doesn't work. It shows me the first LocationRef beyond 10 but not the number of rentals made by the client.
I test with a COUNT in my query :
SELECT client.ClientCode, client.ClientNom, Count(location.LocationRef)
FROM client
INNER JOIN location
ON client.ClientCode = location.ClientCode WHERE location.LocationRef > 9
GROUP BY location.ClientCode;
This is what i got :
ClientCode | ClientNom | Count(location.locationRef)
-----------+-----------+----------------------------
12874 | Alex | 5
This is not what I want, because my client has made 13 rentals and it only shows me the number of rentals beyond the 9th rental.
Script to install my database:
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
CREATE TABLE `client` (
`ClientCode` int(11) NOT NULL,
`ClientNom` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `client` (`ClientCode`, `ClientNom`) VALUES
(123, 'Gaetan'),
(12874, 'Alex'),
(12875, 'Max');
CREATE TABLE `location` (
`LocationRef` int(11) NOT NULL,
`Immatriculation` varchar(11) NOT NULL,
`ClientCode` int(11) NOT NULL,
`MontantLocation` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `location` (`LocationRef`, `Immatriculation`, `ClientCode`, `MontantLocation`) VALUES
(1, 'AA-229-AA', 12874, 123), (2, 'AA-229-AB', 12875, 156),
(3, 'BA-229-AA', 12874, 700), (4, 'AB-229-AA', 12874, 678),
(5, 'AA-229-AB', 12874, 987), (6, 'AA-229-AB', 12874, 980),
(7, 'AB-229-AA', 12874, 567), (8, 'AA-229-AA', 12874, 7789),
(9, 'AA-229-AB', 12874, 567), (10, 'AA-229-AB', 12874, 456),
(11, 'AA-229-AA', 12874, 566), (12, 'AB-229-AA', 12874, 700),
(13, 'AA-229-AA', 12874, 899), (14, 'AA-229-AB', 12874, 67);