Group condition in last data mysql - mysql

I have a data like this :
Table LOT
+-------+--------+
|Lot_id | Prod_id|
+-------+--------+
| LOT-1 | Prd-1 |
| LOT-1 | Prd-2 |
| LOT-1 | Prd-3 |
| LOT-2 | Prd-4 |
+-------+--------+
Table Process
+-------+--------+--------+------------+----------+
|proc_id|proc_cat|proc_seq|proc_prod_id|t_proc_qty|
+-------+--------+--------+------------+----------+
| 1 | Proc-A | 1 | Prd-1 | 100 |
| 2 | Proc-H | 2 | Prd-1 | 100 |
| 3 | Proc-D | 3 | Prd-1 | 100 |
| 4 | Proc-A | 1 | Prd-2 | 100 |
| 5 | Proc-H | 2 | Prd-2 | 100 |
| 6 | Proc-D | 3 | Prd-2 | 20 |
| 7 | Proc-Q | 4 | Prd-2 | 20 |
| 8 | Proc-A | 1 | Prd-3 | 100 |
| 9 | Proc-H | 2 | Prd-3 | 100 |
| 10 | Proc-D | 3 | Prd-3 | 50 |
| 11 | Proc-O | 1 | Prd-4 | 80 |
| 12 | Proc-F | 2 | Prd-4 | 80 |
| 13 | Proc-H | 3 | Prd-4 | 80 |
+-------+--------+--------+------------+----------+
And i want data like this if i want select just LOT=LOT-1.
table LOT joined to table Process and data is accumulated sum(t_proc_qty) from last proc_seq each proc_prod_id and group by proc_cat and order by proc_seq
+--------+--------+----------+
|proc_cat|proc_seq|t_proc_qty|
+--------+--------+----------+
| Proc-D | 3 | 150 |->accumulated from Prd-1 and prd-3 in last process is seq 3
| Proc-Q | 4 | 20 |->accumulated from Prd-2 in last process is seq 4
+--------+--------+----------+
What queries I use in MySQL ?
I stucked in query
SELECT proc_cat, proc_seq, SUM(t_proc_qty)
FROM Process
LEFT JOIN Lot ON proc_prod_id=Prod_id
WHERE Lot_id='LOT-1'
GROUP BY proc_prod_id
ORDER BY proc_seq DESC LIMIT 1
this schema for trial query SQLFiddle

From table Process you want the records with the highest proc_id per proc_prod_id:
select *
from process
where not exists
(
select *
from process later
where later.proc_prod_id = process.proc_prod_id
and later.proc_id > process.proc_id
);
From this data you want an aggregate per proc_cat and proc_sec. And you also want to consider only prod_id for 'LOT-1' in table LOT.
The complete query:
select proc_cat, proc_seq, sum(t_proc_qty)
from process
where proc_prod_id in (select prod_id from lot where lot_id = 'LOT-1')
and not exists
(
select *
from process later
where later.proc_prod_id = process.proc_prod_id
and later.proc_id > process.proc_id
)
group by proc_cat, proc_seq
order by proc_cat, proc_seq;
SQL fiddle: http://sqlfiddle.com/#!9/1fa3fd/5

Related

What is the execution order of the associative sub-query SQL?

I came across this SQL at work, This was written by my colleague. Although there are better solutions, I’m just curious,and now I have simplified it as follows:
-- Calculate the total of the 'APPROVING' salary and the 'AGENT' salary already actual paid
SELECT ifnull(sum(l.salary),0) +
(SELECT ifnull(sum(l1.salary),0)
FROM salary_header h1 JOIN salary_lines l1
ON h1.salary_id = l1.salary_id
WHERE h1.status='APPROVING' AND l1.project_id = l.project_id)
FROM salary_pay_headers h JOIN salary_pay_lines l
ON h.salary_pay_id = l.salary_pay_id
WHERE h.pay_type='AGENT'
AND l.project_id=9904
mysql> select * from salary_header;
+-----------+-----------+
| salary_id | status |
+-----------+-----------+
| 1 | APPROVING |
| 2 | PAID |
+-----------+-----------+
mysql> select * from salary_lines;
+----------------+-----------+------------+--------+
| salary_line_id | salary_id | project_id | salary |
+----------------+-----------+------------+--------+
| 1 | 2 | 9905 | 200.00 |
+----------------+-----------+------------+--------+
mysql> select * from salary_pay_headers;
+---------------+----------+
| salary_pay_id | pay_type |
+---------------+----------+
| 1 | AGENT |
| 2 | OTHER |
+---------------+----------+
mysql> select * from salary_pay_lines;
+--------------------+---------------+------------+--------+
| salary_pay_line_id | salary_pay_id | project_id | salary |
+--------------------+---------------+------------+--------+
| 1 | 1 | 9904 | 3.05 |
| 2 | 1 | 9904 | 201.37 |
| 3 | 1 | 9904 | 6.10 |
| 4 | 1 | 9904 | 10.17 |
| 5 | 1 | 9904 | 6.44 |
| 6 | 1 | 9904 | 9.15 |
| 8 | 3 | 9905 | 100.00 |
+--------------------+---------------+------------+--------+
Its result is not 3.05+201.37+6.10+10.17+6.44+9.15=236.28 as I expected,but 236.28+200=436.28,obviously that one in the salary_line is not filtered out. I have spent the whole afternoon on this problem, so I really want to know the execution order of this SQL.

How to do a ORDER BY inside a INNER JOIN to get last status update?

I need to get records of 2 tables using filters and sorting.
Tables: ra_pedidos, ra_pedido_has_pedido_status
It worked perfectly in MySQL5.0, but now in 5.5 it does not bring the correct result anymore.
I need in the same line to bring the date of the first status and last change of status of each request and ignore the canceled ones.
Example search (ra_pedido_has_pedido_status):
SELECT id_pedido, id_pedido_status, data
FROM ra_pedido_has_pedido_status
WHERE id_pedido IN (86291, 86745, 86622);
Result:
+-----------+------------------+---------------------+
| id_pedido | id_pedido_status | data |
+-----------+------------------+---------------------+
| 86291 | 1 | 2017-10-30 11:16:12 |
| 86291 | 2 | 2017-10-30 14:14:53 |
| 86291 | 3 | 2017-10-31 08:18:47 |
| 86291 | 11 | 2017-11-07 12:08:04 |
| 86622 | 1 | 2017-11-04 12:23:21 |
| 86622 | 2 | 2017-11-04 12:47:33 |
| 86622 | 3 | 2017-11-06 08:24:20 |
| 86622 | 90 | 2017-11-07 08:40:55 |
| 86745 | 1 | 2017-11-07 10:59:51 |
| 86745 | 2 | 2017-11-07 11:09:46 |
+-----------+------------------+---------------------+
Now report SQL:
[EDIT] Explain: JOIN get first match row, then the ORDER BY on SELECT inside this join force to get last row. Note that id_pedido 86622 should not list because it is canceled (last id_pedido_status = 90), and this is filtered in HAVING
SELECT o.id_pedido AS id, pus.id_pedido_status, pus.data
FROM ra_pedido AS o
INNER JOIN
(
SELECT ra_pedido_has_pedido_status.*
FROM ra_pedido_has_pedido_status
ORDER BY data DESC
) AS pus ON o.id_pedido = pus.id_pedido
WHERE o.id_pedido IN (86291, 86745, 86622)
GROUP BY o.id_pedido
HAVING id_pedido_status < 90
ORDER BY data DESC;
This is the result using MySQL 5.0, the expected:
+-------+------------------+---------------------+
| id | id_pedido_status | data |
+-------+------------------+---------------------+
| 86291 | 11 | 2017-11-07 12:08:04 |
| 86745 | 2 | 2017-11-07 11:09:46 |
+-------+------------------+---------------------+
And this is the result using MySQL 5.5, not expected:
+-------+------------------+---------------------+
| id | id_pedido_status | data |
+-------+------------------+---------------------+
| 86745 | 1 | 2017-11-07 10:59:51 |
| 86622 | 1 | 2017-11-04 12:23:21 |
| 86291 | 1 | 2017-10-30 11:16:12 |
+-------+------------------+---------------------+
What do I need to change in SQL?
Thanks and sorry my english...

How should I write this MySQL query containing multiple Left Joins

I have a query consisting of multiple joins and I am wondering whether it can be re-written to improve performance.
I have 2 tables as follows (I have removed non-important columns for this example):
slots
------------------------------------------
| id | name | slot_1 | slot_2 | slot_3 |
------------------------------------------
| 1 | Bob | 1 | 2 | 3 |
| 2 | Jim | 4 | 3 | 3 |
| 3 | Alf | 1 | 2 | 5 |
------------------------------------------
(There are 25 slots in total, each in it's own column)
slot_details
-----------------------------------
| id | stat_1 | stat_2 | stat_3 |
-----------------------------------
| 1 | 1 | 5 | 6 |
| 2 | 4 | 31 | 23 |
| 3 | 6 | 5 | 7 |
| 4 | 7 | 4 | 9 |
| 5 | 2 | 3 | 5 |
-----------------------------------
(There are 10 stats in total)
The query is as follows:
SELECT
slots.name,
slot_1_details.stat_1 AS slot_1_stat_1,
slot_1_details.stat_2 AS slot_1_stat_2,
slot_1_details.stat_3 AS slot_1_stat_3,
slot_2_details.stat_1 AS slot_2_stat_1,
slot_2_details.stat_2 AS slot_2_stat_2,
slot_2_details.stat_3 AS slot_2_stat_3,
slot_3_details.stat_1 AS slot_3_stat_1,
slot_3_details.stat_2 AS slot_3_stat_2,
slot_3_details.stat_3 AS slot_3_stat_3
FROM
slots
LEFT JOIN
slot_details AS slot_1_details
ON (
slot_1_details.id = slots.slot_1
)
LEFT JOIN
slot_details AS slot_2_details
ON (
slot_2_details.id = slots.slot_2
)
LEFT JOIN
slot_details AS slot_3_details
ON (
slot_3_details.id = slots.slot_3
)
WHERE (
slots.id = 1
)
The expected outcome of this query would be as follows:
| name | slot_1_stat_1 | slot_1_stat_2 | slot_1_stat_3 | slot_2_stat_1 | slot_2_stat_2 | slot_2_stat_3 | slot_3_stat_1 | slot_3_stat_2 | slot_3_stat_3 |
|bob | 1 | 5 | 6 | 4 | 31 | 23 | 6 | 5 | 7 |
Unfortunately I am not in a situation where I can change the tables.
Thank you for the help!
maybe
SELECT * FROM slots s LEFT JOIN slot_details sd ON s.id=sd.id
but i'm not sure because the query you posted is very confusing.
what are the keys of those tables?

MYSQL - how to string comparisons and query?

+--------------------+---------------+------+-----+---------+-------+
| ID | GKEY |GOODS | PRI | COUNTRY | Extra |
+--------------------+---------------+------+-----+---------+-------+
| 1 | BOOK-1 | 1 | 10 | | |
| 2 | PHONE-1 | 2 | 12 | | |
| 3 | BOOK-2 | 1 | 13 | | |
| 4 | BOOK-3 | 1 | 10 | | |
| 5 | PHONE-2 | 2 | 10 | | |
| 6 | PHONE-3 | 2 | 20 | | |
| 7 | BOOK-10 | 2 | 20 | | |
| 8 | BOOK-11 | 2 | 20 | | |
| 9 | BOOK-20 | 2 | 20 | | |
| 10 | BOOK-21 | 2 | 20 | | |
| 11 | PHONE-30 | 2 | 20 | | |
+--------------------+---------------+------+-----+---------+-------+
Above is my table. I want to get all records which GKEY > BOOK-2, Who can tell me the expression with mysql?
Using " WHERE GKEY>'BOOK-2' " Cannot get the correct results.
How about (something like):
(this is MSSQL - I guess it will be similar in MySQL)
select
*
from
(
select
*,
index = convert(int,replace(GKEY,'BOOK-',''))
from table
where
GKEY like 'BOOK%'
) sub
where
sub.index > 2
By way of explanation: The inner query basically recreates your table, but only for BOOK rows, and with an extra column containing the index in the right data type to make a greater than comparison work numerically.
Alternatively something like this:
select
*
from table
where
(
case
when GKEY like 'BOOK%' then
case when convert(int,replace(GKEY,'BOOK-','')) > 2 then 1
else 0
end
else 0
end
) = 1
Essentially the problem is that you need to check for BOOK before you turn the index into a numberic, as the other values of GKEY would create an error (without doing some clunky string handling).
SELECT * FROM `table` AS `t1` WHERE `t1`.`id` > (SELECT `id` FROM `table` AS `t2` WHERE `t2`.`GKEY`='BOOK-2' LIMIT 1)

select random value from each type

I have two tables, rating:
+-----------+-----------+-------------+----------+
| rating_id | entity_id | rating_code | position |
+-----------+-----------+-------------+----------+
| 1 | 1 | Quality | 0 |
| 2 | 1 | Value | 0 |
| 3 | 1 | Price | 0 |
+-----------+-----------+-------------+----------+
And rating_option
+-----------+-----------+------+-------+----------+
| option_id | rating_id | code | value | position |
+-----------+-----------+------+-------+----------+
| 1 | 1 | 1 | 1 | 1 |
| 2 | 1 | 2 | 2 | 2 |
| 3 | 1 | 3 | 3 | 3 |
| 4 | 1 | 4 | 4 | 4 |
| 5 | 1 | 5 | 5 | 5 |
| 6 | 2 | 1 | 1 | 1 |
| 7 | 2 | 2 | 2 | 2 |
| 8 | 2 | 3 | 3 | 3 |
| 9 | 2 | 4 | 4 | 4 |
| 10 | 2 | 5 | 5 | 5 |
| 11 | 3 | 1 | 1 | 1 |
| 12 | 3 | 2 | 2 | 2 |
| 13 | 3 | 3 | 3 | 3 |
| 14 | 3 | 4 | 4 | 4 |
| 15 | 3 | 5 | 5 | 5 |
+-----------+-----------+------+-------+----------+
I need a SQL query (not application level, must stay in the database) which will select a set of ratings randomly. A sample result would look like this, but would pick a random value for each rating_id on subsequent calls:
+-----------+-----------+------+-------+----------+
| option_id | rating_id | code | value | position |
+-----------+-----------+------+-------+----------+
| 1 | 1 | 1 | 1 | 1 |
| 8 | 2 | 3 | 3 | 3 |
| 15 | 3 | 5 | 5 | 5 |
+-----------+-----------+------+-------+----------+
I'm totally stuck on the random part, and grouping by rating_id has been a crap shoot so far. Any MySQL ninjas want to take a stab?
Thanks,
Joe
EDIT: I've tried rand() in a bunch of combinations, and I'm sure that it will be necessary to create the randomness of the result, but I cannot figure out how to return one random row for each of the rows in rating. I cannot use order by rand() limit 1 because I need three rows, and order by rand() limit 3 won't give me one of each rating_id, which is the ultimate goal. I need a combination of rand() and either subqueries or joins so that I can ensure one of each rating_id.
Alright, a little messy, but seems to do the job. Someone may know what they're doing better than I do that can clean this up:
SELECT random.rating_id, random.rand_option_id, r3.code, r3.value, r3.position
FROM
(SELECT r.rating_id,
(SELECT r2.option_id
FROM rating_option r2
WHERE r2.rating_id = r.rating_id
ORDER BY RAND() LIMIT 1) AS 'rand_option_id'
FROM rating_option r
GROUP BY r.rating_id
) random
LEFT JOIN rating_option AS r3 ON r3.option_id = rand_option_id
Results (varies every time, of course):
+-----------+----------------+------+-------+----------+
| rating_id | rand_option_id | code | value | position |
+-----------+----------------+------+-------+----------+
| 1 | 4 | 4 | 4 | 4 |
| 2 | 6 | 1 | 1 | 1 |
| 3 | 13 | 3 | 3 | 3 |
+-----------+----------------+------+-------+----------+
You could use the rand() function to do sorting in a select on the rating table.
For example:
select rating_id from rating order by rand() limit 1
As clarified in your comments, and the other posts above
select * from rating_option order by rand()
will return all records in a random order... However, if you want only X number, then inclue that as the limit as noted by others
select * from rating_option order by rand() limit 5 (or whatever number)
Have you looked into the rand() function?
SELECT column FROM table
ORDER BY RAND()
LIMIT 1
http://www.petefreitag.com/item/466.cfm
http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand
Sample code
select *
from rating_option
group by rating_id
order by rand()