Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to display the table like this
Desired Result:
With the conditions:
tgl1, kondisi_1, kondisi_2, kondisi_3, kondisi_4 is latest condition group by kd_item
tgl2, kond1, kond2, kond3, kond4 is latest data where date <= CURDATE()-INTERVAL 7 DAY group by kd_item
SQL:
http://sqlfiddle.com/#!9/661f7/2
I try to write the query like this,
Query:
SELECT a.kd_item, MAX(a.tanggal) as tgl1, a.kondisi_1, a.kondisi_2, a.kondisi_3, a.kondisi_4,
-- I know min is not the right query
MIN(b.tanggal) as tgl2, b.kondisi_1 as kond1, b.kondisi_2 as kond2, b.kondisi_3 as kond3, b.kondisi_4 as kond4
FROM status_item as a
LEFT JOIN status_item as b ON a.kd_item = b.kd_item
GROUP BY a.kd_item LIMIT 10
When I am joining same table, with the above query, why is the displayed data not related with the date?
How to display date with condition latest date where date less then or equal with 7 days ago?
How to display point 2 at left side of the table?
you should select the max date, join with the data for get column non involved in group by and then join the two different query toghter
select * from (
select a.kd_item, a.tanggal as tgl1, a.kondisi_1, a.kondisi_2, a.kondisi_3, a.kondisi_4
from status_item a
inner join (
SELECT kd_item, MAX(tanggal) as max_tanggal
FROM status_item
group by kd_item
) t1 on t1.max_tanggal = a.tanggal and t1.kd_item = a.kd_item
) t3
inner join (
select b.kd_item, b.tanggal as b_tgl1, b.kondisi_1 as b_kondisi_1, b.kondisi_2 as b_kondisi_2 , b.kondisi_3 as b_kondisi_3, b.kondisi_4 as b_kondisi_4
from status_item b
inner join (
SELECT kd_item, MAX(tanggal) as max_tanggal
FROM status_item
where tanggal <= date_sub(CURDATE(), INTERVAL 7 DAY )
group by kd_item
) t2 on t2.max_tanggal = b.tanggal and t2.kd_item = b.kd_item
) t4 on t3.kd_item= t4.kd_item
Is this what you are looking for?
select
a.kd_item,
a.tanggal as tgl1,
a.kondisi_1,
a.kondisi_2,
a.kondisi_3,
a.kondisi_4,
b.tanggal as tgl2,
b.kondisi_1 as kond1,
b.kondisi_2 as kond2,
b.kondisi_3 as kond3,
b.kondisi_4 as kond4
from
status_item as a
left join
status_item as b
on
a.kd_item = b.kd_item and
b.tanggal <= date_sub(a.tanggal, interval 7 day);
The results are:
+---------+------------+-----------+-----------+-----------+-----------+------------+-------+-------+-------+-------+
| kd_item | tgl1 | kondisi_1 | kondisi_2 | kondisi_3 | kondisi_4 | tgl2 | kond1 | kond2 | kond3 | kond4 |
+---------+------------+-----------+-----------+-----------+-----------+------------+-------+-------+-------+-------+
| 1 | 2017-10-22 | 4 | 0 | 0 | 0 | 2017-10-07 | 3 | 0 | 1 | 0 |
| 1 | 2017-10-17 | 3 | 1 | 0 | 0 | 2017-10-07 | 3 | 0 | 1 | 0 |
| 2 | 2017-12-22 | 3 | 0 | 1 | 0 | 2017-12-12 | 2 | 1 | 0 | 1 |
| 2 | 2017-12-22 | 3 | 0 | 1 | 0 | 2017-10-22 | 4 | 0 | 0 | 0 |
| 2 | 2017-12-12 | 2 | 1 | 0 | 1 | 2017-10-22 | 4 | 0 | 0 | 0 |
| 3 | 2017-12-12 | 4 | 0 | 0 | 0 | 2017-10-22 | 1 | 1 | 1 | 1 |
| 8 | 2017-12-06 | 4 | 0 | 0 | 0 | 2017-11-28 | 0 | 0 | 4 | 0 |
| 1 | 2017-10-07 | 3 | 0 | 1 | 0 | null | null | null | null | null |
| 2 | 2017-10-22 | 4 | 0 | 0 | 0 | null | null | null | null | null |
| 3 | 2017-10-22 | 1 | 1 | 1 | 1 | null | null | null | null | null |
| 4 | 2017-10-22 | 4 | 0 | 0 | 0 | null | null | null | null | null |
| 5 | 2017-10-27 | 4 | 0 | 0 | 0 | null | null | null | null | null |
| 5 | 2017-10-22 | 3 | 0 | 1 | 0 | null | null | null | null | null |
| 6 | 2017-10-22 | 4 | 0 | 0 | 0 | null | null | null | null | null |
| 7 | 2017-10-22 | 4 | 0 | 0 | 0 | null | null | null | null | null |
| 8 | 2017-11-28 | 0 | 0 | 4 | 0 | null | null | null | null | null |
+---------+------------+-----------+-----------+-----------+-----------+------------+-------+-------+-------+-------+
16 rows in set (0.00 sec)
For point 3 in your question, you could add
date_sub(a.tanggal, interval 7 day)
as the first result column:
select
date_sub(a.tanggal, interval 7 day) as week_ago,
a.kd_item,
a.tanggal as tgl1,
...
Related
From the following game table of sports matches:
+-----+-----------+---------------+----------+-------+-------+---------+---------+
| id_ | date_time | tournament_id | round_id | p1_id | p2_id | p1_stat | p2_stat |
+-----+-----------+---------------+----------+-------+-------+---------+---------+
| 1 | NULL | 1 | 4 | 1 | 3 | 2 | 3 |
| 2 | NULL | 1 | 5 | 1 | 4 | 4 | 6 |
| 3 | NULL | 1 | 9 | 1 | 5 | 6 | 9 |
| 4 | NULL | 1 | 10 | 2 | 1 | 8 | 12 |
| 5 | NULL | 2 | 4 | 1 | 2 | 10 | 15 |
| 6 | NULL | 2 | 5 | 4 | 1 | 12 | 18 |
+-----+-----------+---------------+----------+-------+-------+---------+---------+
I'm trying to get the stats for each player for their previous match. The output should look like this:
+-----+--------------+--------------+
| id_ | prev_p1_stat | prev_p2_stat |
+-----+--------------+--------------+
| 1 | NULL | NULL |
| 2 | 2 | NULL |
| 3 | 4 | NULL |
| 4 | NULL | 6 |
| 5 | 12 | 8 |
| 6 | 6 | 10 |
+-----+--------------+--------------+
However, the date_time column is quite often blank and id_ is not date sequential. The tournament table does have a date_time column which is always populated:
+-----+------------+
| id_ | date_time |
+-----+------------+
| 1 | 1997-01-01 |
| 2 | 1997-01-06 |
+-----+------------+
This means the tournament date_time can be used in conjunction with game round_id to determine the previous match.
I've found the following answers here and here but they both focus on a single table and don't have the added complexity of having to determine whether the p1_stat or the p2_stat should be selected.
I've got as far as this query:
SELECT
g.id_ AS game_id,
CASE
WHEN g.p1_id = sq_p1.p1_id THEN sq_p1.p1_stat
ELSE sq_p1.p2_stat
END AS prev_p1_stat,
CASE
WHEN g.p1_id = sq_p2.p1_id THEN sq_p2.p1_stat
ELSE sq_p2.p2_stat
END AS prev_p2_stat
FROM
test.game AS g
JOIN
test.tournament AS t ON t.id_ = g.tournament_id
LEFT OUTER JOIN
(SELECT
g.id_ AS match_id,
t.date_time AS tournament_date,
g.round_id,
g.p1_id,
g.p2_id,
g.p1_stat,
g.p2_stat
FROM
test.game AS g
JOIN test.tournament AS t ON t.id_ = g.tournament_id) AS sq_p1 ON (sq_p1.p1_id = g.p1_id
OR sq_p1.p2_id = g.p1_id)
AND (sq_p1.tournament_date = t.date_time
AND sq_p1.round_id < g.round_id
OR sq_p1.tournament_date < t.date_time)
LEFT OUTER JOIN
(SELECT
g.id_ AS match_id,
t.date_time AS tournament_date,
g.round_id,
g.p1_id,
g.p2_id,
g.p1_stat,
g.p2_stat
FROM
test.game AS g
JOIN test.tournament AS t ON t.id_ = g.tournament_id) AS sq_p2 ON (sq_p2.p1_id = g.p1_id
OR sq_p2.p2_id = g.p1_id)
AND (sq_p2.tournament_date = t.date_time
AND sq_p2.round_id < g.round_id
OR sq_p2.tournament_date < t.date_time)
ORDER BY t.date_time , g.round_id
But this isn't even close to what I'm looking for :(
I've created a dbfiddle.
One other thing that's perhaps worth mentioning... I intend to use a couple of versions of this query in a union query such that the final result (including all columns for reference) will look like this:
+-----+------------+-----------+-------------+-------------+---------------+------------------+--------------------+
| id_ | player_num | player_id | opponent_id | player_stat | opponent_stat | player_prev_stat | opponent_prev_stat |
+-----+------------+-----------+-------------+-------------+---------------+------------------+--------------------+
| 1 | 1 | 1 | 3 | 2 | 3 | NULL | NULL |
| 1 | 2 | 3 | 1 | 3 | 2 | NULL | NULL |
| 2 | 1 | 1 | 4 | 4 | 6 | 2 | NULL |
| 2 | 2 | 4 | 1 | 6 | 4 | NULL | 2 |
| 3 | 1 | 1 | 5 | 6 | 9 | 4 | NULL |
| 3 | 2 | 5 | 1 | 9 | 6 | NULL | 4 |
| 4 | 1 | 2 | 1 | 8 | 12 | NULL | 6 |
| 4 | 2 | 1 | 2 | 12 | 8 | 6 | NULL |
| 5 | 1 | 1 | 2 | 10 | 15 | 12 | 8 |
| 5 | 2 | 2 | 1 | 15 | 10 | 8 | 12 |
| 6 | 1 | 4 | 1 | 12 | 18 | 6 | 10 |
| 6 | 2 | 1 | 4 | 18 | 12 | 10 | 6 |
+-----+------------+-----------+-------------+-------------+---------------+------------------+--------------------+
Perhaps it makes more sense to do a union and then engineer the previous stats?
For some final info, the actual game table has about 1.5m rows and the actual tournament table has about 30k rows. I'm using MySQL 8.0.26.
Kudos to #Barmer for the direction - here's the query I created using LAG():
WITH union_matches AS (
SELECT
g.id_ AS match_id,
t.date_time AS tournament_date,
g.round_id AS round_id,
1 AS player_num,
g.p1_id AS player_id,
g.p2_id AS opponent_id,
g.p1_stat AS player_stat,
g.p2_stat AS opponent_stat
FROM
game AS g
JOIN
tournament AS t ON t.id_ = g.tournament_id
UNION SELECT
g.id_ AS match_id,
t.date_time AS tournament_date,
g.round_id AS round_id,
2 AS player_num,
g.p2_id AS player_id,
g.p1_id AS opponent_id,
g.p2_stat AS player_stat,
g.p1_stat AS opponent_stat
FROM
game AS g
JOIN
tournament AS t ON t.id_ = g.tournament_id
)
SELECT
match_id,
player_num,
player_id,
opponent_id,
player_stat,
opponent_stat,
LAG(player_stat, 1) OVER (PARTITION BY player_id ORDER BY tournament_date, round_id) AS wrong_player_prev_stat,
LAG(opponent_stat, 1) OVER (PARTITION BY opponent_id ORDER BY tournament_date, round_id) AS wrong_opponent_prev_stat
FROM
union_matches
ORDER BY
tournament_date, round_id, player_num
And a link to the dbfiddle.
I am setting up a virtual classroom in PHP and MySQL. This classroom consists of courses and each course contains different subjects or modules. The student has to examine each module and, finally, a summary (evaluation board) is made to know if the student has passed the course or not.
Having said that, I have a table in which I store the evaluations of each student, in which I keep inscripcion_id (student - inscription_id), modulo_id (module_id), fecha (date_of_examination), aciertos (number_of_right_answers), ultima_convocatoria (evaluation_last_convocatory) and estado (status).
SQL Fiddle -> here
Through some previously established rules that tell me if a student has passed a module or not, I get the following set of data:
+----------------+-----------+---------------------+----------+---------------------+--------+------------+
| inscripcion_id | modulo_id | fecha | aciertos | ultima_convocatoria | estado | ev |
+----------------+-----------+---------------------+----------+---------------------+--------+------------+
| 890 | 1 | 2018-01-24 22:26:09 | 8 | 2 | 1 | aprobado |
| 890 | 2 | 2018-01-24 22:36:58 | 3 | 3 | 0 | suspendido |
| 890 | 5 | 2018-01-24 22:38:50 | 3 | 1 | 0 | suspendido |
| 890 | 6 | 2018-01-24 22:44:20 | 7 | 3 | 0 | suspendido |
| 891 | 1 | 2018-01-25 09:24:42 | 8 | 1 | 1 | aprobado |
| 891 | 2 | 2018-01-25 10:01:55 | 4 | 8 | 0 | suspendido |
| 891 | 4 | 2018-01-25 10:51:49 | 5 | 3 | 1 | suspendido |
| 891 | 5 | 2018-01-25 10:23:45 | 9 | 1 | 1 | aprobado |
| 891 | 6 | 2018-01-25 11:21:20 | 7 | 3 | 0 | suspendido |
| 896 | 1 | 2018-01-25 11:55:48 | 1 | 1 | 1 | suspendido |
| 898 | 1 | 2018-01-25 14:01:51 | 6 | 1 | 1 | suspendido |
| 907 | 1 | 2018-03-25 16:06:18 | 3 | 1 | 0 | suspendido |
| 907 | 2 | 2018-03-25 16:07:34 | 3 | 1 | 0 | suspendido |
| 907 | 3 | 2018-03-25 16:09:04 | 3 | 1 | 0 | suspendido |
| 907 | 4 | 2018-03-25 16:08:13 | 3 | 1 | 0 | suspendido |
| 907 | 5 | 2018-03-25 16:10:37 | 2 | 1 | 0 | suspendido |
| 907 | 6 | 2018-03-25 16:08:44 | 3 | 1 | 0 | suspendido |
+----------------+-----------+---------------------+----------+---------------------+--------+------------+
This data is obtained through the following query:
SELECT e1.inscripcion_id,
e1.modulo_id,
e1.fecha,
e1.aciertos,
e1.convocatoria AS ultima_convocatoria,
e1.estado,
if (
( e1.modulo_id in (SELECT modulo.modulo_id FROM modulo WHERE modulo.curso_id = 1 AND modulo.categoria_id = 1)
AND e1.aciertos <= 7 )
OR ( e1.modulo_id = (SELECT modulo.modulo_id FROM modulo WHERE modulo.curso_id = 1 AND modulo.categoria_id = 2)
AND e1.aciertos <= 11 ),
"suspendido",
"aprobado"
) AS ev
FROM (
SELECT inscripcion_id,
modulo_id,
MAX(convocatoria) AS max_convocatoria
FROM `evaluacion`
GROUP BY inscripcion_id,
modulo_id
ORDER BY `inscripcion_id` ASC,
`modulo_id` ASC,
`convocatoria` ASC
) AS e2
INNER JOIN evaluacion AS e1
ON e1.inscripcion_id = e2.inscripcion_id
AND e1.modulo_id = e2.modulo_id
AND e1.convocatoria = e2.max_convocatoria
As you can see, the student 890, has made modules 1, 2, 5 and 6. What I want to achieve is that the modules that are still pending, I also get as a result in the previous data set. The exemplification:
+----------------+-----------+---------------------+----------+---------------------+--------+------------+
| inscripcion_id | modulo_id | fecha | aciertos | ultima_convocatoria | estado | ev |
+----------------+-----------+---------------------+----------+---------------------+--------+------------+
| 890 | 1 | 2018-01-24 22:26:09 | 8 | 2 | 1 | aprobado |
| 890 | 2 | 2018-01-24 22:36:58 | 3 | 3 | 0 | suspendido |
| 890 | 3 | NULL | NULL | NULL | NULL | pendiente |
| 890 | 4 | NULL | NULL | NULL | NULL | pendiente |
| 890 | 5 | 2018-01-24 22:38:50 | 3 | 1 | 0 | suspendido |
| 890 | 6 | 2018-01-24 22:44:20 | 7 | 3 | 0 | suspendido |
| 891 | 1 | 2018-01-25 09:24:42 | 8 | 1 | 1 | aprobado |
| 891 | 2 | 2018-01-25 10:01:55 | 4 | 8 | 0 | suspendido |
| 891 | 3 | NULL | NULL | NULL | NULL | pendiente |
| 891 | 4 | 2018-01-25 10:51:49 | 5 | 3 | 1 | suspendido |
| 891 | 5 | 2018-01-25 10:23:45 | 9 | 1 | 1 | aprobado |
| 891 | 6 | 2018-01-25 11:21:20 | 7 | 3 | 0 | suspendido |
| 896 | 1 | 2018-01-25 11:55:48 | 1 | 1 | 1 | suspendido |
| 896 | 2 | NULL | NULL | NULL | NULL | pendiente |
| 896 | 3 | NULL | NULL | NULL | NULL | pendiente |
| 896 | 4 | NULL | NULL | NULL | NULL | pendiente |
| 896 | 5 | NULL | NULL | NULL | NULL | pendiente |
| 896 | 6 | NULL | NULL | NULL | NULL | pendiente |
| ... | | | | | | |
+----------------+-----------+---------------------+----------+---------------------+--------+------------+
The result is that the modules that the student has not yet done have been added, with the new value "pending" for the ev column.
I have no idea how to do this ... I've tried, I searched the internet and nothing :(
What is the final objective? What I want is to obtain a final list with all those students who have the pending course (that is, they have some pending module/s), to send them a reminder email that they have to examine themselves of the remaining modules. To those who have approved or suspended, no email will be sent to them.
Can you help me?
SQL Fiddle -> here
THANK YOU VERY MUCH
Suppose you want to get students with no module in evaluation table as 'pending' like what you provided in the example. The way to get student joining all module is to do a full join on modulo and evaluacion to get full set of distinct inscripcion_id modulo_id. Then left join with your existing query will provide the result you want.
sqlfiddle
SELECT fs.inscripcion_id,
fs.modulo_id,
e3.fecha,
e3.aciertos,
e3.ultima_convocatoria,
e3.estado,
IF(e3.ev IS NULL, "pendiente", e3.ev) AS ev
FROM (SELECT m.modulo_id,
e.inscripcion_id
FROM modulo m,
evaluacion e
GROUP BY m.modulo_id,
e.inscripcion_id) AS fs
LEFT JOIN (SELECT
e1.inscripcion_id,
e1.modulo_id,
e1.fecha,
e1.aciertos,
e1.convocatoria
AS
ultima_convocatoria
,
e1.estado,
IF (( e1.modulo_id IN (SELECT modulo.modulo_id
FROM modulo
WHERE modulo.curso_id = 1
AND modulo.categoria_id =
1)
AND e1.aciertos <= 7 )
OR ( e1.modulo_id = (SELECT modulo.modulo_id
FROM modulo
WHERE
modulo.curso_id = 1
AND modulo.categoria_id = 2)
AND e1.aciertos <= 11 ), "suspendido",
"aprobado")
AS ev
FROM (SELECT inscripcion_id,
modulo_id,
Max(convocatoria) AS max_convocatoria
FROM `evaluacion`
GROUP BY inscripcion_id,
modulo_id
ORDER BY `inscripcion_id` ASC,
`modulo_id` ASC,
`convocatoria` ASC) AS e2
INNER JOIN evaluacion AS e1
ON e1.inscripcion_id = e2.inscripcion_id
AND e1.modulo_id = e2.modulo_id
AND e1.convocatoria = e2.max_convocatoria)
AS e3
ON fs.modulo_id = e3.modulo_id
AND fs.inscripcion_id = e3.inscripcion_id
ORDER BY fs.inscripcion_id,
fs.modulo_id;
For the further question,
You may want to use
SELECT inscripcion_id,
SUM(case when ev = 'aprobado' then 1 else 0 end) as approved_cnt,
SUM(case when ev = 'suspendido' then 1 else 0 end) as suspended_cnt,
SUM(case when ev = 'pendiente' then 1 else 0 end) as pending_cnt
From --the above query...
Group by inscripcion_id
to get the count of status for each student, and then do the logic using those count.
After reviewing the new sqlfiddle
I wrote the query below, I think it should cover what you want
Notice that you more than one evaluation per module, meaning that you'll get more than one status per module
To solve that, you can add a group statement (in comment now)
Or you the different evaluations to your needs...
SELECT
i.inscripcion_id,
c.curso_id,
c.titulo AS curso_titulo,
m.modulo_id,
m.titulo AS modulo_titulo,
IFNULL(ev.estado,0) AS estado,
m.*
FROM
inscripcion i
INNER JOIN curso c ON c.curso_id = i.curso_id
INNER JOIN modulo m ON m.curso_id = c.curso_id
LEFT JOIN evaluacion ev ON ev.modulo_id = m.modulo_id
WHERE
(ev.estado = 0 OR ev.estado IS NULL)
/*
GROUP BY
m.modulo_id
*/
;
I have 2 tables bellow
0 --> Pending
1 --> Success
2 --> Fail
table : mntnc
+-------+-------+-------+
| id | own | sts |
+-------+-------+-------+
| 1 | BN | 1 |
| 2 | BB | 2 |
| 3 | BN | 1 |
| 4 | BD | 1 |
| 5 | BD | 0 |
table : istlsi
+-------+-------+-------+
| id | own | sts |
+-------+-------+-------+
| 1 | BN | 1 |
| 2 | BB | 1 |
| 3 | BB | 1 |
| 4 | BC | 0 |
| 5 | BD | 2 |
of the two tables above, I want to add both of them to be the table below
+-------+-----------+-----------+-----------+
| own | success | fail | pending |
+-------+-----------+-----------+-----------+
| BN | 3 | 0 | 0 |
| BB | 2 | 1 | 0 |
| BD | 1 | 1 | 1 |
| BC | 0 | 0 | 1 |
The two key points here:
Union tables (I aliased result to B)
Use sum(case...) for each column.
First we union both tables together as an inline view.
We then use a case statement for each desired column and evaluate the status setting the value to 1 or 0 depending on sts value. and then sum those...
SELECT own
, sum(case when sts=1 then 1 else 0 end) as Success
, sum(case when sts=2 then 1 else 0 end) as Fail
, sum(case when sts=0 then 1 else 0 end) as Pending
FROM ( SELECT ID, own, sts
FROM mntnc
UNION ALL
SELECT id, own, sts
FROM istlsi
) B
GROUP BY own
I'm building a website for our ball team for the fun of it and keeping track of stats using PHP and SQL for the database. I've learned both by reading the manuals and through forums. I'm working on building a query that will display the current longest hitting streak. I stumbled across a page about detecting runs and streaks and am trying to work with that. I'm really new to all this stuff, so maybe I've structured my tables incorrectly.
Table "games"
+--------+------------+------+
| GameID | Date | Time |
+--------+------------+------+
| 1 | 2015/08/19 | 6:30 |
| 2 | 2015/08/20 | 6:30 |
| 3 | 2015/08/22 | 6:30 |
| 4 | 2015/08/24 | 8:00 |
| 5 | 2015/08/24 | 6:30 |
| 6 | 2015/07/15 | 8:00 |
+--------+------------+------+
Table "player"
+--------+----+---+
| GameID | AB | H |
+--------+----+---+
| 1 | 3 | 1 |
| 2 | 4 | 2 |
| 3 | 2 | 0 |
| 4 | 3 | 0 |
| 5 | 2 | 1 |
| 6 | 3 | 0 |
+--------+----+---+
Code
SELECT games.GameID, GR.H,
(SELECT COUNT(*)
FROM player G
WHERE (CASE WHEN G.H > 0 THEN 1 ELSE 0 END) <> (CASE WHEN GR.H > 0 THEN 1 ELSE 0 END)
AND G.GameID <= GR.GameID) as RunGroup
FROM player GR
INNER JOIN games
ON GR.gameID = games.GameID
ORDER BY Date ASC, Time ASC
Basically in order to correctly get the hit streak right, I need to reorder the GameIDs on the "player" table based on the Date (ASC) and Time (ASC) on the "games" table before executing the RunGroup part of the code. Obviously by adding the ORDER BY, everything gets sorted only after the RunGroup has finished querying and results in incorrect data. I've been stuck here for a few days and now need some help.
The Result I currently get is:
+--------+---+----------+
| GameID | H | RunGroup |
+--------+---+----------+
| 6 | 0 | 3 |
| 1 | 1 | 0 |
| 2 | 2 | 0 |
| 3 | 0 | 2 |
| 5 | 1 | 2 |
| 4 | 0 | 2 |
+--------+---+----------+
This is what I'm trying to achieve:
+--------+---+----------+
| GameID | H | RunGroup |
+--------+---+----------+
| 6 | 0 | 0 |
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 0 | 2 |
| 5 | 1 | 2 |
| 4 | 0 | 3 |
+--------+---+----------+
Thanks
Consider the following:
DROP TABLE IF EXISTS games;
CREATE TABLE games
(game_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,date_played DATETIME NOT NULL
);
INSERT INTO games VALUES
(1,'2015/08/19 18:30:00'),
(2,'2015/08/20 18:30:00'),
(3,'2015/08/22 18:30:00'),
(4,'2015/08/24 20:00:00'),
(5,'2015/08/24 18:30:00'),
(6,'2015/07/15 20:00:00');
DROP TABLE IF EXISTS stats;
CREATE TABLE stats
(player_id INT NOT NULL
,game_id INT NOT NULL
,at_bat INT NOT NULL
,hits INT NOT NULL
,PRIMARY KEY(player_id,game_id)
);
INSERT INTO stats VALUES
(1,1,3,1),
(1,2,4,2),
(1,3,2,0),
(1,4,3,0),
(1,5,2,1),
(1,6,3,0),
(2,1,2,1),
(2,2,3,2),
(2,3,3,0),
(2,4,3,1),
(2,5,2,1),
(2,6,3,0);
SELECT x.*
, SUM(y.at_bat) runningAB
, SUM(y.hits) runningH
, SUM(y.hits)/SUM(y.at_bat) BA
FROM
(
SELECT s.*, g.date_played FROM stats s JOIN games g ON g.game_id = s.game_id
) x
JOIN
(
SELECT s.*, g.date_played FROM stats s JOIN games g ON g.game_id = s.game_id
) y
ON y.player_id = x.player_id
AND y.date_played <= x.date_played
GROUP
BY x.player_id
, x.date_played;
+-----------+---------+--------+------+---------------------+-----------+----------+--------+
| player_id | game_id | at_bat | hits | date_played | runningAB | runningH | BA |
+-----------+---------+--------+------+---------------------+-----------+----------+--------+
| 1 | 6 | 3 | 0 | 2015-07-15 20:00:00 | 3 | 0 | 0.0000 |
| 1 | 1 | 3 | 1 | 2015-08-19 18:30:00 | 6 | 1 | 0.1667 |
| 1 | 2 | 4 | 2 | 2015-08-20 18:30:00 | 10 | 3 | 0.3000 |
| 1 | 3 | 2 | 0 | 2015-08-22 18:30:00 | 12 | 3 | 0.2500 |
| 1 | 5 | 2 | 1 | 2015-08-24 18:30:00 | 14 | 4 | 0.2857 |
| 1 | 4 | 3 | 0 | 2015-08-24 20:00:00 | 17 | 4 | 0.2353 |
| 2 | 6 | 3 | 0 | 2015-07-15 20:00:00 | 3 | 0 | 0.0000 |
| 2 | 1 | 2 | 1 | 2015-08-19 18:30:00 | 5 | 1 | 0.2000 |
| 2 | 2 | 3 | 2 | 2015-08-20 18:30:00 | 8 | 3 | 0.3750 |
| 2 | 3 | 3 | 0 | 2015-08-22 18:30:00 | 11 | 3 | 0.2727 |
| 2 | 5 | 2 | 1 | 2015-08-24 18:30:00 | 13 | 4 | 0.3077 |
| 2 | 4 | 3 | 1 | 2015-08-24 20:00:00 | 16 | 5 | 0.3125 |
+-----------+---------+--------+------+---------------------+-----------+----------+--------+
I rebuilt my database to have only one table to contain the stats from all players. From there i was able to use this query to find my longest current hitting streak for a certain player.
SELECT *
FROM (SELECT (CASE WHEN h > 0 THEN 1 ELSE 0 END) As H, MIN(date_played) as StartDate,
MAX(date_played) as EndDate, COUNT(*) as Games
FROM (SELECT date_played, (CASE WHEN h > 0 THEN 1 ELSE 0 END) as H, (SELECT COUNT(*)
FROM stats G WHERE ((CASE WHEN G.h > 0 THEN 1 ELSE 0 END) <> (CASE WHEN GR.h > 0 THEN 1 ELSE 0 END))
AND G.date_played <= GR.date_played AND player_id = 13) as RunGroup
FROM stats GR
WHERE player_id = 13) A
GROUP BY H, RunGroup
ORDER BY Min(date_played)) A
WHERE H = 1
ORDER BY Games DESC
LIMIT 1
I have a table like this:
id | status | user_id | created_at
---:--------:---------:--------------------
1 | 0 | 1 | 2014-01-05 07:23:15
2 | 1 | 1 | 2014-01-05 07:23:16
3 | 1 | 1 | 2014-01-05 07:23:17
4 | 0 | 1 | 2014-01-05 07:23:18
5 | 0 | 1 | 2014-01-05 07:23:19
6 | 1 | 1 | 2014-01-05 07:23:20
7 | 0 | 2 | 2014-01-05 07:23:21
8 | 0 | 1 | 2014-01-05 07:23:22
9 | 0 | 2 | 2014-01-05 07:23:23
10 | 1 | 2 | 2014-01-05 07:23:24
11 | 0 | 2 | 2014-01-05 07:23:25
12 | 1 | 2 | 2014-01-05 07:23:26
I'd like to query the changes on the status field, grouped by user_id, always fetching the last status (based on created_at). The result of the query should be something like this:
id | status | user_id | created_at
---:--------:---------:--------------------
1 | 0 | 1 | 2014-01-05 07:23:15
3 | 1 | 1 | 2014-01-05 07:23:17
5 | 0 | 1 | 2014-01-05 07:23:19
6 | 1 | 1 | 2014-01-05 07:23:20
8 | 0 | 1 | 2014-01-05 07:23:22
9 | 0 | 2 | 2014-01-05 07:23:23
10 | 1 | 2 | 2014-01-05 07:23:24
11 | 0 | 2 | 2014-01-05 07:23:25
12 | 1 | 2 | 2014-01-05 07:23:26
Is there a way to query for changes in SQL in a situation like this? How this query should be written?
Consider the following...
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,status TINYINT NOT NULL DEFAULT 1
,user_id INT NOT NULL
,created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO my_table VALUES
(1 , 0 , 1 ,'2014-01-05 07:23:15'),
(2 , 1 , 1 ,'2014-01-05 07:23:16'),
(3 , 1 , 1 ,'2014-01-05 07:23:17'),
(4 , 0 , 1 ,'2014-01-05 07:23:18'),
(5 , 0 , 1 ,'2014-01-05 07:23:19'),
(6 , 1 , 1 ,'2014-01-05 07:23:20'),
(7 , 0 , 2 ,'2014-01-05 07:23:21'),
(8 , 0 , 1 ,'2014-01-05 07:23:22'),
(9 , 0 , 2 ,'2014-01-05 07:23:23'),
(10 , 1 , 2 ,'2014-01-05 07:23:24'),
(11 , 0 , 2 ,'2014-01-05 07:23:25'),
(12 , 1 , 2 ,'2014-01-05 07:23:26');
For the solution provided below it actually doesn't matter that the id is contiguous, just that it's sequential. I've broken the solution down into bits so you can see what it's doing...
The first part ranks results by user...
SELECT x.*
, COUNT(*) rank
FROM my_table x
JOIN my_table y
ON y.user_id = x.user_id
AND y.id <= x.id
GROUP
BY x.id
ORDER
BY x.user_id,rank;
+----+--------+---------+---------------------+------+
| id | status | user_id | created_at | rank |
+----+--------+---------+---------------------+------+
| 1 | 0 | 1 | 2014-01-05 07:23:15 | 1 |
| 2 | 1 | 1 | 2014-01-05 07:23:16 | 2 |
| 3 | 1 | 1 | 2014-01-05 07:23:17 | 3 |
| 4 | 0 | 1 | 2014-01-05 07:23:18 | 4 |
| 5 | 0 | 1 | 2014-01-05 07:23:19 | 5 |
| 6 | 1 | 1 | 2014-01-05 07:23:20 | 6 |
| 8 | 0 | 1 | 2014-01-05 07:23:22 | 7 |
| 7 | 0 | 2 | 2014-01-05 07:23:21 | 1 |
| 9 | 0 | 2 | 2014-01-05 07:23:23 | 2 |
| 10 | 1 | 2 | 2014-01-05 07:23:24 | 3 |
| 11 | 0 | 2 | 2014-01-05 07:23:25 | 4 |
| 12 | 1 | 2 | 2014-01-05 07:23:26 | 5 |
+----+--------+---------+---------------------+------+
The second part joins this query to itself, and highlights anomalies...
SELECT a.*
, b.id
FROM
( SELECT x.*
, COUNT(*) rank
FROM my_table x
JOIN my_table y
ON y.user_id = x.user_id
AND y.id <= x.id
GROUP
BY x.id
) a
LEFT
JOIN
( SELECT x.*
, COUNT(*) rank
FROM my_table x
JOIN my_table y
ON y.user_id = x.user_id
AND y.id <= x.id
GROUP
BY x.id
) b
ON b.user_id = a.user_id
AND b.status = a.status
AND b.rank = a.rank + 1;
+----+--------+---------+---------------------+------+------+
| id | status | user_id | created_at | rank | id |
+----+--------+---------+---------------------+------+------+
| 1 | 0 | 1 | 2014-01-05 07:23:15 | 1 | NULL |
| 2 | 1 | 1 | 2014-01-05 07:23:16 | 2 | 3 |
| 3 | 1 | 1 | 2014-01-05 07:23:17 | 3 | NULL |
| 4 | 0 | 1 | 2014-01-05 07:23:18 | 4 | 5 |
| 5 | 0 | 1 | 2014-01-05 07:23:19 | 5 | NULL |
| 6 | 1 | 1 | 2014-01-05 07:23:20 | 6 | NULL |
| 7 | 0 | 2 | 2014-01-05 07:23:21 | 1 | 9 |
| 8 | 0 | 1 | 2014-01-05 07:23:22 | 7 | NULL |
| 9 | 0 | 2 | 2014-01-05 07:23:23 | 2 | NULL |
| 10 | 1 | 2 | 2014-01-05 07:23:24 | 3 | NULL |
| 11 | 0 | 2 | 2014-01-05 07:23:25 | 4 | NULL |
| 12 | 1 | 2 | 2014-01-05 07:23:26 | 5 | NULL |
+----+--------+---------+---------------------+------+------+
The third and final part is deliberately left as an exercise for the reader, however, one drawback with this solution is that it does not scale particularly well.
Probably it is a good idea to use variables in MySQL in such scenarios.
Here is one quick attempt with steps elaborated. Clean and tweak it up to suit the requirements and performance.
select id, status, user_id, created_at from
(select id, status, user_id, created_at,
(case when #user_id != user_id then 'true' else 'false' end) as user_changed,
(case when #status != status then 'true' else 'false' end) as status_changed,
(case when #user_id != user_id then #user_id := user_id end) as new_user_id,
(case when #status != status then #status := status end) as new_status
from (select * from logs order by user_id asc, created_at desc) l
join (select #user_id := 0) u
join (select #status := 0) s) q
where user_changed = 'true' or status_changed = 'true'
order by id
;