I have a table like this:
T A B C ID
2015-07-19 a b c 1
2015-07-16 a y z 2
2015-07-21 a b c 1
2015-07-17 a y c 2
2015-07-18 a y c 1
2015-07-20 a b c 1
2015-07-17 a y c 1
2015-07-19 a b c 2
2015-07-16 a y z 1
2015-07-20 a b c 2
2015-07-15 a y z 1
2015-07-22 x b c 1
2015-07-21 a b c 2
2015-07-18 a y c 2
2015-07-15 a y z 2
2015-07-22 a y c 2
2015-07-14 x b c 1
I need to get an ordered result by datetime column T, but I need that the query detects and avoid repeated rows in columns A, B and C. And all this ordered and separated by ID.
It could be a stored procedure. It's important to be fast, because is a huge log table. With millions of rows.
The result should be like this:
T A B C ID
2015-07-22 x b c 1
2015-07-19 a b c 1
2015-07-17 a y c 1
2015-07-15 a y z 1
2015-07-14 x b c 1
2015-07-22 a y c 2
2015-07-19 a b c 2
2015-07-17 a y c 2
2015-07-15 a y z 2
Any ideas?
This query gives the expected result (tested):
SELECT t1.* FROM mytable t1
LEFT JOIN mytable t2 ON t1.t = t2.t + INTERVAL 1 DAY AND t1.A = t2.A AND t1.B = t2.B AND t1.C = t2.C AND t1.ID = t2.ID
WHERE t2.T IS NULL
ORDER BY t1.ID, t1.T DESC
Try this query:
SELECT max(t),a,b,c,id FROM table GROUP BY A,B,C,id ORDER BY ID, max(T)
Try this query:
SELECT * FROM table GROUP BY A,B,C,T ORDER BY ID, T
Edit: added T to group by
Related
I want to know which cinema room is more frequented daily and I need the values as relative frequency.
My tables are:
table1:
client_id
cinema_room
1
A
2
B
3
C
4
C
5
A
6
C
7
B
8
A
9
B
10
B
11
B
12
A
13
C
14
A
15
A
16
B
Table 2:
day
client_id
01/01/2022
1
01/01/2022
1
01/02/2022
1
01/02/2022
2
01/02/2022
4
01/03/2022
8
01/04/2022
14
01/04/2022
15
01/04/2022
16
So I need results like:
day
cinema_room
avg_freq
01/01/2022
A
1
01/01/2022
B
0
01/01/2022
C
0
01/02/2022
A
0.33
01/02/2022
B
0.33
01/02/2022
C
0.33
01/03/2022
A
1
01/03/2022
B
0
01/03/2022
C
0
01/04/2022
A
0.66
01/04/2022
B
0.33
01/04/2022
C
0
What I've got by now is:
SELECT day , cinema_room, COUNT(t2.client_id) as t2_tot
FROM table1 t1
LEFT JOIN table2 t2 ON t1.client_id = t2.client_id
GROUP BY day, cinema_room
ORDER BY day
Its insufficient results are:
NOTE 1: I'm counting, not even averaging.
NOTE 2: The first three rows are wrong af.
day
cinema_room
count_freq
None
A
0
None
B
0
None
C
0
01/01/2022
A
3
01/01/2022
B
0
01/01/2022
C
0
01/02/2022
A
1
01/02/2022
B
1
01/02/2022
C
1
01/03/2022
A
1
01/03/2022
B
0
01/03/2022
C
0
01/04/2022
A
2
01/04/2022
B
1
01/04/2022
C
0
You can try to use CROSS JOIN in a subquery which would be a calendar table, then aggregate table1 & table2 in another subquery the get count by each cinema_room per day.
SELECT t1.day , t1.cinema_room, SUM(IFNULL(t2.cnt,0)) / (SELECT COUNT(*) FROM table2 tt2 WHERE t1.day = tt2.day) as t2_tot
FROM (
SELECT DISTINCT day,cinema_room
FROM table2 t2 CROSS JOIN table1
) t1
LEFT JOIN (
SELECT day,cinema_room,COUNT(*) cnt
FROM table1 t1 INNER JOIN table2 t2
ON t1.client_id = t2.client_id
GROUP BY day,cinema_room
) t2 ON t1.day = t2.day AND t1.cinema_room = t2.cinema_room
GROUP BY t1.day , t1.cinema_room
ORDER BY t1.day
sqlfiddle
What is the best way to subtract the lowest value from all values by group?
Something like:
ID Name Value
1 A 10
1 B 40
1 C 100
2 A 20
2 B 80
2 C 90
3 A 4
3 B 7
3 C 8
turn to:
ID Name Value
1 A 0
1 B 30
1 C 90
2 A 0
2 B 60
2 C 70
3 A 0
3 B 3
3 C 4
Use window functions:
select id, name, value - min(value) over (partition by id) as
from t;
If you actually want to update the values, in MySQL, aggregation and join are probably the simplest solution:
update t join
(select id, min(value) as min_value
from t
group by id
) tt
on t.id = tt.id
set value = value - min_value
where min_value <> 0;
I have 2 columns
A B
1 2
2 2
1 2
3 2
5 2
0 2
4 2
11 4
12 4
11 4
I want the SQL query to return the pairs (A,B) where:
B has appeared 3 or more times
AND (A,B) is unique
The resulting table would be:
A B
1 2
2 2
3 2
5 2
0 2
4 2
You could use a join with a selected table grouped by B having count = 3
select distinct A, B
from my_table as t1
inner join (
select b
from my_table
group by b
having count(*)= 3
) t2 on t2.b = t1.b
and for 3 or more
select distinct A, B
from my_table as t1
inner join (
select b
from my_table
group by b
having count(*) >= 3
) t2 on t2.b = t1.b
I have the following tables:
TABLE1:
A B C
1 2 3
2 4 6
3 6 9
TABLE2:
A B C
4 8 12
5 10 15
6 12 18
TABLE3:
A D
2 X
4 Y
6 Z
I need one query that gives:
A B C D
1 2 3
2 4 6 X
3 6 9
4 8 12 Y
5 10 15
6 12 18 Z
Is that possible?
I can do it in 2 queries, but the person I'm doing it for wants it in 1.
Thanks!
Try this (example on sqlfiddle):
SELECT x.a, x.b, x.c, d
FROM (
SELECT a, b, c FROM table1
UNION ALL
SELECT a, b, c FROM table2
) x
LEFT JOIN table3 ON ( table3.a = x.a )
Sure:
select v1.*, table3.d
from
(select table1.a, table1.b, table1.c
from table1
union all
select table2.a, table2.b, table2.c
from table2
) v1
left join table3 on v1.a = table3.a
Well I think it's a simple question but I could not found the solution.
Well I have three Tables:
Table 1
id(AS t1id) Name LASTNAME Value
1 a z 50
2 b e 60
3 c k 70
4 d u 60
Table2
id idTable1 Name(AS t2me) Value(AS t2ve)
1 1 er 50
2 1 zx 150
3 2 zc 300
Table 3
id idTable1 Name(AS t3me) Value(AS t3ve)
1 2 erxc 50
2 2 zvvx 150
3 2 zcz 300
How to get this result with SQL
t1id Name LASTNAME t2me t2ve t3me t3ve
1 a z er 50 erdxc 150
1 a z zx 150
2 b e zc 300 erxc 50
2 b e zvvx 150
2 b e zcz 300
Is that possible? If not what could I do?
SELECT t1.id as t1id, t1.Name, t1.LASTNAME,
t2.Name as t2me, t2.Value as t2ve, t3.Name as t3me,
t3.Value as t3ve from Table1 t1
LEFT JOIN Table2 t2 on t1.id = t2.idTable1
LEFT JOIN Table3 on t3.idTable1 = t1.id