Related
I have a table named 'reports', something like this:
id
user_id
type
customer_id
text
created_at
1
1
1
3
....
2021-08-07 17:00:52
2
1
1
3
....
2021-08-12 10:11:11
3
1
1
9
....
2021-08-12 10:08:14
4
1
2
3
....
2021-08-12 10:04:08
5
1
2
9
....
2021-08-13 20:32:21
6
2
1
7
....
2021-08-13 20:34:17
7
2
2
8
....
2021-08-14 18:55:09
I want to get the count of rows that a user has submitted type 1 reports that result in a type 2 report.
Type 1 report means reporting a phone call to the customer and type 2 means meeting the customer. I want to receive the number of calls that resulted in an meeting.
For example, for user 1 should returns 3, because for customer 3, IDs 1 and 2 have led to ID 4, and for customer 9, ID 3 has led to ID 5.
But for user 2, ID 7 is type 2 but there is no previous type 1 report for customer 8, so it returns 0.
Schema (MySQL v5.7)
CREATE TABLE reports
(id int auto_increment primary key,
user_id int,
type int,
customer_id int,
text varchar(4),
created_at varchar(19)
);
INSERT INTO reports
(id, user_id, type, customer_id, text, created_at)
VALUES
(1, 1, 1, 3, '....', '2021-08-07 17:00:52'),
(2, 1, 1, 3, '....', '2021-08-12 10:11:11'),
(3, 1, 1, 9, '....', '2021-08-12 10:08:14'),
(4, 1, 2, 3, '....', '2021-08-12 10:04:08'),
(5, 1, 2, 9, '....', '2021-08-13 20:32:21'),
(6, 2, 1, 7, '....', '2021-08-13 20:34:17'),
(7, 2, 2, 8, '....', '2021-08-14 18:55:09');
Query #1
SELECT x.user_id
, COUNT(DISTINCT y.id) total
FROM reports x
LEFT
JOIN reports y
ON y.id<=x.id
AND y.user_id = x.user_id
AND y.customer_id = x.customer_id
AND y.type = 1
WHERE x.type = 2
GROUP
BY x.user_id;
user_id
total
1
3
2
0
View on DB Fiddle
You haven't shown how your are expecting results, but simply refer to User 1 and 2 and the total votes, you can try using a lateral join
select user_id, Coalesce(Sum(valid),0) rowcount
from reports r
join lateral (
select
case when exists (select * from reports rr
where rr.user_id=r.user_Id and rr.type=2 and rr.customer_id=r.customer_Id and r.type=1)
then 1 end valid
)x
group by User_Id
Example Fiddle
First you Count the number of customers that comply with your criterioa type and typoe 2. But ti get all user you need to join the list of users
I used LEFT join as also RIGHt JOIN in my example
CREATE TABLE reports
(`id` int, `user_id` int, `type` int, `customer_id` int, `text` varchar(4), `created_at` varchar(19))
;
INSERT INTO reports
(`id`, `user_id`, `type`, `customer_id`, `text`, `created_at`)
VALUES
(1, 1, 1, 3, '....', '2021-08-07 17:00:52'),
(2, 1, 1, 3, '....', '2021-08-12 10:11:11'),
(3, 1, 1, 9, '....', '2021-08-12 10:08:14'),
(4, 1, 2, 3, '....', '2021-08-12 10:04:08'),
(5, 1, 2, 9, '....', '2021-08-13 20:32:21'),
(6, 2, 1, 7, '....', '2021-08-13 20:34:17'),
(7, 2, 2, 8, '....', '2021-08-14 18:55:09')
;
SELECT IFNULL(Counts,0),t1.user_id
FROM (SELECT COUNT(distinct r.customer_id) counts,user_id
FROM reports r
WHERE type = 1 AND EXISTS(SELECT 1 FROM reports WHERE type = 2 AND customer_id = r.customer_id)
GROUP BY user_id) r RIGHT JOIN (SELECT DISTINCT user_id FROm reports) t1 ON r.user_id = t1.user_id
IFNULL(Counts,0) | user_id
---------------: | ------:
2 | 1
0 | 2
SELECT IFNULL(Counts,0),t1.user_id
FROM (SELECT DISTINCT user_id FROm reports) t1 LEFT JOIN (SELECT COUNT(distinct r.customer_id) counts,user_id
FROM reports r
WHERE type = 1 AND EXISTS(SELECT 1 FROM reports WHERE type = 2 AND customer_id = r.customer_id)
GROUP BY user_id) r ON r.user_id = t1.user_id
IFNULL(Counts,0) | user_id
---------------: | ------:
2 | 1
0 | 2
db<>fiddle here
MySQL Server version: 5.5.41-0+wheezy1 - (Debian)
I've got a MySQL issue that is almost a year old. Have tried a few freelancers and while we got close, it was never totally solved.
I need to find the most recent outbound shipment for every product we have. And in the same row of results, the specific lot (including lot number and expiration date) of the product that was involved in that inventory adjustment.
The most recent version of this query is almost working but it has one fatal flaw (at least one). If the product only ever had one outbound shipment, it won't show up in the results. I'm assuming that is because on line 30 where a comparison is done, which looks like this
AND liai0.LocationInventoryAdjustmentItemID > liai.LocationInventoryAdjustmentItemID
I'm assuming it is because the one and only adjustment for a product couldn't be greater than itself, it fails to be considered by the where clause. I've tested this by adding a second, fake outbound shipment, for a missing product. The query then returns the missing product.
That isn't the only line with a comparison like that. Perhaps the same problem could happen if there was only ever one location for a given product (line 23) and perhaps if there was only ever one lot for a product (line 16).
Here is the latest version of the query:
SELECT
p.`ProductID`,
p.`ProductName`,
lot.`ProductLotID`,
lot.`ExpirationDate`,
lot.`LotNumber`,
lia.`LocationInventoryAdjustmentID`,
lia.`Created`,
liai.`count`
FROM products AS p
INNER JOIN product_lots AS lot ON lot.ProductID = p.ProductID
AND NOT EXISTS (
SELECT 1
FROM product_lots AS lot0
WHERE lot0.ProductID = lot.ProductID
AND lot0.ProductLotID > lot.ProductLotID
)
INNER JOIN product_locations AS pl ON pl.ProductLotID = lot.ProductLotID
AND NOT EXISTS (
SELECT 1
FROM product_locations AS pl0
WHERE pl0.ProductLotID = pl.ProductLotID
AND pl0.ProductLocationID > pl.ProductLocationID
)
INNER JOIN location_inventory_adjustment_items AS liai ON liai.ProductLocationID = pl.ProductLocationID
AND NOT EXISTS (
SELECT 1
FROM location_inventory_adjustment_items AS liai0
WHERE liai0.ProductLocationID = liai.ProductLocationID
AND liai0.LocationInventoryAdjustmentItemID > liai.LocationInventoryAdjustmentItemID
)
INNER JOIN location_inventory_adjustments AS lia ON lia.LocationInventoryAdjustmentID = liai.LocationInventoryAdjustmentID
INNER JOIN location_inventory_adjustment_reasons AS liar ON liar.ReasonID = lia.ReasonID
WHERE liar.`Name` LIKE '%Out-Bound%'
ORDER BY p.ProductID ASC, liai.LocationInventoryAdjustmentID
And now the tables
Table products
ProductID ProductName
1 Banana
2 Apple
3 Orange
4 Shirt
Table product_lots
ProductLotID ProductID ExpirationDate LotNumber
20 1 2022-01-01 Chikita22
21 3 2023-12-01 Florida-9
22 4 NULL HANES-001
23 1 2024-01-01 Chikita24
Table product_locations
ProductLocationID ProductLotID LocationCode LocationType
30 20 A-01-01-01 1
31 21 A-02-01-01 1
32 22 12-03-01-01 2
33 23 A-01-01-01 1
Table location_inventory_adjustment_items
LocationInventoryAdjustmentItemID LocationInventoryAdjustmentID ProductLocationID Count
40 50 30 100
41 51 31 200
42 52 32 300
43 53 33 150
Table location_inventory_adjustments
LocationInventoryAdjustmentID LocationTypeID ReasonID Created
50 1 4 2020-01-01
51 1 4 2020-05-01
52 2 4 2020-06-01
53 1 4 2020-07-07
Table location_inventory_adjustment_reasons
ReasonID Name Description
1 Discarded Product Trash
2 In-Bound Shipment Delivery
3 Out-Bound Shipment Product was shipped out
4 Out-Bound Shipment - FBA US Product was shipped out to an Amazon fulfillment center in the US
5 Out-Bound Shipment - FBA UK Product was shipped out to an Amazon fulfillment center in the UK
Now that the table structure is added, only the product Banana would be returned by the query since it is the only product to be involved in more than one inventory adjustment (more than one outbound shipment).
What can I do to fix this bug (in my query, obviously not a bug in MySQL)
Ican't really explain it it must be one or other restricted wording.
But try, at least mysql 8 workbench doesn't mind
CREATE TABLE products
(`ProductID` int, `ProductName` varchar(6))
;
INSERT INTO products
(`ProductID`, `ProductName`)
VALUES
(1, 'Banana'),
(2, 'Apple'),
(3, 'Orange'),
(4, 'Shirt')
;
✓
✓
CREATE TABLE product_lots
(`ProductLotID` int, `ProductID` int, `ExpirationDate` varchar(10), `LotNumber` varchar(9))
;
INSERT INTO product_lots
(`ProductLotID`, `ProductID`, `ExpirationDate`, `LotNumber`)
VALUES
(20, 1, '2022-01-01', 'Chikita22'),
(21, 3, '2023-12-01', 'Florida-9'),
(22, 4, NULL, 'HANES-001'),
(23, 1, '2024-01-01', 'Chikita24')
;
✓
✓
CREATE TABLE product_locations
(`ProductLocationID` int, `ProductLotID` int, `LocationCode` varchar(11), `LocationType` int)
;
INSERT INTO product_locations
(`ProductLocationID`, `ProductLotID`, `LocationCode`, `LocationType`)
VALUES
(30, 20, 'A-01-01-01', 1),
(31, 21, 'A-02-01-01', 1),
(32, 22, '12-03-01-01', 2),
(33, 23, 'A-01-01-01', 1)
;
✓
✓
CREATE TABLE location_inventory_adjustment_items
(`LocationInventoryAdjustmentItemID` int, `LocationInventoryAdjustmentID` int, `ProductLocationID` int, `Count` int)
;
INSERT INTO location_inventory_adjustment_items
(`LocationInventoryAdjustmentItemID`, `LocationInventoryAdjustmentID`, `ProductLocationID`, `Count`)
VALUES
(40, 50, 30, 100),
(41, 51, 31, 200),
(42, 52, 32, 300),
(43, 53, 33, 150)
;
✓
✓
CREATE TABLE location_inventory_adjustments
(`LocationInventoryAdjustmentID` int, `LocationTypeID` int, `ReasonID` int, `Created` date)
;
INSERT INTO location_inventory_adjustments
(`LocationInventoryAdjustmentID`, `LocationTypeID`, `ReasonID`, `Created`)
VALUES
(50, 1, 4, '2020-01-01'),
(51, 1, 4, '2020-05-01'),
(52, 2, 4, '2020-06-01'),
(53, 1, 4, '2020-07-07')
;
✓
✓
CREATE TABLE location_inventory_adjustment_reasons
(`ReasonID` int, `Name` varchar(27), `Description` varchar(65))
;
INSERT INTO location_inventory_adjustment_reasons
(`ReasonID`, `Name`, `Description`)
VALUES
(1, 'Discarded Product', 'Trash'),
(2, 'In-Bound Shipment', 'Delivery'),
(3, 'Out-Bound Shipment', 'Product was shipped out'),
(4, 'Out-Bound Shipment - FBA US', 'Product was shipped out to an Amazon fulfillment center in the US'),
(5, 'Out-Bound Shipment - FBA UK', 'Product was shipped out to an Amazon fulfillment center in the U')
;
✓
✓
SELECT
p.`ProductID`,
p.`ProductName`,
lot.`ProductLotID`,
lot.`ExpirationDate`,
lot.`LotNumber`,
lia.`LocationInventoryAdjustmentID`,
lia.`Created`,
liai.`count`
FROM products AS p
INNER JOIN product_lots AS lot ON lot.ProductID = p.ProductID
AND NOT EXISTS (
SELECT 1
FROM product_lots AS lot0
WHERE lot0.ProductID = lot.ProductID
AND lot0.ProductLotID > lot.ProductLotID
)
INNER JOIN product_locations AS pl ON pl.ProductLotID = lot.ProductLotID
AND NOT EXISTS (
SELECT 1
FROM product_locations AS pl0
WHERE pl0.`ProductLotID` = pl.`ProductLotID`
AND pl0.ProductLocationID > pl.ProductLocationID
)
INNER JOIN location_inventory_adjustment_items AS liai ON liai.ProductLocationID = pl.ProductLocationID
AND NOT EXISTS (
SELECT 1
FROM location_inventory_adjustment_items AS liai0
WHERE liai0.ProductLocationID = liai.ProductLocationID
AND liai0.`LocationInventoryAdjustmentItemID` > liai.`LocationInventoryAdjustmentItemID`
)
INNER JOIN location_inventory_adjustments AS lia ON lia.LocationInventoryAdjustmentID = liai.LocationInventoryAdjustmentID
INNER JOIN location_inventory_adjustment_reasons AS liar ON liar.ReasonID = lia.ReasonID
WHERE liar.`Name` LIKE '%Out-Bound%'
ORDER BY p.ProductID ASC, liai.LocationInventoryAdjustmentID
ProductID | ProductName | ProductLotID | ExpirationDate | LotNumber | LocationInventoryAdjustmentID | Created | count
--------: | :---------- | -----------: | :------------- | :-------- | ----------------------------: | :--------- | ----:
1 | Banana | 23 | 2024-01-01 | Chikita24 | 53 | 2020-07-07 | 150
3 | Orange | 21 | 2023-12-01 | Florida-9 | 51 | 2020-05-01 | 200
4 | Shirt | 22 | null | HANES-001 | 52 | 2020-06-01 | 300
db<>fiddle here
I have a MySQL DB that defines series of numbers within sets as such:
set item1 item2
1 1 2
1 2 3
1 3 4
1 4 5
1 5 6
I want to write a query (or queries) that returns to me the fact that set 1 is a series of numbers that spans from 1 to 6. Is this possible?
Please note that the real DB I'm dealing with contains hundreds of sets and that each set can contain a series of items that can be somewhat long as well (up to 50 items per set, I'm guessing). Also, I'm not totally sure, but the DB might also have cases where the series of numbers split. Using the example above, there may be instances like the following:
set item1 item2
1 1 2
1 2 3
1 3 4
1 4 5
1 5 6
1 3 7
1 7 8
1 8 9
In which case, I'd want to know that set 1 has two series of numbers: [1, 2, 3, 4, 5, 6] and [1, 2, 3, 7, 8, 9]. Is this possible with hopefully one query (or if necessary, multiple queries)?
Edit: Please note that I used the numbers 1-9 in sequential order to make the question easier to understand. The real data is much more mixed up and not that orderly.
As you are aware, MySQL cannot handle recursion 'out-of-the-box', so options include:
writing a stored procedure
switching from an adjacency list to an alternative model (e.g. nested set)
joining the table to itself as often as could be required
handling the recursion in application level code (e.g. a bit of PHP)
Here is an example using option 3, but it could be easily adapted to suit option 4...
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(
family_id INT NOT NULL,
item_id INT NOT NULL,
parent_id INT NULL,
PRIMARY KEY(family_id, item_id)
);
INSERT INTO my_table
VALUES (101, 1, null), (101, 2, 1), (101, 3, 2), (101, 4, 3),
(101, 5, 4), (101, 6, 5), (101, 7, 3), (101, 8, 7), (101, 9, 8);
SELECT CONCAT_WS(','
, a.item_id
, b.item_id
, c.item_id
, d.item_id
, e.item_id
, f.item_id
, g.item_id
, h.item_id
, i.item_id
) series
FROM
my_table a
LEFT JOIN
my_table b ON b.parent_id = a.item_id AND b.family_id = a.family_id
LEFT JOIN
my_table c ON c.parent_id = b.item_id AND c.family_id = b.family_id
LEFT JOIN
my_table d ON d.parent_id = c.item_id AND d.family_id = c.family_id
LEFT JOIN
my_table e ON e.parent_id = d.item_id AND e.family_id = d.family_id
LEFT JOIN
my_table f ON f.parent_id = e.item_id AND f.family_id = e.family_id
LEFT JOIN
my_table g ON g.parent_id = f.item_id AND g.family_id = f.family_id
LEFT JOIN
my_table h ON h.parent_id = g.item_id AND h.family_id = g.family_id
LEFT JOIN
my_table i ON i.parent_id = h.item_id AND i.family_id = h.family_id
WHERE
a.parent_id IS NULL;
+-------------+
| series |
+-------------+
| 1,2,3,4,5,6 |
| 1,2,3,7,8,9 |
+-------------+
I can solve the first problem.
Note that "set" is a keyword, so I renamed the first column to "sset"
You can see the result in http://sqlfiddle.com/#!9/ef6360/5
Create table and insert data:
create table test
(
sset int not null
, item1 int not null
, item2 int not null
) engine=InnoDB;
insert into test
values
(1, 1, 2)
, (1, 2, 3)
, (1, 3, 4)
, (1, 4, 5)
, (1, 5, 6)
Run the query:
select
sset
, group_concat(distinct item1or2 order by item1or2 asc)
from
(
select
sset
, item1 as item1or2
from test
union all
select
sset
, item2 as item1or2
from test
) u;
The output is:
1,2,3,4,5,6
I have the following tables:
user
userId name
1 Sam
2 Harold
3 John
othertable
id id2 number
1 111 12
1 222 23
1 333 33
2 111 12
2 444 11
3 555 12
3 222 44
The user table's userId matches the othertable's id column. Ideally I'd like to join the content of othertable to user depending on how many rows are present for that id. That's the output I'm aiming for:
e.g.
user
userId name 111 222 333 444 555
1 Sam 12 12 33
2 Harold 12 11
3 John 44 12
Any idea?
Update: The id2's values are limited. Only valid values 111, 222, 333, 444 and 555.
you may try out this 1... not sure if it meet you requirement...
CREATE TABLE users
(
userId int,
name varchar(max)
)
INSERT INTO USERS VALUES
(1, 'Sam'),
(2, 'Harold'),
(3, 'John')
CREATE TABLE othertable
(
id int,
id2 int,
number int
)
INSERT INTO othertable VALUES
(1, 111, 12),
(1, 222, 23),
(1, 333, 33),
(2, 111, 12),
(2, 444, 11),
(3, 555, 12),
(3, 222, 44)
SELECT
u.userId,
u.name,
SUM(CASE WHEN (id2=111) THEN number ELSE 0 END) AS [111],
SUM(CASE WHEN (id2=222) THEN number ELSE 0 END) AS [222],
SUM(CASE WHEN (id2=333) THEN number ELSE 0 END) AS [333],
SUM(CASE WHEN (id2=444) THEN number ELSE 0 END) AS [444],
SUM(CASE WHEN (id2=555) THEN number ELSE 0 END) AS [555]
FROM othertable o
INNER JOIN users u ON o.id = u.userId
GROUP BY u.userId, u.name
Please have a try. :)
UPDATE: Sorry, I'm not really familiar with MySQL. But I did tried my best
by changing the query into subquery and hope this can help you. If
this doesn't meet you requirement, I hope some other people can help
you.
UPDATE 2: Avoid using PIVOT
I am trying to combine data from 3 tables as follows :
SELECT u.USER_ID,u.USERNAME,u.FIRST_NAME,u.LAST_NAME,u.ISACTIVE,
u.ISADMIN, COUNT(m.PURCHASED_ID) AS MOVIES_PURCHASED,
COUNT(r.RENTED_ID) AS MOVIES_RENTED
FROM TBL_USERS AS u
LEFT JOIN TBL_MOVIE_PURCHASED AS m
ON u.USER_ID = m.USER_ID
LEFT JOIN TBL_RENTED_MOVIES AS r
ON u.USER_ID = r.USER_ID
GROUP BY u.USER_ID,u.USERNAME,u.FIRST_NAME,u.LAST_NAME,u.ISACTIVE,u.ISADMIN
ORDER BY MOVIES_PURCHASED, MOVIES_RENTED;
The tables have the following columns :
TBL_USERS :
USER_ID, USERNAME, FIRSTNAME, LASTNAME, ISACTIVE, ISADMIN
TBL_MOVIE_PURCHASED :
USER_ID, MOVIE_ID, PURCHASE_ID, PURCHASED_ON (purchase ID is unique)
TBL_RENTED_MOVIES :
USER_ID, MOVIE_ID, RENTED_ID, RENTED_ON (rented ID is unique )
I am trying to display :
1. all contents of TBL_USERS
2. the count of the rented movies for every user from TBL_RENTED_MOVIES,
3. the count of the purchased movies for every user from TBL_MOVIES_PURCHASED.
4. ORDER BY the results based on both COUNTS.
If say user1 has rented 5 movies and purchased 10 movies, then the query is supposed to return :
(MOVIES_PURCHASED, MOVIES_RENTED) = (5,10).
Instead, the query returns 5*10 = 50 for both columns :
(MOVIES_PURCHASED, MOVIES_RENTED) = (50,50)
I know I have gone wrong in joining the results. I tried using UNION to combine results as well but didnt work. Any ideas?
Hence, The output should Ideally be :
USER_ID, USERNAME, FIRST_NAME, LAST_NAME, ISACTIVE, ISADMIN, MOVIES_PURCHASED, MOVIES-RENTED :
1, user1, userFirst, userLast, Active, NotAdmin, 5, 10
. Any help is appreciated.
Here's one way...
CREATE table USERS(USER_ID integer primary key,
USERNAME varchar(50), FIRST_NAME varchar(50),
LAST_NAME varchar(50), ISACTIVE VARCHAR(10)
DEFAULT '0',
ISADMIN VARCHAR(10) DEFAULT '0');
CREATE table MOVIES_PURCHASED(PURCHASE_ID integer primary key,
USER_ID integer,
FOREIGN KEY(USER_ID) REFERENCES USERS(USER_ID),
PURCHASED_ON VARCHAR(50));
CREATE table MOVIES_RENTED(RENTED_ID integer primary key,
USER_ID integer,
FOREIGN KEY(USER_ID) REFERENCES USERS(USER_ID),
RENTED_ON VARCHAR(50));
INSERT INTO USERS (USER_ID, USERNAME, FIRST_NAME, LAST_NAME, ISACTIVE, ISADMIN)
VALUES
(1, 'user1', 'user1FN', 'user1LN', '1', '0'),
(2, 'user2', 'user2FN', 'user2LN', '1', '0'),
(3, 'user3', 'user3FN', 'user3LN', '1', '0'),
(4, 'user4', 'user4FN', 'user4LN', '1', '0'),
(5, 'user5', 'user5FN', 'user5LN', '1', '0');
insert into MOVIES_RENTED (RENTED_ID, USER_ID, RENTED_ON)
values
(1, 5 ,'2014-07-05'),
(2, 4 ,'2014-07-05'),
(3, 5 ,'2014-07-05'),
(4, 4 ,'2014-07-05'),
(5, 5 ,'2014-07-05'),
(6, 5 ,'2014-07-05'),
(7, 3 ,'2014-07-05'),
(8, 2 ,'2014-07-05'),
(9, 2 ,'2014-07-05'),
(10, 1 ,'2014-07-05');
insert into MOVIES_PURCHASED (PURCHASE_ID, USER_ID, PURCHASED_ON)
values
(1, 1 ,'2014-07-05'),
(2, 3 ,'2014-07-05'),
(3, 3 ,'2014-07-05'),
(4, 3 ,'2014-07-05'),
(5, 4 ,'2014-07-05'),
(6, 4 ,'2014-07-05'),
(7, 5 ,'2014-07-05'),
(8, 5 ,'2014-07-05'),
(9, 1 ,'2014-07-05'),
(10, 2 ,'2014-07-05'),
(11, 2 ,'2014-07-05'),
(12, 2 ,'2014-07-05');
SELECT u.*
, SUM(x.source = 'rented') rented
, SUM(x.source = 'purchased') purchased
, COUNT(x.source) total
FROM users u
LEFT
JOIN
( SELECT 'rented' source, user_id FROM movies_rented
UNION ALL
SELECT 'purchased', user_id FROM movies_purchased
) x
ON x.user_id = u.user_id
GROUP
BY user_id
ORDER
BY total DESC;
USER_ID USERNAME FIRST_NAME LAST_NAME ISACTIVE ISADMIN RENTED PURCHASED TOTAL
5 user5 user5FN user5LN 1 0 4 2 6
2 user2 user2FN user2LN 1 0 2 3 5
4 user4 user4FN user4LN 1 0 2 2 4
3 user3 user3FN user3LN 1 0 1 3 4
1 user1 user1FN user1LN 1 0 1 2 3
http://sqlfiddle.com/#!2/8b3c8/10
A simple DISTINCT keyword for m.PURCHASE_ID and r.RENTED_ID solves the problem :
the query that worked is :
SELECT u.USER_ID,u.USERNAME,u.FIRST_NAME,u.LAST_NAME,u.ISACTIVE, u.ISADMIN, COUNT(DISTINCT m.PURCHASED_ID) AS MOVIES_PURCHASED, COUNT(DISTINCT r.RENTED_ID) AS MOVIES_RENTED,
(COUNT(DISTINCT m.PURCHASED_ID) + COUNT(DISTINCT r.RENTED_ID)) AS MAX_ACTIVITY
FROM TBL_USERS AS u
LEFT JOIN TBL_MOVIE_PURCHASED AS m
ON u.USER_ID = m.USER_ID
LEFT JOIN TBL_RENTED_MOVIES AS r
ON u.USER_ID = r.USER_ID
GROUP BY u.USER_ID,u.USERNAME,u.FIRST_NAME,u.LAST_NAME,u.ISACTIVE,u.ISADMIN
ORDER BY MAX_ACTIVITY DESC;
I added an extra MAX_ACTIVITY column and ordered the results by max_activity.
the link to an example for creating multiple tables, performing left joins on them, counting values for columns, ordering and grouping them and avoiding redundant data from being displayed while performing the joins :
http://sqlfiddle.com/#!2/7d44a2/2/0