MySQL Left join with grouping and date - mysql

As you can see on this sqlfiddle, I have this schema:
CREATE TABLE reviews
(`id` int(11) NOT NULL AUTO_INCREMENT,
`shop_id` int(11),
`order_id` char(255),
`product_id` char(32),
`review_time` int(11),
PRIMARY KEY (`id`)
)
;
INSERT INTO reviews
(`shop_id`, `order_id`, `product_id`, `review_time`)
VALUES
('10', '100', '1000', '1466190000'),
('10', '100', '1000', '1466276400'),
('10', '100', '1000', '1466462800'),
('20', '800', '8000', '1466249200')
;
CREATE TABLE tags
(`id` int(11) NOT NULL AUTO_INCREMENT,
`shop_id` int(11),
`order_id` char(255),
`product_id` char(32),
`tag_time` INT(11) NULL,
PRIMARY KEY (`id`)
)
;
INSERT INTO tags
(`shop_id`, `order_id`, `product_id`, `tag_time`)
VALUES
('10', '100', '1000', '1466449200'),
('10', '100', '1000', NULL),
('10', '100', '3000', NULL),
('20', '800', '8000', '1469449200')
;
I need to get statistics by date showing how many reviews I have per date and how many were tagged and how many were not. I'm using this query:
SELECT
DATE_FORMAT(FROM_UNIXTIME(r.`review_time`), "%d.%m.%Y") AS review_submited_on,
r.`shop_id`,
COUNT(*) as total_orders,
COUNT(*) as tagged_orders
FROM
reviews AS r
LEFT JOIN tags as t
ON r.`shop_id` = t.`shop_id` AND
r.`order_id` = t.`order_id` AND
r.`product_id` = t.`product_id`
WHERE
t.`tag_time` IS NOT NULL
GROUP BY r.`shop_id`, r.`order_id`, r.`product_id`
ORDER BY review_submited_on ASC
UPDATE
The expected result would look like this:
| review_submited_on | shop_id | total_orders | tagged_orders |
|--------------------|---------|--------------|---------------|
| 17.06.2016 | 10 | 3 | 1 |
| 18.06.2016 | 20 | 1 | 1 |
I created this sqlfiddle for demo.
Thanks for any help :)

Try this, and let me know if that is something you want.
SELECT review_submited_on, shop_id, total_orders, IFNULL(tagged_orders, 0) tagged_orders
FROM
(SELECT shop_id, COUNT(DISTINCT shop_id, order_id, product_id) total_orders, DATE_FORMAT(FROM_UNIXTIME(review_time), "%d.%m.%Y") AS review_submited_on
FROM reviews
GROUP BY shop_id) review_counter
LEFT JOIN
(SELECT shop_id, COUNT(DISTINCT shop_id, order_id, product_id) tagged_orders
FROM tags
WHERE tag_time IS NOT NULL
GROUP BY shop_id) tag_counter
USING (shop_id)
Result
| review_submited_on | shop_id | total_orders | tagged_orders |
|--------------------|---------|--------------|---------------|
| 17.06.2016 | 10 | 1 | 1 |
| 18.06.2016 | 20 | 1 | 1 |

Related

Inner Join table with a maximum date

Please help me.
I have 2 table Users and Diseaselogs
1 user has many diseaselogs
I want to select all users and each user must be field date in diseaselogs is max
Data example
Users
id | name | age
1 | Nam | 21
2 | Thao | 23
3 | An | 19
Diseaselogs
id | logType | userId | date
1 | positive | 1 | 2021-06-21
2 | negative | 2 | 2021-06-22
3 | pending | 1 | 2021-06-24
4 | negative | 1 | 2021-06-26
5 | negative | 2 | 2021-06-21
6 | pending | 3 | 2021-06-23
7 | negative | 1 | 2021-06-24
8 | negative | 2 | 2021-06-25
9 | pending | 3 | 2021-06-28
Expect output
id | name | logId | logType | date
1 | Nam | 4 | negative | 2021-06-26
2 | Thao | 8 | negative | 2021-06-25
3 | An | 9 | pending | 2021-06-28
As ROW_NUMBER() doesn't support lower version of mysql i.e 5.7 to downwords. So I've tried here alternatives of ROW_NUMBER().
-- MySQL (V 5.6)
SELECT u.id, u.name, tmp.logId, tmp.logType, tmp.date
FROM users u
INNER JOIN (SELECT #row_no := IF(#prev_val = t.userId, #row_no + 1, 1) AS row_number
, #prev_val := t.userId AS userId
, t.date
, t.logType
, t.id AS logId
FROM Diseaselogs t,
(SELECT #row_no := 0) x,
(SELECT #prev_val := '') y
ORDER BY t.userId, t.date DESC ) tmp
ON u.id = tmp.userId
AND tmp.row_number = 1;
Please check url http://sqlfiddle.com/#!9/741b96/16
Also if your MySQL version is 5.8 then you can apply the below query where row_number() is used.
SELECT u.id, u.name, tmp.logId, tmp.logType, tmp.date
FROM users u
INNER JOIN (SELECT ROW_NUMBER() OVER (PARTITION BY userId ORDER BY date DESC) row_number
, userId
, date
, logType
, id AS logId
FROM Diseaselogs ) tmp
ON u.id = tmp.userId
AND tmp.row_number = 1;
in mysql 8 you can use ROW_NUMBER and an INNER JOIN
in prior versions you need to use another method for rownumber
Schema (MySQL v8.0)
CREATE TABLE Diseaselogs (
`id` INTEGER,
`logType` VARCHAR(8),
`userId` INTEGER,
`date` VARCHAR(10)
);
INSERT INTO Diseaselogs
(`id`, `logType`, `userId`, `date`)
VALUES
('1', 'positive', '1', '2021-06-21'),
('2', 'negative', '2', '2021-06-22'),
('3', 'pending', '1', '2021-06-24'),
('4', 'negative', '1', '2021-06-26'),
('5', 'negative', '2', '2021-06-21'),
('6', 'pending', '3', '2021-06-23'),
('7', 'negative', '1', '2021-06-24'),
('8', 'negative', '2', '2021-06-25'),
('9', 'pending', '3', '2021-06-28');
CREATE TABLE users (
`id` INTEGER,
`name` VARCHAR(4),
`age` INTEGER
);
INSERT INTO users
(`id`, `name`, `age`)
VALUES
('1', 'Nam', '21'),
('2', 'Thao', '23'),
('3', 'An', '19');
Query #1
SELECT
u.id, `name`, t1.id,`logType`, `date`
FROM
(SELECT
id,`logType`, `userId`, `date`
, ROW_NUMBER() OVER (PARTITION BY `userId` ORDER BY `date` DESC) rn
FROM
Diseaselogs) t1
INNER JOIN users u ON t1.userId = u.id
WHERE rn = 1;
id
name
id
logType
date
1
Nam
4
negative
2021-06-26
2
Thao
8
negative
2021-06-25
3
An
9
pending
2021-06-28
View on DB Fiddle
Schema (MySQL v8.0)
CREATE TABLE Diseaselogs (
`id` INTEGER,
`logType` VARCHAR(8),
`userId` INTEGER,
`date` VARCHAR(10)
);
INSERT INTO Diseaselogs
(`id`, `logType`, `userId`, `date`)
VALUES
('1', 'positive', '1', '2021-06-21'),
('2', 'negative', '2', '2021-06-22'),
('3', 'pending', '1', '2021-06-24'),
('4', 'negative', '1', '2021-06-26'),
('5', 'negative', '2', '2021-06-21'),
('6', 'pending', '3', '2021-06-23'),
('7', 'negative', '1', '2021-06-24'),
('8', 'negative', '2', '2021-06-25'),
('9', 'pending', '3', '2021-06-28');
CREATE TABLE users (
`id` INTEGER,
`name` VARCHAR(4),
`age` INTEGER
);
INSERT INTO users
(`id`, `name`, `age`)
VALUES
('1', 'Nam', '21'),
('2', 'Thao', '23'),
('3', 'An', '19');
Query #1
SELECT
u.id, `name`, `age`,`logType`, `date`
FROM
(SELECT
`logType`, `userId`, `date`
, ROW_NUMBER() OVER (PARTITION BY `userId` ORDER BY `date` DESC) rn
FROM
Diseaselogs) t1
INNER JOIN users u ON t1.userId = u.id
WHERE rn = 1;
id
name
age
logType
date
1
Nam
21
negative
2021-06-26
2
Thao
23
negative
2021-06-25
3
An
19
pending
2021-06-28
View on DB Fiddle

Mysql returning multiple row instead of one row with array of Images

Consider the following schema:
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(45) NULL,
PRIMARY KEY (id)
);
CREATE TABLE attachments (
id INT NOT NULL,
name VARCHAR(45) NULL DEFAULT NULL,
filePath VARCHAR(255) NULL DEFAULT NULL,
thumbnailPath VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (id)
);
CREATE TABLE reviews (
id INT NOT NULL AUTO_INCREMENT,
productId INT NULL,
user_id INT NULL,
review VARCHAR(45) NULL,
rating INT NULL,
PRIMARY KEY (id));
CREATE TABLE review_attachments (
id INT NOT NULL AUTO_INCREMENT,
review_id INT NULL,
attachment_id INT NULL,
PRIMARY KEY (id));
INSERT INTO users (id, name) VALUES
('1', 'XYZ'),
('2', 'ABC'),
('3', 'EVE');
INSERT INTO attachments (id, name, filePath, thumbnailPath) VALUES
('100', 'f.png', '/resources/attachments/2020/f.png', '/resources/attachments/thumbnail/2020/f.png'),
('101', 'd.png', '/resources/attachments/2020/d.png', '/resources/attachments/thumbnail/2020/d.png'),
('102', 'g.png', '/resources/attachments/2020/g.png', '/resources/attachments/thumbnail/2020/g.png');
INSERT INTO reviews (id, productId, user_id, review, rating) VALUES
('1', '1', '1', 'Great Product', '5'),
('2', '1', '2', 'Good Product', '4'),
('3', '1', '3', 'Bad Product', '1');
INSERT INTO review_attachments (id, review_id, attachment_id) VALUES
('1', '1', '100'),
('2', '1', '101'),
('3', '1', '102');
When I query against this schema using the following query I am getting two rows with the same entries.
I am using JSON_ARRAYAGG to return an array it may be because of groupby.
Can anyone help me get this query right?
select
review, rating, u.name,reviews.createdAt,
(SELECT
JSON_ARRAYAGG(JSON_OBJECT("attachmentId", attachments.id,
'filePath', TRIM(CONCAT("${process.env.NODE_SERVER_API_HOST}", '/',attachments.filePath)),
'thumbnailPath', TRIM(CONCAT("${process.env.NODE_SERVER_API_HOST}", '/',attachments.thumbnailPath))
))
FROM attachments
WHERE attachments.id = ra.attachment_id) as reviewAttachments
from reviews
left join users as u on u.id = reviews.user_id
left join reviewAttachments as ra on ra.review_id = reviews.id
left join attachments as at on at.id = ra.attachment_id
where productId = 1
and reviews.isDeleted =0
and reviews.review is not null
limit 0,5
|review |rating| name | createdAt | reviewAttachments |
|:---- |:----:| -----:| ---------:| ----------------:
|Great Product | 5 | XYZ | 2020-11-04| [{"filePath": "${process.env.NODE_SERVER_API_HOST}/resources/attachments/2020/f.png", "attachmentId": 102, "thumbnailPath": "${process.env.NODE_SERVER_API_HOST}/resources/attachments/thumbnail/2020/f.png"}]
|Great Product | 5 | XYZ | 2020-11-04| [{"filePath": "${process.env.NODE_SERVER_API_HOST}/resources/attachments/2020/d.png", "attachmentId": 101, "thumbnailPath": "${process.env.NODE_SERVER_API_HOST}/resources/attachments/thumbnail/2020/d.png"}]
|Great Product | 5 | XYZ | 2020-11-04| [{"filePath": "${process.env.NODE_SERVER_API_HOST}/resources/attachments/2020/g.png", "attachmentId": 100, "thumbnailPath": "${process.env.NODE_SERVER_API_HOST}/resources/attachments/thumbnail/2020/g.png"}]
|Good Product | 4 | ABC | 2020-11-04| null
|Bad Product | 1 | EVE | 2020-11-04| null
Desired output:
|review |rating| name | createdAt | reviewAttachments |
|:---- |:----:| -----:| ---------:| ----------------:
|Great Product | 5 | XYZ | 2020-11-04| [{"filePath": "${process.env.NODE_SERVER_API_HOST}/resources/attachments/2020/f.png", "attachmentId": 102, "thumbnailPath": "${process.env.NODE_SERVER_API_HOST}/resources/attachments/thumbnail/2020/f.png"}, {"filePath": "${process.env.NODE_SERVER_API_HOST}/resources/attachments/2020/d.png", "attachmentId": 101, "thumbnailPath": "${process.env.NODE_SERVER_API_HOST}/resources/attachments/thumbnail/2020/d.png"}, {"filePath": "${process.env.NODE_SERVER_API_HOST}/resources/attachments/2020/g.png", "attachmentId": 100, "thumbnailPath": "${process.env.NODE_SERVER_API_HOST}/resources/attachments/thumbnail/2020/g.png"}]
|Good Product | 4 | ABC | 2020-11-04| null
|Bad Product | 1 | EVE | 2020-11-04| null
Notice: The above row with rating 5 is returning array of reviewAttachments associated with that review. My output is returning each row with duplicate entries of that rating with different review attachment.
select
review, rating, u.name,
(SELECT
JSON_ARRAYAGG(JSON_OBJECT("attachmentId", attachments.id,
'filePath', TRIM(CONCAT("${process.env.NODE_SERVER_API_HOST}", '/',attachments.filePath)),
'thumbnailPath', TRIM(CONCAT("${process.env.NODE_SERVER_API_HOST}", '/',attachments.thumbnailPath))
))
FROM review_attachments ra
left join attachments on attachments.id = ra.attachment_id
WHERE ra.review_id = reviews.id) as reviewAttachments
from reviews
left join users as u on u.id = reviews.user_id
where productId = 1
and reviews.review is not null
limit 0,5

Get Users available based on validation of status, id, and time spread across 3 tables

Lets say I want to pull a list of users(From TableA) who:
are currently within Status = 2
and
currently not in TableB,
OR
are in TableB.UserId AND (TableB.AppId > 1year) by comparing the dates between TableC.id's CreatedDate and with the CurrentDate.
Current Schema Setup...
TableA (UserID)
--------------------------------------
id | fName | lName | Status | CreatedDate
1 | John | Doe | 2 | 2017-03-02 06:31:15.482
2 | Marry | Jane | 2 | 2017-05-03 16:43:56.937
3 | William | Thompson | 4 | 2017-06-15 13:12:32.219
4 | Timothy | Limmons | 2 | 2017-09-27 01:52:42.842
TableB
--------------------------------------
id | AppID | UserID | CreatedDate
1 | 2 | 1 | 2019-04-16 23:21:56.099
2 | 3 | 4 | 2019-08-03 04:32:18.472
TableC (AppID)
--------------------------------------
id | Title | CreatedDate
1 | ToDo List | 2017-03-09 22:45:12.907
2 | Magic Marshmellows | 2018-11-14 07:01:04.050
3 | Project Falcon | 2019-07-23 14:22:44.837
The info above should pull users from TableA with the id's of 1 and 2.
Marry has not been paired with an App, and is therefor available
John is paired with the App Magic Marshmellows, but the project began over 1 year ago and is therefor available
The following info should NOT pull users with the id's of 3 and 4.
William is a status of 4 (not 2) and is therefor NOT available.
Timothy is paired with the App Project Falcon, and this app began within a year from the current DateTime (12/15/2019)... and is therefor NOT available
I need something like...
Select *
FROM
[TableA] a
WHERE
a.Status = 2
IF
TableB.UserID NOT CONTAINS a.id
ELSE IF
TableB.UserID = a.id
AND WHERE
TableB.AppID = TableC.id
AND WHERE
TableC.CreatedDate is less than 1 year old from Current Date
I'm just not sure how to go about using the right syntax for this. Any help would be appreciated.
P.S. If there is a better title for this complicated question, please let me know.
IN MYSQL you would do a query like this.
CREATE TABLE UserID
(`id` int, `fName` varchar(7), `lName` varchar(8), `Status` int, `CreatedDate` Date)
;
INSERT INTO UserID
(`id`, `fName`, `lName`, `Status`, `CreatedDate`)
VALUES
(1, 'John', 'Doe', 1, '2017-03-02 06:31:15.482'),
(2, 'Marry', 'Jane', 2, '2017-05-03 16:43:56.937'),
(3, 'William', 'Thompson', 4, '2017-06-15 13:12:32.219'),
(4, 'Timothy', 'Limmons', 2, '2017-09-27 01:52:42.842')
;
✓
✓
CREATE TABLE TableB
(`id` int, `AppID` int, `UserID` int, `CreatedDate` Date)
;
INSERT INTO TableB
(`id`, `AppID`, `UserID`, `CreatedDate`)
VALUES
(1, 2, 1, '2019-04-16 23:21:56.099'),
(2, 3, 4, '2019-08-03 04:32:18.472')
;
✓
✓
CREATE TABLE APPID
(`id` int, `Title` varchar(18), `CreatedDate` Date)
;
INSERT INTO APPID
(`id`, `Title`, `CreatedDate`)
VALUES
(1, 'ToDo List', '2017-03-09 22:45:12.907'),
(2, 'Magic Marshmellows', '2018-11-14 07:01:04.050'),
(3, 'Project Falcon', '2019-07-23 14:22:44.837')
;
✓
✓
SELECT u.*
From UserID u LEFT JOIN TableB b ON u.id = b.UserID
LEFT JOIN APPID a ON b.APPID = a.id
WHERE Status = 2
AND (u.id NOT IN (SELECT UserID FROM TableB)
OR (u.id IN (SELECT UserID FROM TableB) AND a.CreatedDate > NOW() - INTERVAL 1 YEAR));
id | fName | lName | Status | CreatedDate
-: | :------ | :------ | -----: | :----------
4 | Timothy | Limmons | 2 | 2017-09-27
2 | Marry | Jane | 2 | 2017-05-03
SELECT * FROM APPID WHERE `CreatedDate` > NOW() - INTERVAL 1 YEAR
id | Title | CreatedDate
-: | :------------- | :----------
3 | Project Falcon | 2019-07-23
db<>fiddle here
SELECT
a.*
FROM
TableA AS a
LEFT JOIN (
SELECT
b.UserID
FROM
TableB AS b
INNER JOIN TableC AS c ON (
b.AppID = c.id
)
WHERE
c.CreatedDate >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
) AS s ON (
a.id = s.UserID
)
WHERE
a.Status = 2
AND s.UserID IS NULL
OR
SELECT
a.*
FROM
TableA AS a
LEFT JOIN TableB AS b ON (
b.UserID = a.id
)
LEFT JOIN TableC AS c ON (
c.id = b.AppID
)
WHERE
a.Status = 2
AND (
b.UserID IS NULL
OR c.CreatedDate < DATE_SUB(NOW(), INTERVAL 1 YEAR)
)
OR
SELECT
a.*
FROM
TableA AS a
WHERE
a.Status = 2
AND NOT EXISTS (
SELECT
*
FROM
TableB AS b
INNER JOIN TableC AS c ON (
b.AppID = c.id
)
WHERE
b.UserID = a.id
AND c.CreatedDate >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
)
=>
id fName lName Status CreatedDate
1 John Doe 2 2017-03-02
2 Marry Jane 2 2017-05-03
CREATE TABLE IF NOT EXISTS `TableA` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`fName` varchar(7) DEFAULT NULL,
`lName` varchar(8) DEFAULT NULL,
`Status` int(11) DEFAULT NULL,
`CreatedDate` date DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `Status` (`Status`,`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
DELETE FROM `TableA`;
INSERT INTO `TableA` (`id`, `fName`, `lName`, `Status`, `CreatedDate`) VALUES
(1, 'John', 'Doe', 2, '2017-03-02'),
(2, 'Marry', 'Jane', 2, '2017-05-03'),
(3, 'William', 'Thompson', 4, '2017-06-15'),
(4, 'Timothy', 'Limmons', 2, '2017-09-27');
CREATE TABLE IF NOT EXISTS `TableB` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`AppID` int(11) DEFAULT NULL,
`UserID` int(11) DEFAULT NULL,
`CreatedDate` date DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `AppID` (`AppID`),
KEY `UserID` (`UserID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
DELETE FROM `TableB`;
INSERT INTO `TableB` (`id`, `AppID`, `UserID`, `CreatedDate`) VALUES
(1, 2, 1, '2019-04-16'),
(2, 3, 4, '2019-08-03');
CREATE TABLE IF NOT EXISTS `TableC` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Title` varchar(18) DEFAULT NULL,
`CreatedDate` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `CreatedDate` (`CreatedDate`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
DELETE FROM `TableC`;
INSERT INTO `TableC` (`id`, `Title`, `CreatedDate`) VALUES
(1, 'ToDo List', '2017-03-09 22:45:12'),
(2, 'Magic Marshmellows', '2018-11-14 07:01:04'),
(3, 'Project Falcon', '2019-07-23 14:22:44');
UPDATED: WHERE condition fixed

Double GroupBy with Count and Dates Returns Wrong Dates

I have three tables to keep track of emails and their assigned categories: Email keeps the mail's content, Category lists the categories and Classification links an Email entry ID with a Category entry ID. Schema with sample data and query is available on SQLFiddle: http://sqlfiddle.com/#!9/a410a6/26/0
CREATE TABLE `Category` (
`id` int(6) unsigned NOT NULL,
`name` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
CREATE TABLE `Mail` (
`id` int(6) unsigned NOT NULL,
`content` varchar(500) NOT NULL,
`date` datetime NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
CREATE TABLE `Classification` (
`id` int(6) unsigned NOT NULL,
`mail_id` int(6) unsigned NOT NULL,
`category_id` int(6) unsigned NOT NULL,
FOREIGN KEY (mail_id) REFERENCES Mail(id),
FOREIGN KEY (category_id) REFERENCES Category(id),
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `Category` (`id`, `name`) VALUES
('1', 'Important'),
('2', 'Urgent'),
('3', 'Normal');
INSERT INTO `Mail` (`id`, `content`, `date`) VALUES
('1', 'Important Email', '2019-01-04T13:53:52'),
('2', 'Urgent Email', '2019-01-19T13:53:52'),
('3', 'Very Urgent Email', '2019-01-24T13:53:52'),
('4', 'Quite Urgent Email', '2019-01-24T13:53:52'),
('5', 'Normal Email', '2019-01-21T13:53:52'),
('6', 'Regular Email', '2019-01-14T13:53:52'),
('7', 'Regular Email', '2019-01-23T13:53:52'),
('8', 'Regular Email', '2019-01-23T13:53:52'),
('9', 'Regular Email', '2019-01-20T13:53:52'),
('10', 'Very Urgent Email', '2019-01-25T13:53:52'),
('11', 'Very Urgent Email', '2019-01-25T13:53:52');
INSERT INTO `Classification` (`id`, `mail_id`, `category_id`) VALUES
('1', '1', '1'),
('2', '2', '2'),
('3', '3', '2'),
('4', '4', '2'),
('5', '5', '3'),
('6', '6', '3'),
('7', '6', '3'),
('8', '6', '3'),
('9', '6', '3'),
('10', '6', '2'),
('11', '6', '2');
I want to return the number of mails received for each category for each date recorded, i.e. my expected results would be
+----------------------+-----------+----------+
| date | name | count(*) |
+----------------------+-----------+----------+
| 2019-01-04T13:53:52Z | Important | 1 |
| 2019-01-14T13:53:52Z | Normal | 1 |
| 2019-01-19T13:53:52Z | Urgent | 1 |
| 2019-01-20T13:53:52Z | Normal | 1 |
| 2019-01-21T13:53:52Z | Normal | 1 |
| 2019-01-23T13:53:52Z | Normal | 2 |
| 2019-01-24T13:53:52Z | Urgent | 1 |
| 2019-01-25T13:53:52Z | Urgent | 2 |
+----------------------+-----------+----------+
To do so I run the following query with a double groupby, filtering on the Classification table:
SELECT Mail.date, Category.name, count(*) FROM Mail, Classification, Category WHERE Category.id = Classification.category_id AND Classification.mail_id = Mail.id GROUP BY Mail.date, Category.name
Which gives me the following results:
+----------------------+-----------+----------+
| date | name | count(*) |
+----------------------+-----------+----------+
| 2019-01-04T13:53:52Z | Important | 1 |
| 2019-01-14T13:53:52Z | Normal | 4 |
| 2019-01-14T13:53:52Z | Urgent | 2 |
| 2019-01-19T13:53:52Z | Urgent | 1 |
| 2019-01-21T13:53:52Z | Normal | 1 |
| 2019-01-24T13:53:52Z | Urgent | 2 |
+----------------------+-----------+----------+
Which is entirely wrong.
I've tried substituting the WHERE statement for a JOIN:
SELECT Mail.date, Category.name, count(*) FROM (Mail, Category) RIGHT JOIN Classification ON Category.id = Classification.category_id AND Classification.mail_id = Mail.id GROUP BY Mail.date, Category.name `
But I get the exact same results as above.
Why are those queries returning these erroneous results and what should I do to fix them ?
First, your query should look like this:
SELECT m.date, c.name, count(*)
FROM Mail m JOIN
Classification cl
ON cl.mail_id = m.id JOIN
Category c
ON c.id = cl.category_id
GROUP BY m.date, c.name ;
Now that we have gotten that out of the way, your problem is that emails have multiple categories. So, they are multiply counted. Hence, the results you are getting are correct.
You have exact duplicates in the classification table, so a simple solution is:
SELECT m.date, c.name, count(distinct m.id)
FROM Mail m JOIN
Classification cl
ON cl.mail_id = m.id JOIN
Category c
ON c.id = cl.category_id
GROUP BY m.date, c.name ;
That said, the real solution is to fix your data, so it doesn't have duplicates.
Here is the SQL Fiddle using your data. You have a "2" for emails on 2019-01-23. However, there are no classified emails on that date, so they are not in the results.

Getting stock balance

I have 3 tables as follows:
products:[product_id,product_name]
stock:[product_id,date,qty_received]
sales:[product_id,date,qty_sold]
I'm trying to display the balance of all products by subtracting qty_received - qty_sold.
My query is:
SELECT DISTINCT p.product_id, p.product_name,
IFNULL(sum(qty_received),0)-IFNULL(sum(qty_sold),0) as balance FROM products p
LEFT JOIN stock st ON (p.product_id=st.product_id)
LEFT JOIN sales sa ON (p.product_id=sa.product_id)
GROUP BY p.product_id
With the above query I'm not getting the correct balance.
Could you please help.
Additional Information:
Following are a simplified structure for my tables
Products table:
CREATE TABLE IF NOT EXISTS `products` (
`product_id` varchar(15) NOT NULL,
`product_name` varchar(20) NOT NULL,
UNIQUE KEY `product_id` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- Dumping data for table products
INSERT INTO `products` (`product_id`, `product_name`) VALUES
('111', 'Product One'),
('222', 'Product Two'),
('333', 'Product Three'),
('444', 'Product Four');
Stock table:
CREATE TABLE IF NOT EXISTS `stock` (
`stock_id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL,
`qty_received` int(11) NOT NULL,
PRIMARY KEY (`stock_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=112 ;
-- Dumping data for table stock
INSERT INTO `stock` (`stock_id`, `product_id`, `qty_received`) VALUES
(1, 111, 5),
(2, 222, 10),
(3, 333, 4),
(4, 444, 6),
(5, 111, 2),
(6, 222, 7),
(7, 111, 3),
(8, 111, 3);
Sales Table
CREATE TABLE IF NOT EXISTS `sales` (
`sales_id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL,
`qty_sold` int(11) NOT NULL,
PRIMARY KEY (`sales_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
-- Dumping data for table sales
INSERT INTO `sales` (`sales_id`, `product_id`, `qty_sold`) VALUES
(1, 111, -1),
(2, 222, -3),
(3, 333, -2),
(4, 111, -3),
(5, 222, -1),
(6, 222, -4);
The Result I'm getting is:
+------------+--------------+---------+
| product_id | product_name | balance |
+------------+--------------+---------+
| 111 | Product One | 10 |
| 222 | Product Two | 35 |
| 333 | Product Three| 2 |
| 444 | Product Four | 6 |
+------------+--------------+---------+
Correct balance I'm expecting is
+------------+--------------+---------+
| product_id | product_name | balance |
+------------+--------------+---------+
| 111 | Product One | 9 |
| 222 | Product Two | 9 |
| 333 | Product Three| 2 |
| 444 | Product Four | 6 |
+------------+--------------+---------+
Finally got a working sql for this solution , but not sure if this is the best way doing this. I'm very concerned about the performance, since these tables got large amount of records.
SELECT p.product_id,p.product_name,
IFNULL(sum(st.qty_received),0)+IFNULL(sold,0) as balance FROM products p
LEFT JOIN stock st ON (p.product_id=st.product_id)
LEFT JOIN (SELECT sa.product_id,sum(sa.qty_sold) as sold FROM sales sa
GROUP BY sa.product_id) as t1 ON (t1.product_id=p.product_id)
GROUP BY product_id
Anyone can suggest a better sql (or change in schema) for better speed/performance to get stock balance?
First, add p.product_name to GROUP BY clause.
Second, remove DISTINCT - it's unnecessary with GROUP BY.
UPDATE
I've tried a couple of queries and found that this one looks like the simplest:
SELECT
p.product_id,
p.product_name,
COALESCE(SUM(qty), 0) as balance
FROM products p
LEFT JOIN (
SELECT product_id, qty_received AS qty FROM stock st UNION ALL
SELECT product_id, qty_sold FROM sales st
) AS T
ON p.product_id = T.product_id
GROUP BY
p.product_id,
p.product_name
Also, I think it would be a good idea to make data type of product_id in all your tables the same. It's varchar in products and int in sales/stock.
I did a Fiddle for you