MySQL select to detect mutually exclusive row conflicts within groups - mysql

With the following sample data, I want to create a query that can find where item/desc is duplicate and their respective data fields are not mutually exclusive. Meaning... there can be only 1 data value in column for a given combination of item/desc.
Sample table records:
id | item | desc | data1 | data2 | data3
----+-------+-------+-------+-------+-------
1 | 1 | cat | a | |
2 | 1 | cat | | b |
3 | 1 | cat | | e |
4 | 2 | dog | a | |
5 | 2 | dog | | h | f
6 | 3 | apple | k | | m
7 | 3 | worm | a | g | x
8 | 4 | rock | p | | s
9 | 4 | rock | | | s
10 | 4 | rock | | t | z
Expected query result:
item | desc
-------+-------
1 | cat (because of conflict in data2 with b & e)
4 | rock (because of conflict in data3 with s,s & z

This should work:
select distinct
item,
`desc`
from
table
group by item, `desc`
HAVING count(distinct data1) > 1 or count(distinct data2) > 1 or count(distinct data3) > 1

You could do simple count aggregation and HAVING clause to achieve this.
SELECT `item`,
`desc`,
COUNT(`data1`) AS `data1count`,
COUNT(`data2`) AS `data2count`,
COUNT(`data3`) AS `data3count`
FROM table
GROUP BY `item`, `desc`
HAVING `data1count` > 1
OR `data2count` > 1
OR `data3count` > 1

Related

Joining multiple rows with same ID in one

I am having trouble with an SQL query. I have two tables.
My first table:
+------------+-------------+---------------+
| id_mission | Some column | Other column |
+------------+-------------+---------------+
| 1 | ... | ... |
| 2 | ... | ... |
+------------+-------------+---------------+
My second table:
+------------+-------------+---------+
| id_mission | id_category | points |
+------------+-------------+---------+
| 1 | 1 | 3 |
| 1 | 2 | 4 |
| 1 | 3 | 4 |
| 1 | 4 | 8 |
| 2 | 1 | -4 |
| 2 | 2 | 3 |
| 2 | 3 | 1 |
| 2 | 4 | -7 |
+------------+-------------+---------+
And I would like to have this kind of result with my SELECT request
+------------+-------------+--------------+---------------+----------------+
| id_mission | Some column | Other column | id_category 1 | id_category X |
+------------+-------------+--------------+---------------+----------------+
| 1 | ... | ... | ... | ... |
| 2 | ... | ... | ... | ... |
+------------+-------------+--------------+---------------+----------------+
I have tried this with the first two column but it doesn't work, I also tried GROUP_CONCAT, it works but it's not the result I want.
SELECT m.id_mission ,mc.id_category 1,mc1.id_category 2
from mission m
left join mission_category mc on m.id_mission = mc.id_mission
left join mission_category mc1 on m.id_mission = mc1.id_mission
Can someone help me?
You can use conditional aggregation. Assuming that you want to pivot the points value per category:
select
t1.*,
max(case when t2.id_category = 1 then points end) category_1,
max(case when t2.id_category = 2 then points end) category_2,
max(case when t2.id_category = 3 then points end) category_3
from t1
inner join t2 on t2.id_mission = t1.id_mission
group by t1.id_mission
This assumes that id_mission is the primary key of t1 (else, you need to enumerate the columns you want in both the select and group by clauses).

How to sum up row sizes across multiple tables

In MySQL I have three tables:
A:
-----------------
|g_id | txt |
|------|--------|
| 1 | cat |
| 3 | dog |
and
B:
--------------------------
|g_id | txt | txt2 |
|------|---------|--------
| 1 | hat | chat |
| 3 | that | NULL |
| 3 | that | NULL |
and
C:
------------------
|g_id | txt |
|------|---------|
| 1 | hat |
| 1 | mat |
| 3 | that |
My goal is to sum up the txt column of each row across tables A, B, and C grouped by g_id...
So after my query, the expected result would be:
----------------
|g_id | size |
|------|-------|
| 1 | 16 |
| 3 | 15 |
My query fails:
SELECT
g_id,
SUM(length(txt)) + SUM(length(txt2) as size
FROM ((SELECT a.g_id, a.txt FROM a) UNION ALL
(SELECT b.g_id, b.txt, b.txt2 FROM b) UNION ALL
(SELECT c.g_id, c.txt FROM c)
) abc
GROUP BY g_id;
Error: The used SELECT statements have a different number of columns
Use union all instead of join:
SELECT g_id,
SUM(length(txt)) as size
FROM ((SELECT a.g_id, a.txt FROM a) UNION ALL
(SELECT b.g_id, b.txt FROM b) UNION ALL
(SELECT c.g_id, c.txt FROM c)
) abc
GROUP BY g_id;

Alternate order by logic in MySQL

I'm looking to allow for a custom ordering logic through mySQL that allows the following data set:
+----+-----------------+------------+-------+--+
| ID | item | Popularity | Views | |
+----+-----------------+------------+-------+--+
| 1 | A special place | 3 | 10 | |
| 2 | Another title | 5 | 12 | |
| 3 | Words go here | 1 | 15 | |
| 4 | A wonder | 2 | 8 | |
+----+-----------------+------------+-------+--+
To return an order that alternates, row by row, by popularity and then by views, so the return results look like:
+----+-----------------+------------+-------+--+
| ID | item | Popularity | Views | |
+----+-----------------+------------+-------+--+
| 3 | Words go here | 1 | 15 | |
| 2 | Another title | 5 | 12 | |
| 4 | A wonder | 2 | 8 | |
| 1 | A special place | 3 | 10 | |
+----+-----------------+------------+-------+--+
Where you will see the first row returns the 'most popular', the second row returns the most views, the third row returns the second most popular, and the 4th row returns the 2nd most views.
Currently I'm gathering an entire table through mySQL twice, and then merging these results in PHP. This isn't going to cut it when the database is large. Is this possible in mysql at all?
I guess something along these lines could work. Consider the following:
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,x INT NOT NULL
,y INT NOT NULL
);
INSERT INTO my_table VALUES
(1,3,10),
(2,5,12),
(3,1,15),
(4,2, 8)
(5,4, 1);
We can rank x and y in turn, and then arrange those ranks in a single list - so will have x1,y1,x2,y2,etc - but all rows will appear twice; once for the x rank and once for the y rank...
SELECT * FROM
(
( SELECT a.*, COUNT(*) rank FROM my_table a JOIN my_table b ON b.x <= a.x GROUP BY a.id )
UNION ALL
( SELECT a.*, COUNT(*) rank FROM my_table a JOIN my_table b ON b.y <= a.y GROUP BY a.id )
) n
ORDER BY rank
+----+---+----+------+
| id | x | y | rank |
+----+---+----+------+
| 5 | 4 | 1 | 1 |
| 3 | 1 | 15 | 1 |
| 4 | 2 | 8 | 2 |
| 4 | 2 | 8 | 2 |
| 1 | 3 | 10 | 3 |
| 1 | 3 | 10 | 3 |
| 5 | 4 | 1 | 4 |
| 2 | 5 | 12 | 4 |
| 2 | 5 | 12 | 5 |
| 3 | 1 | 15 | 5 |
+----+---+----+------+
Now we can just grab the lowest rank for each id...
SELECT id
, x
, y
FROM
(
( SELECT a.*, COUNT(*) rank FROM my_table a JOIN my_table b ON b.x <= a.x GROUP BY a.id )
UNION ALL
( SELECT a.*, COUNT(*) rank FROM my_table a JOIN my_table b ON b.y <= a.y GROUP BY a.id )
) m
GROUP
BY id,x,y
ORDER
BY MIN(rank);
+----+---+----+
| id | x | y |
+----+---+----+
| 3 | 1 | 15 |
| 5 | 4 | 1 |
| 4 | 2 | 8 |
| 1 | 3 | 10 |
| 2 | 5 | 12 |
+----+---+----+
Incidentally, this should be faster with variables - but I cannot make that solution work at present - senior moment, perhaps.

SQL, difficult fetching data query

Suppose I have such a table:
+-----+---------+-------+
| ID | TIME | DAY |
+-----+---------+-------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
| 1 | 1 | 2 |
| 2 | 2 | 2 |
| 3 | 3 | 2 |
| 1 | 1 | 3 |
| 2 | 2 | 3 |
| 3 | 3 | 3 |
| 1 | 1 | 4 |
| 2 | 2 | 4 |
| 3 | 3 | 4 |
| 1 | 1 | 5 |
| 2 | 2 | 5 |
| 3 | 3 | 5 |
+-----+---------+-------+
I want to fetch a table which represents 2 IDs which got the largest sum of TIME within the last 3 days (means from 3 to 5 in a DAY column)
So the correct result would be:
+-----+---------+
| ID | SUM |
+-----+---------+
| 3 | 9 |
| 2 | 6 |
+-----+---------+
The original table is much larger and more complex. So i need a generic approach.
Thanks in advance.
And so I just learned that MySQL used LIMIT instead of TOP...
fiddle
CREATE TABLE tbl (ID INT,tm INT,dy INT);
INSERT INTO tbl (id, tm, dy) VALUES
(1,1,1)
,(2,2,1)
,(3,3,1)
,(1,1,2)
,(1,1,1)
SELECT ID
,SUM(SumTimeForDay) SumTimeFromLastThreeDays
FROM (SELECT ID
,SUM(tm) SumTimeForDay
FROM tbl
GROUP BY ID, dy
HAVING dy > MAX(dy) -3) a
GROUP BY id
ORDER BY SUM(SumTimeForDay) DESC
LIMIT 2
select t1.`id`, sum(t1.`time`) as `sum`
from `table` t1
inner join ( select distinct `day` from `table` order by `day` desc limit 3 ) t2
on t2.`da`y = t1.`day`
group by t1.`id`
order by sum(t1.`time`) desc
limit 2

Create a column that counts the number of times a value shows up on another column, from the same table - MySQL

In MySQL:
Lets say I've this table:
id | name | count |
1 | John | |
2 | John | |
3 | John | |
4 | Mary | |
5 | Lewis| |
6 | Lewis| |
7 | Max | |
8 | Max | |
The names are already grouped, so the same name comes up together.
Now I want the table to be like this:
id | name | count |
1 | John | 1 |
2 | John | 2 |
3 | John | 3 |
4 | Mary | 1 |
5 | Lewis| 1 |
6 | Lewis| 2 |
7 | Max | 1 |
8 | Max | 2 |
Notice it auto increments the value of count everytime there is a repetition of the same name.
Thanks!
You can use a user variable.
Something like this:-
UPDATE somepeople a
INNER JOIN (
SELECT id, name, IF(#PrevName=name, #aCnt := #aCnt + 1, #aCnt := 1) AS sequence, #PrevName:=name
FROM somepeople,
(SELECT #aCnt:=1, #PrevName:='') Sub1
ORDER BY name, id) b
ON a.id = b.id
SET a.count = b.sequence