I have a question about this mysql query on mysql Ver 14.14 Distrib 5.5.35:
I have a table named mytable with 3 columns date,value and id_patient.
CREATE TABLE `mytable`
( `date` DATE NOT NULL
, `id_patient` INT(11) NOT NULL
, `value` INT(3) NULL DEFAULT NULL
);
INSERT INTO `mytable` (`date`, `id_patient`, `value`) VALUES
('2019-11-17', '87321', '6'),
('2019-11-18', '87321', '1'),
('2019-11-19', '87321', '2'),
('2019-11-20', '87321', NULL),
('2019-11-21', '87321', '5'),
('2019-11-22', '87321', '8'),
('2019-11-23', '87321', NULL),
('2019-11-24', '87321', '3'),
('2019-11-25', '87321', '4'),
('2019-11-26', '87321', '6'),
('2019-11-27', '87321', '1'),
('2019-11-28', '87321', '10');
For each row i need to know the sum of 4th previous values not null.
SELECT #date:=date, value,
( SELECT SUM(value)
FROM mytable
WHERE date<#date
AND id_patient=87321
AND value IS NOT NULL
ORDER BY date DESC LIMIT 0,4 ) somme
FROM mytable
WHERE id_patient=87321
It doesnt work. The result is the same with or without ORDER BY date DESC LIMIT 0,4 and every rows before the current rows are selected.
Somebody know why ?
There is an example of the expected result:
+-------------+--------------------+--------+
| #date:=date | value | somme |
+-------------+--------------------+--------+
| 2019-11-17 | 6 | NULL | SUM OF 0 previous values not null
| 2019-11-18 | 1 | 6 | SUM OF 1 previous values not null
| 2019-11-19 | 2 | 7 | SUM OF 2 previous values not null
| 2019-11-20 | NULL | 9 | SUM OF 3 previous values not null
| 2019-11-21 | 5 | 9 | SUM OF 4 previous values not null
| 2019-11-22 | 8 | 14 | SUM OF 4 previous values not null
| 2019-11-23 | NULL | 16 | SUM OF 4 previous values not null
| 2019-11-24 | 3 | 16 | SUM OF 4 previous values not null
| 2019-11-25 | 4 | 18 | SUM OF 4 previous values not null
| 2019-11-26 | 6 | 20 | SUM OF 4 previous values not null
| 2019-11-27 | 1 | 21 | SUM OF 4 previous values not null
| 2019-11-28 | 10 | 14 | SUM OF 4 previous values not null
+-------------+--------------------+--------+
Thanks for your help :)
With a self join and aggregation:
select m.date, m.id_patient, m.value,
sum(mm.value) somme
from mytable m left join mytable mm
on mm.id_patient = m.id_patient and mm.value is not null and mm.date < m.date
and (
select count(*) from mytable
where id_patient = m.id_patient and value is not null
and date >= mm.date and date < m.date
) <= 4
where m.id_patient = '87321'
group by m.date, m.id_patient, m.value
See the demo.
Results:
| date | id_patient | value | somme |
| ---------- | ---------- | ----- | ----- |
| 2019-11-17 | 87321 | 6 | |
| 2019-11-18 | 87321 | 1 | 6 |
| 2019-11-19 | 87321 | 2 | 7 |
| 2019-11-20 | 87321 | | 9 |
| 2019-11-21 | 87321 | 5 | 9 |
| 2019-11-22 | 87321 | 8 | 14 |
| 2019-11-23 | 87321 | | 16 |
| 2019-11-24 | 87321 | 3 | 16 |
| 2019-11-25 | 87321 | 4 | 18 |
| 2019-11-26 | 87321 | 6 | 20 |
| 2019-11-27 | 87321 | 1 | 21 |
| 2019-11-28 | 87321 | 10 | 14 |
The sum() already took place on all rows before you limit the results. Limit first the result before applying the sum() function using another subquery.
SELECT #date:=date, value,
(SELECT sum(value) from
(SELECT value
FROM mytable
WHERE date<#date
AND id_patient=87321
AND value IS NOT NULL
ORDER BY date DESC LIMIT 0,4))
FROM mytable
WHERE date<#date
AND id_patient=87321
AND value IS NOT NULL
ORDER BY date DESC LIMIT 0,4
Related
I have a mysql table like this:
| data | valore | inflation |
+------------+---------+-----------+
| 2022-06-01 | 296.311 | NULL |
| 2022-05-01 | 292.296 | NULL |
| 2022-04-01 | 289.109 | NULL |
| 2022-03-01 | 287.504 | NULL |
| 2022-02-01 | 283.716 | NULL |
| 2022-01-01 | 281.148 | NULL |
| 2021-12-01 | 278.802 | NULL |
| 2021-11-01 | 277.948 | NULL |
| 2021-10-01 | 276.589 | NULL |
| 2021-09-01 | 274.310 | NULL |
| 2021-08-01 | 273.567 | NULL |
| 2021-07-01 | 273.003 | NULL |
| 2021-06-01 | 271.696 | NULL |
I would need to insert (update) inflation value, calculated as current year value / past year value.
For examle the inflation for 2022-06-01 should be given by 296.311/271.696 * 100 - 100 (or as percentage anyway).
How can I do?
Thank You
Use a self join in the UPDATE statement:
UPDATE tablename t1
INNER JOIN tablename t2 ON t2.data = t1.data - INTERVAL 1 YEAR
SET t1.inflation = t1.valore / t2.valore * 100 - 100;
See the demo.
Ho usato mySQL 8.0:
CREATE TABLE inflation_tab
(data DATE, valore FLOAT, inflazione FLOAT);
INSERT INTO inflation_tab VALUES
('2022-06-01', 296.311, NULL),
('2022-05-01', 292.296, NULL),
('2022-04-01', 289.109, NULL),
('2022-03-01', 287.504, NULL),
('2022-02-01', 283.716, NULL),
('2022-01-01', 281.148, NULL),
('2021-12-01', 278.802, NULL),
('2021-11-01', 277.948, NULL),
('2021-10-01', 276.589, NULL),
('2021-09-01', 274.310, NULL),
('2021-08-01', 273.567, NULL),
('2021-07-01', 273.003, NULL),
('2021-06-01', 271.696, NULL);
SET #last_val=271.696;
WITH cte AS (
SELECT data, valore, ROUND(valore/#last_val*100 - 100, 3) AS inflazione
FROM inflation_tab IT
)
UPDATE inflation_tab IT INNER JOIN cte ON (IT.data=cte.data)
SET IT.inflazione=cte.inflazione
I have the following tables structure and trying to make a report from these:
___BillableDatas
|--------|------------|---------|--------------|------------|
| BIL_Id | BIL_Date |BIL_Rate | BIL_Quantity | BIL_Status |
|--------|------------|---------|--------------|------------|
| 1 | 2018-03-01 | 105 | 1 | charged |
| 2 | 2018-03-02 | 105 | 1 | cancelled |
| 3 | 2018-03-01 | 15 | 2 | notcharged |
| 4 | 2018-03-01 | 21 | 1 | notcharged |
| 5 | 2018-03-02 | 15 | 2 | notcharged |
| 6 | 2018-03-02 | 21 | 1 | notcharged |
|--------|------------|---------|--------------|------------|
___SalesTaxes
|--------|--------------|------------|
| STX_Id | STX_TaxeName | STX_Amount |
|--------|--------------|------------|
| 8 | Tax 1 | 5.000 |
| 9 | Tax 2 | 5.000 |
| 10 | Tax 3 | 19.975 |
|--------|--------------|------------|
STX_Amount is a percentage.
___ApplicableTaxes
|-----------|-----------|
| ATX_BILId | ATX_STXId |
|-----------|-----------|
| 1 | 8 |
| 1 | 9 |
| 1 | 10 |
| 2 | 8 |
| 2 | 9 |
| 2 | 10 |
| 3 | 9 |
| 3 | 10 |
| 4 | 9 |
| 5 | 9 |
| 5 | 10 |
| 6 | 9 |
|-----------|-----------|
ATX_BILId is the item ID link with ___BillableDatas.
ATX_STXId is the tax ID link with ___SalesTaxes.
I need to get to sum of the items per day
- without tax
- with tax
So mething like this:
|------------------|---------------|------------|
| BIL_RateNonTaxed | BIL_RateTaxed | BIL_Status |
|------------------|---------------|------------|
| 105.00 | 136.47 | charged | <- Taxes #8, #9 and #10 applicable
| 102.00 | 118.035 | notcharged | <- Taxes #9 and #10 applicable
|------------------|---------------|------------|
Explications on the totals:
105 = 105*1 -- (total of the charged item multiply by the quantity)
102 = (15*2)*2+(21*2) -- (total of the notcharged items multiply by the quantity)
136.47 = 105+(105*(5+5+19.975)/100)
119.085 = 102+(((15*2)*2)*(5+19.975)/100+(21*2)*5/100)
My last try was this one:
SELECT
BIL_Date,
(BIL_Rate*BIL_Quantity) AS BIL_RateNonTaxed,
(((BIL_Rate*BIL_Quantity)*SUM(STX_Amount)/100)+BIL_Rate*BIL_Quantity) AS BIL_RateTaxed,
BIL_Status
FROM ___BillableDatas
LEFT JOIN ___SalesTaxes
ON FIND_IN_SET(STX_Id, BIL_ApplicableTaxes) > 0
LEFT JOIN ___ApplicableTaxes
ON ___BillableDatas.BIL_Id = ___ApplicableTaxes.ATX_BILId
WHERE BIL_BookingId=1
GROUP BY BIL_Id AND BIL_Status
ORDER BY BIL_Date
ASC
Please see this SQLFiddle to help you if needed:
http://sqlfiddle.com/#!9/425854f
Thanks.
I cannot bear to work with your naming policy, so I made my own...
DROP TABLE IF EXISTS bills;
CREATE TABLE bills
(bill_id SERIAL PRIMARY KEY
,bill_date DATE NOT NULL
,bill_rate INT NOT NULL
,bill_quantity INT NOT NULL
,bill_status ENUM('charged','cancelled','notcharged')
);
INSERT INTO bills VALUES
(1,'2018-03-01',105,1,'charged'),
(2,'2018-03-02',105,1,'cancelled'),
(3,'2018-03-01',15,2,'notcharged'),
(4,'2018-03-01',21,1,'notcharged'),
(5,'2018-03-02',15,2,'notcharged'),
(6,'2018-03-02',21,1,'notcharged');
DROP TABLE IF EXISTS sales_taxes;
CREATE TABLE sales_taxes
(sales_tax_id SERIAL PRIMARY KEY
,sales_tax_name VARCHAR(12) NOT NULL
,sales_tax_amount DECIMAL(5,3) NOT NULL
);
INSERT INTO sales_taxes VALUES
( 8,'Tax 1', 5.000),
( 9,'Tax 2', 5.000),
(10,'Tax 3',19.975);
DROP TABLE IF EXISTS applicable_taxes;
CREATE TABLE applicable_taxes
(bill_id INT NOT NULL
,sales_tax_id INT NOT NULL
,PRIMARY KEY(bill_id,sales_tax_id)
);
INSERT INTO applicable_taxes VALUES
(1, 8),
(1, 9),
(1,10),
(2, 8),
(2, 9),
(2,10),
(3, 9),
(3,10),
(4, 9),
(5, 9),
(5,10),
(6, 9);
SELECT bill_status
, SUM(bill_rate*bill_quantity) nontaxed
, SUM((bill_rate*bill_quantity)+(bill_rate*bill_quantity*total_sales_tax/100)) taxed
FROM
( SELECT b.*
, SUM(t.sales_tax_amount) total_sales_tax
FROM bills b
JOIN applicable_taxes bt
ON bt.bill_id = b.bill_id
JOIN sales_taxes t
ON t.sales_tax_id = bt.sales_tax_id
GROUP
BY bill_id
) x
GROUP
BY bill_status;
+-------------+---------+-------------+
| bill_status | untaxed | total |
+-------------+---------+-------------+
| charged | 105 | 136.4737500 |
| cancelled | 105 | 136.4737500 |
| notcharged | 102 | 119.0850000 |
+-------------+---------+-------------+
My answer is very slightly different from yours, so one of us has made a mistake somewhere. Either way, this should get you pretty close.
SELECT a.BIL_Date, BIL_RateNonTaxed, BIL_RateNonTaxed+BIL_RateTaxed AS BIL_RateTaxed FROM (
SELECT BIL_Date,
SUM(BIL_Rate*BIL_Quantity) AS BIL_RateNonTaxed
FROM ___BillableDatas
WHERE BIL_Status != 'cancelled'
GROUP BY BIL_Date
) a INNER JOIN (
SELECT BIL_Date,
(((BIL_Rate*BIL_Quantity)*SUM(STX_Amount)/100)) AS BIL_RateTaxed
FROM ___BillableDatas
LEFT JOIN ___ApplicableTaxes
ON ___BillableDatas.BIL_Id = ___ApplicableTaxes.ATX_BILId
LEFT JOIN ___SalesTaxes
ON STX_Id = ATX_STXId
WHERE BIL_Status != 'cancelled'
GROUP BY BIL_Date
) b
ON a.BIL_Date = b.BIL_Date
ORDER BY a.BIL_Date;
Explanation:
Your BIL_RateNonTaxed calculation is not using the ___SalesTaxes table, so it must not appear on the query otherwise it would interfere the SUM function.
Howerver, your BIL_RateTaxed does use the ___SalesTaxes table. In that case, I solved by creating 2 subqueries and joining the results.
I know there are better answers, but I'm not familiar with MySQL syntax.
I tried a lot, but I cannot figure out a way to do this:
I have a table with (not unique) IDs and dates. All entries should be selected in the end, but they need to be sorted.
Table:
+----+------------+
| id | date |
+----+------------+
| 1 | 2017-12-10 |
| 1 | 2015-05-22 |
| 7 | 2016-04-05 |
| 2 | 2017-12-12 |
| 2 | 2014-03-10 |
| 7 | 2016-01-14 |
| 1 | 2016-08-17 |
+----+------------+
What I need:
+----+------------+
| id | date |
+----+------------+
| 2 | 2017-12-12 |
| 2 | 2014-03-10 |
| 1 | 2017-12-10 |
| 1 | 2016-08-17 |
| 1 | 2015-05-22 |
| 7 | 2016-04-05 |
| 7 | 2016-01-14 |
+----+------------+
I need everything "grouped" by the ids, starting with the id that has the most recent date linked to it.
id: 2 / date: 2017-12-12
has the most recent date, so now all rows with Id 2 follow, ordered by the date descending. After that, which "block" of ids comes next is determined again by the next most recent date and so on.
Using a subquery that groups by id, we get the max date, then joining this to the source data gives us the max date on every row to sort by.
SQL Fiddle
MySQL 5.6 Schema Setup:
CREATE TABLE Table1
(`id` int, `date` datetime)
;
INSERT INTO Table1
(`id`, `date`)
VALUES
(1, '2017-12-10 00:00:00'),
(1, '2015-05-22 00:00:00'),
(7, '2016-04-05 00:00:00'),
(2, '2017-12-12 00:00:00'),
(2, '2014-03-10 00:00:00'),
(7, '2016-01-14 00:00:00'),
(1, '2016-08-17 00:00:00')
;
Query 1:
select t.*
from table1 t
inner join (
select id, max(`date`) maxdate
from table1
group by id
) g on t.id = g.id
order by g.maxdate DESC, t.id, t.date DESC
Results:
| id | date |
|----|----------------------|
| 2 | 2017-12-12T00:00:00Z |
| 2 | 2014-03-10T00:00:00Z |
| 1 | 2017-12-10T00:00:00Z |
| 1 | 2016-08-17T00:00:00Z |
| 1 | 2015-05-22T00:00:00Z |
| 7 | 2016-04-05T00:00:00Z |
| 7 | 2016-01-14T00:00:00Z |
if your table is stack
SELECT * FROM `stack` LEFT OUTER JOIN (SELECT * FROM `stack` GROUP BY `id` )t1 ON
`stack`.`id` = t1.`id` ORDER BY t1.`date` DESC,`stack`.`date` DESC
I have the following two tables:
Table TempUser22 : 57,000 rows:
+------+-----------+
| Id | Followers |
+------+-----------+
| 874 | 55542 |
| 1081 | 330624 |
| 1378 | 17919 |
| 1621 | 920 |
| 1688 | 255463 |
| 2953 | 751 |
| 3382 | 204466 |
| 3840 | 273489 |
| 4145 | 376 |
| ... | ... |
+------+-----------+
Table temporal_users : 10,000,000 rows total, 3200 rows Where Date=2010-12-31:
+---------------------+---------+--------------------+
| Date | User_Id | has_original_tweet |
+---------------------+---------+--------------------+
| 2008-02-22 12:00:00 | 676493 | 2 |
| 2008-02-22 12:00:00 | 815263 | 1 |
| 2008-02-22 12:00:00 | 6245822 | 1 |
| 2008-02-22 12:00:00 | 8854092 | 1 |
| 2008-02-23 12:00:00 | 676493 | 2 |
| 2008-02-23 12:00:00 | 815263 | 1 |
| 2008-02-23 12:00:00 | 6245822 | 1 |
| 2008-02-23 12:00:00 | 8854092 | 1 |
| 2008-02-24 12:00:00 | 676493 | 2 |
| ............. | ... | .. |
+---------------------+---------+--------------------+
I am running the following join query on these tables:
SELECT sum(has_original_tweet), b.Id
FROM temporal_users AS a
RIGHT JOIN TempUser22 AS b
ON a.User_ID = b.Id
GROUP BY b.Id;
Which returns 57,00 rows as expected, with NULL answers on the first field:
+-------------------------+------+
| sum(has_original_tweet) | Id |
+-------------------------+------+
| NULL | 874 |
| NULL | 1081 |
| 135 | 1378 |
| 164 | 1621 |
| 652 | 1688 |
| 691 | 2953 |
| NULL | 3382 |
| NULL | 3840 |
| NULL | 4145 |
| ... | .... |
+-------------------------+------+
However, when adding the WHERE line specifying a date as below:
SELECT sum(has_original_tweet), b.Id
FROM temporal_users AS a
RIGHT JOIN TempUser22 AS b
ON a.User_ID = b.Id
WHERE a.Date BETWEEN '2010-12-31-00:00:00' AND '2010-12-31-23:59:59'
GROUP BY b.Id;
I receive the following answer, of only 3200 rows, and without any NULL in the first field.
+-------------------------+---------+
| sum(has_original_tweet) | Id |
+-------------------------+---------+
| 1 | 797194 |
| 1 | 815263 |
| 0 | 820678 |
| 1 | 1427511 |
| 0 | 4653731 |
| 1 | 5933862 |
| 2 | 7530552 |
| 1 | 7674072 |
| 1 | 8149632 |
| .. | .... |
+-------------------------+---------+
My question is: How to get, for a given date, an answer of size 57,000 rows for each user in TempUser22 with NULL values when has_original_tweet is not present in temporal_user for the given date?
Thanks.
SELECT b.Id, SUM(a.has_original_tweet) s
FROM TempUser22 b
LEFT JOIN temporal_users a ON b.Id = a.User_Id
AND a.Date BETWEEN '2010-12-31-00:00:00' AND '2010-12-31-23:59:59'
GROUP BY b.Id;
Id s
1 null
2 1
3 null
4 3
5 null
6 null
For debugging, I used:
CREATE TEMPORARY TABLE TempUser22(Id INT, Followers INT)
SELECT 1 Id, 10 Followers UNION ALL
SELECT 2, 20 UNION ALL
SELECT 3, 30 UNION ALL
SELECT 4, 40 UNION ALL
SELECT 5, 50 UNION ALL
SELECT 6, 60
;
CREATE TEMPORARY TABLE temporal_users(`Date` DATETIME, User_Id INT, has_original_tweet INT)
SELECT '2008-02-22 12:00:00' `Date`, 1 User_Id, 1 has_original_tweet UNION ALL
SELECT '2008-12-31 12:00:00', 2, 1 UNION ALL
SELECT '2010-12-31 12:00:00', 2, 1 UNION ALL
SELECT '2012-12-31 12:00:00', 2, 1 UNION ALL
SELECT '2008-12-31 12:00:00', 4, 9 UNION ALL
SELECT '2010-12-31 12:00:00', 4, 1 UNION ALL
SELECT '2010-12-31 12:00:00', 4, 2 UNION ALL
SELECT '2012-12-31 12:00:00', 4, 9
;
That's because NULL values will always be discarded from the where clause
You can use a coalesce in your where clause.
WHERE coalesce(a.Date, 'some-date-in-the-range') BETWEEN '2010-12-31-00:00:00' AND '2010-12-31-23:59:59'
With this instead, you force null values to be considered as valid.
I have seen solutions for something similar on other posts, but I am having an issue applying it to my problem.
I have this table for voucher last history:
t_release:
+------------+---------+----------------+-------------------+
| release_id | code_id | code_status_id | code_created_date |
+------------+---------+----------------+-------------------+
| 1 | 32 | 2 | 4/28/2016 8:54 |
| 1 | 32 | 2 | 4/28/2016 8:54 |
| 2 | 32 | 3 | 4/28/2016 8:55 |
| 3710 | 32 | 2 | 6/18/2016 10:20 |
| 4 | 33 | 2 | 4/28/2016 9:54 |
| 5 | 33 | 2 | 4/28/2016 10:54 |
| 3711 | 33 | 2 | 6/18/2016 11:20 |
| 6 | 34 | 2 | 4/28/2016 11:54 |
| 7 | 34 | 3 | 4/28/2016 0:54 |
| 3712 | 34 | 2 | 6/18/2016 0:20 |
+------------+---------+----------------+-------------------+
And
r_code_status:
+----------------+-------------+
| code_status_id | code_status |
+----------------+-------------+
| 1 | Available |
| 2 | Requesting |
| 3 | Paid |
+----------------+-------------+
When I running:
SELECT
t1.release_id,
t1.code_id,
t1.code_status_id,
t1.code_created_date
FROM t_release t1
LEFT JOIN t_release t2 ON t1.code_id = t2.code_id AND t1.release_id < t2.release_id
WHERE ISNULL(t2.release_id)
+------------+---------+----------------+-------------------+
| release_id | code_id | code_status_id | code_created_date |
+------------+---------+----------------+-------------------+
| 3710 | 32 | 2 | 6/18/2016 10:20 |
| 3711 | 33 | 2 | 6/18/2016 11:20 |
| 3712 | 34 | 2 | 6/18/2016 0:20 |
+------------+---------+----------------+-------------------+
I need if code_id meet code_status_id = '3' or 'Paid', the query can retrieve it as last history, else if code_id with code_status_id = '2' then last id(release_id) retrieved.
I want the result like this:
+------------+---------+----------------+-------------------+
| release_id | code_id | code_status_id | code_created_date |
+------------+---------+----------------+-------------------+
| 2 | 32 | 3 | 4/28/2016 08:55 |
| 3711 | 33 | 2 | 6/18/2016 11:20 |
| 7 | 34 | 3 | 4/28/2016 0:54 |
+------------+---------+----------------+-------------------+
DDL:
create table t_release (
release_id INTEGER(11) NOT NULL AUTO_INCREMENT,
code_id INTEGER(11) DEFAULT NULL,
code_status_id TINYINT(4) DEFAULT NULL,
code_created_date DATETIME DEFAULT NULL,
PRIMARY KEY (`release_id`)
) ENGINE=InnoDB
AUTO_INCREMENT=1 ROW_FORMAT=DYNAMIC CHARACTER SET 'utf8';
INSERT INTO t_release
(`release_id`,`code_id`,`code_status_id`,`code_created_date`)
VALUES
(1, '32', '2', '2016-4-28 8:54'),
(2, '32', '3', '2016-4-28 8:55'),
(3710, '32', '2', '2016-6-18 10:20'),
(4, '33', '2', '2016-4-28 9:54'),
(5, '33', '2', '2016-4-28 10:54'),
(3711, '33', '2', '2016-6-18 11:20'),
(6, '34', '2', '2016-4-28 11:54'),
(7, '34', '3', '2016-4-28 0:54'),
(3712, '34', '2', '2016-6-18 0:20');
and this is link sqlfiddle: http://sqlfiddle.com/#!9/87843
SELECT * FROM t_release t where release_id IN
(SELECT DISTINCT release_id FROM t_release WHERE code_status_id = 3)
UNION ALL
SELECT * FROM t_release t where release_id IN
(SELECT MAX(release_id) FROM t_release x WHERE
NOT EXISTS (SELECT 1 FROM t_release y WHERE code_status_id = 3 and y.code_id = x.code_id))
GROUP BY code_id)
First query will fetch all those that are paid, the other will fetch the last release_id of those that remain unpaid.
You can achieve this using MySQL user defined variables
SELECT
t.release_id,
t.code_id,
t.code_status_id,
t.code_created_date
FROM
(
SELECT
*,
IF(#sameCodeID = code_id, #rn := #rn + 1,
IF(#sameCodeID := code_id, #rn := 1, #rn := 1)
) AS row_number
FROM t_release t
CROSS JOIN (SELECT #sameCodeID := 0, #rn := 1) AS var
ORDER BY code_id, code_status_id DESC, release_id DESC
) AS t
WHERE t.row_number <= 1
ORDER BY t.code_id
SEE DEMO