How to properly exclude rows with references to each other? - mysql

This question is based on the not very trivial question How to remove two duplicate column. I already suggested solution on that question, but I think there is some more suitable and elegant solution than mine.
There is some table of private messages with columns msg_id, from, to.
And we have this data in it:
msg_id from to
----------------
1 46 0
2 46 18
3 46 50
4 46 39
5 46 11
6 11 46
7 46 12
8 46 56
9 46 11
We need to exclude rows with the conversations, in which there are more than one message (like rows with msg_id = 5, 6 and 9) and in the same time we need to leave first row in output among these rows. In general output should be like this (note: without msg_id = 6 and msg_id = 9):
msg_id from to
----------------
1 46 0
2 46 18
3 46 50
4 46 39
5 46 11
7 46 12
8 46 56
My solution is:
select distinct pm.`from`, pm.`to`
from `tsk_private_message` pm
left join
(select distinct pm.`from`, pm.`to`
from `tsk_private_message` pm
inner join `tsk_private_message` pm2
on (pm.`to` = pm2.`from`) and (pm2.`to` <> pm.`from`)) a
using (`from`, `to`)
where a.`from` is null;
I just search unnecessary rows among these conversations via subquery and "subtract" result from the main table. What do you think? Is there more elegant and more simple solution? I just really don't like this tricky code.
Here is SQL Fiddle

SELECT mx.msg_id, pm.ffrom, pm.tto
FROM tsk_private_message pm
WHERE NOT EXISTS (
SELECT * FROM tsk_private_message nx1
WHERE nx1.ffrom = pm.ffrom AND nx1.tto = pm.tto
AND nx1.msg_id < pm.msg_id
)
AND NOT EXISTS (
SELECT * FROM tsk_private_message nx2
WHERE nx2.ffrom = pm.tto AND nx2.tto = pm.ffrom
AND nx2.msg_id < pm.msg_id
);
Note: I renamed the to and from columns to tto and ffrom, because to and from both are keywords in SQL and I don't like quoting identifiers.
Extra: sqlfiddle (courtesy of Alexander Myshov)

SELECT *
FROM
tsk_private_message INNER JOIN (
SELECT MIN(id) min_id
FROM tsk_private_message
GROUP BY
LEAST(`from`, `to`),
GREATEST(`from`, `to`)) min_msg
ON tsk_private_message.id = min_msg.min_id
ORDER BY
id
Please see fiddle here.

Related

Retrieve a column with the list of object_id's that occurs together more than once and a second column with the count of how many times it occurs

Note that object_id's 18,10 and 21 are associated to the cart_id 3 once. I wanna know whether the combination occurs again in another cart_id and how many times does that occurs over all the rows existent. I expect two columns as a resultset "combination" and "combination_occurrence_count"
It is quite complicated task to check all possible combinations as it is too many of them.
However, if you simplify your requirements a bit, you can get something useful.
Lets start with finding all combinations of two items. At the beginning you can try the following query:
SELECT
c1.cart_id AS cart1_id
, c1.object_id AS object1_id
, c2.object_id AS object2_id
, cx1.cart_id AS cartX_id
, cx1.object_id AS objectX1_id
, cx2.object_id AS objectX2_id
FROM
cart_item AS c1
INNER JOIN cart_item AS c2 ON (
c2.cart_id = c1.cart_id
AND c2.object_id > c1.object_id
)
INNER JOIN cart_item AS cx1 ON (
cx1.cart_id > c1.cart_id
AND cx1.object_id = c1.object_id
)
INNER JOIN cart_item AS cx2 ON (
cx2.cart_id = cx1.cart_id
AND cx2.object_id = c2.object_id
)
ORDER BY
c1.cart_id
, c1.object_id
, c2.object_id
, cx1.cart_id
, cx1.object_id
, cx2.object_id
There are two ideas behind the query:
Get all possible combinations of two object ids that are exist in
carts. Carts with only one item will be excluded. The only existing
combinations would be analyzed (instead of all possible combinations). [c1 & c2]
Find other carts that have the same object ids combinations [cx1 & cx2]
The results would be something like this:
cart1_id object1_id object2_id cartX_id objectX1_id objectX2_id
3 10 18 30 10 18
3 10 18 31 10 18
3 10 21 30 10 21
3 18 21 30 18 21
30 10 18 31 10 18
Then you can group these results to get "the most popular" pairs:
SELECT
cx1.object_id AS object1_id
, cx2.object_id AS object2_id
, 1 + COUNT(DISTINCT cx1.cart_id) AS cnt
FROM
cart_item AS c1
INNER JOIN cart_item AS c2 ON (
c2.cart_id = c1.cart_id
AND c2.object_id > c1.object_id
)
INNER JOIN cart_item AS cx1 ON (
cx1.cart_id > c1.cart_id
AND cx1.object_id = c1.object_id
)
INNER JOIN cart_item AS cx2 ON (
cx2.cart_id = cx1.cart_id
AND cx2.object_id = c2.object_id
)
GROUP BY
cx1.object_id
, cx2.object_id
ORDER BY
cnt DESC
LIMIT
20
Results:
object1_id object2_id cnt
10 18 3
10 21 2
18 21 2
So pair 10 + 18 is the most popular and are exist in 3 carts.
Pairs 10 + 21 and 18 + 21 are in 2 different carts.
You can continue and do something like this for 3-objects combinations.
P.S. I used the following data set (added a few rows to your data to get a bit more interesting results):
id cart_id object_id
10 2 24
9 3 10
3 3 18
19 3 21
12 4 24
1 7 30
5 9 24
2 11 10
20 14 12
14 14 18
8 14 27
13 15 11
7 16 9
18 16 13
15 20 11
6 21 6
4 23 5
17 23 6
16 25 16
11 29 11
23 30 1
21 30 10
22 30 18
24 30 21
25 31 10
26 31 18
P.P.S. I have not spent too much time on this so it is possible that I missed something in queries. However, I hope you understand the general idea.
The following returns the list of carts with all three objects:
select cart_id
from t
where object_id in (18, 10, 21)
group by cart_id
having count(distinct cart_id) = 3;
select group_concat(`app_item`.`object_id`) as `combination`
from `app_item`
group by `app_item`.`cart_id`
The query return a "combination" resultset as I was looking for:
Since I cannot group again using the "combination" and then make a count of occurrences for each combination and then get the "combination_occurrence_count" it contains, I am now doing this through a method in the application as following
and now I can display an array as key/pair like "combination" => "occurrence count" as following

Select only rows with matching pair

I have a MySQL table as below
ID SHEET_NUMBER DAYS RESULT
1 55201 9 10
2 55209 28 25.5
3 55209 28 27.9
4 38558 7 12
5 38552 5 19
6 38559 5 5
I want to select only rows with firstly matching sheet numbers & only if there if there is a matching pair of 28 days specimens
so if there is only one 28 day it will select nothing but if there is at least 2x 28 day it will get both rows
I'm totally lost, i know i should be using group by.. but i'm unsure of its use.
thankyou
Can you try the following query:
SELECT *
FROM test
WHERE sheet_number IN (
SELECT sheet_number
FROM test
WHERE days = 28
GROUP BY sheet_number
HAVING COUNT(*) >= 2
);
Here's the SQL Fiddle.
First, write a query that finds sheet_number with two or more rows with days value of 28.
SELECT d.sheet_number
FROM my_table_below d
WHERE d.days = 28
GROUP BY d.sheet_number
HAVING COUNT(1) > 1
With that query, we can use that as an inline view, and join back to the original table to find the matching rows:
SELECT t.*
FROM ( SELECT d.sheet_number
FROM my_table_below d
WHERE d.days = 28
GROUP BY d.sheet_number
HAVING COUNT(1) > 1
) m
JOIN my_table_below t
ON t.sheet_number = m.sheet_number
AND t.days = 28
ORDER BY t.sheet_number, t.id
Omit the condition t.days = 28 on the outer query, if the specification is to return all of the rows for the sheet_number, not just the rows with days=28. (The specification is a bit unclear.)

MySQL count without grouping

I am running the following query to understand to get users' first attempt to answer a question listed next to their second attempt.
SELECT
s.id AS attempt_id_first, m.id AS attempt_id_second, s.user_id
FROM
attempt s
INNER JOIN attempt m on s.user_id = m.user_id
WHERE
s.id<m.id
I end up with this:
attempt_first attempt_second user_id
7 17 1
9 10 2
9 15 2
10 15 2
4 6 9
24 25 15
29 34 19
29 36 19
34 36 19
I would like to have a new column that counts the number of attempts by users so that:
7 17 1 1
9 10 2 3
9 15 2 3
10 15 2 3
4 6 9 1
24 25 15 1
29 34 19 3
29 36 19 3
34 36 19 3
I am sure this is trivial, but I cannot get it to work. Help anyone?
I think this is it: Just display the results, and throw in an extra count subquery:
select
userid,
id,
(select
count('x')
from
attempt x
where
x.userid = a.userid) as attempcount
from
attempt a
If you like to keep the first and second attempt in separate columns, you can of course embed the subselect in your original query.
It seems wrong, though. Firstly, you need to have at least two attemps, otherwise none will show. You can solve that by changing inner join to left join and move the condition in the where clause to that join. Secondly, the 'second attempt' is not the second attempt per say. Actually, for each of the attempts you get all next attempts. Look at the example of user 2. You accidentally get three rows (where there are three attemps), but you get attempt 9 and 10, as well as attempt 9 and 15 as well as 10 and 15. 9, 15 is incorrect, since 15 isn't the attempt that followed 9. The more attempts a user has, the more of these false results you will get.
If you want one attempt listed next to the next one, with the count, I would suggest:
SELECT s.user_id, s.id AS attempt_id_first,
(select s2.id
from attempt s2
where s2.user_id = s.user_id and
s2.id > s.id
order by s2.id
limit 1
) as attempt_id_second,
(select count(*)
from attempt s3
where s3.user_id = s.user_id
) as totalAttempts
FROM attempt s ;
This only lists each attempt once with the next one. The count is included as the last column.

MySQL calculate difference in count column between two queries

I have two different queries. One for "plus" and one for "minus". I want to find the difference in the count value for each player.
I tried union all and got some very weird numbers.
Here are the results of each query which I ned to find the difference of:
fk_plus_player1_id cnt
90 71
65 68
79 66
45 59
64 57
27 56
55 56
93 55
37 55
40 44
1 36
84 33
20 31
24 28
8 23
fk_minus_player1_id cnt
93 44
64 42
79 40
37 35
90 33
20 31
84 31
27 30
65 30
40 26
1 26
24 25
45 25
55 22
8 10
How would I accomplish this? Thanks in advance for your help. I am a noob...
UGH...Trying to do the join method. Having issues, getting no results, just 4 empty columns. This is what I am trying
SELECT
*
FROM
(
SELECT
statement for plus results here
) AS tp
JOIN (
SELECT
statement for minus results here
) AS tm ON tp.fk_plus_player1_id = tm.fk_minus_player1_id
GROUP BY
fk_plus_player1_id
suggestions??
You have two tables.
You want for each player, the difference of the counts.
So :
SELECT t1.fk_minus_player1_id AS player, ABS(t1.cnt - t2.cnt) AS difference
FROM table1 t1, table2 t2
WHERE t1.fk_minus_player1_id = t2.fk_plus_player1_id
GROUP BY t1.fk_minus_player1_id;
Maybe this is what you're looking for ?
WITH query1 AS
(SELECT t1.fk_minus_player1_id AS player, (t1.cnt - IFNULL(t2.cnt,0)) AS difference
FROM table1 t1 LEFT OUTER JOIN table2 t2 ON t1.fk_minus_player1_id = t2.fk_plus_player1_id
GROUP BY t1.fk_minus_player1_id),
query2 AS (SELECT t2.fk_plus_player1_id AS player, (IFNULL(t1.cnt,0) - t2.cnt) AS difference
FROM table2 t2 LEFT OUTER JOIN table1 t1 ON t1.fk_minus_player1_id = t2.fk_plus_player1_id
GROUP BY t2.fk_plus_player1_id)
(SELECT player, difference
FROM query1)
UNION
(SELECT player, difference
FROM query2 WHERE player NOT IN (SELECT player FROM query1))
You run the risk that the same players are not in both lists. The solution is union all with group by:
select player1id, sum(pluscnt) as pluscnt, sum(minuscnt) as minuscnt,
(sum(pluscnt) - sum(minuscnt)) as diff
from ((select player1id, cnt as pluscnt, 0 as minuscnt
from plustable
) union all
(select player1id, 0, cnt
from minustable
)
) t
group by player1id;

MySQL Query suggestion with access id

I have a MySQL table like this
ownerlisting_access_id property_id mainaccess_id subaccess_id access_value
62 2 35 41 Yes
64 2 35 36 Yes
123 4 35 41 Yes
125 4 35 36 Yes
306 7 35 41 Yes
307 7 35 42 Yes
308 7 35 36 Yes
I want a query that will give me this output using subaccess_id(41,42,36) and mainaccess_id(35) -
ownerlisting_access_id property_id mainaccess_id subaccess_id access_value
306 7 35 41 Yes
307 7 35 42 Yes
308 7 35 36 Yes
I need to get the property_id as 7 using sub access id with 41, 42, 36
The fastest way to get an answer to your question is to describe the PROBLEM not just show results you need. It's not clear what is the logic behind your desired output. I guess you need the rows with the highest property_id for each group subaccess_id. If so here is the query:
select * from t
join (select subaccess_id, max(property_id) MAX_property_id
from t
where mainaccess_id=35
and
subaccess_id in (41,42,36)
group by subaccess_id
) t1
on t.subaccess_id=t1.subaccess_id
and
t.property_id=t1.MAX_property_id
SQLFiddle demo
Also here is a query that outputs results you needed :) But I guess it doesn't solve your PROBLEM:
select * from t where property_id=7
Try this:
SELECT table1.* FROM (
select property_id, group_concat(DISTINCT subaccess_id ORDER BY subaccess_id) as list
from table1 as t1 group by property_id
) a, table1
WHERE a.property_id = table1.property_id
AND a.list = '36,41,42'
Working query: http://sqlfiddle.com/#!2/4744ea/2
SELECT MAX(DISTINCT property_ID) AS property_ID, mainaccess_id,
MAX(DISTINCT subaccess_id) AS subaccess_id, MAX(DISTINCT access_value)
FROM tableName GROUP BY mainaccess_id ORDER BY mainaccess_id
SELECT property_ID, mainaccess_id, subaccess_id, access_value
FROM tableName t1
WHERE t1.mainaccess_id = 35
AND (t1.subaccess_id = 41 OR t1.subaccess_id = 42 OR t1.subaccess_id = 36)
AND t1.property_ID = (SELECT MAX(t2.property_ID)
FROM tableName t2
WHERE t2.mainaccess_id = 35
AND (t2.subaccess_id = 41 OR t2.subaccess_id = 42 OR t2.subaccess_id = 36))