Find record less than or equals 0 but no repeat record - mysql

I have 2 tables, 1 first look this:
Table state_inventary
ID_STATE_INVENTARY, DESCRIPTION
0 STORE
1 TRANSIT
2 SOLD_STORE
3 STORAGE
Table article_stock
ID_STOCK ID_ORIGIN ARTICLE UNIT_SOLD ID_STATE_INVENTARY
0 1 A 10 0
1 2 A 0 1
2 1 B 5 2
3 3 C 0 3
4 4 D 0 3
5 5 E 10 1
6 2 A 0 2
7 1 B 0 2
I need to find articles with ID_STATE_INVENTORY with value 0 or 2 or 3, I get it
But I need to find articles with the UNIT_SOLD the sum is zero, I don't know how do these
I want to find somthing like that
ID_STOCK ID_ORIGIN ARTICLE UNIT_SOLD ID_STATE_INVENTARY
3 3 C 0 3
4 4 D 0 3
OR
ARTICLE
C
D
In my query I have next result
ID_STOCK ID_ORIGIN ARTICLE UNIT_SOLD ID_STATE_INVENTARY
1 2 A 0 1
3 3 C 0 3
4 4 D 0 3
6 2 A 0 2
7 1 B 0 2
Anyone idea how can I do?

Try this.
SELECT SUM(`UNIT_SOLD`) AS `UNIT_SOLD`, `ARTICLE` FROM `table_name` GROUP BY `ARTICLE`;

Related

How to write a query to calculate number of items sold by dates in MySQL

I have 2 tables (Product and Order) like this:
Product Table:
id
name
1
A
2
B
3
C
Order Table:
id
user_id
product_id
date
quantity
1
1
1
2022-07-01
2
2
1
1
2022-07-02
3
3
1
2
2022-07-01
5
4
1
1
2022-07-02
2
5
1
1
2022-07-03
12
6
1
3
2022-07-05
2
7
1
2
2022-07-05
1
expect table
Name
2022-07-01
2022-07-02
2022-07-03
2022-07-04
2022-07-05
A
2
3
12
0
0
B
5
0
0
0
1
C
0
0
0
0
2
How to write a MySQL query to get the result like the 'expect table'

Deriving new fields that show a value dependent on the prior record in MySQL

Please note: this question is an extension / modification of a question I asked here that was expertly and graciously answered by
#Abhik Chakraborty
I have a MySQL table full of outcomes from a chess tournament:
P1_id P2_id Outcome_for_P1 Day
1 2 W 2015-12-07
1 3 W 2015-12-06
1 4 D 2015-12-05
1 5 L 2015-12-04
1 6 D 2015-12-03
1 7 D 2015-12-02
1 8 L 2015-12-01
2 1 L 2015-12-07
2 3 W 2015-12-06
2 4 W 2015-12-05
2 5 W 2015-12-04
2 6 L 2015-12-03
2 7 D 2015-12-02
2 8 W 2015-12-01
I've realized I need to derive 3 new columns. In my previous question, I was trying to track P1_id's record throughout the tournament, by showing the outcome of the chess game that is represented by each row. This was as such:
P1_id P2_id Outcome_for_P1 P1_W P1_L P1_D Day
1 2 W 2 2 3 2015-12-07
1 3 W 1 2 3 2015-12-06
1 4 D 0 2 3 2015-12-05
1 5 L 0 2 2 2015-12-04
1 6 D 0 1 2 2015-12-03
1 7 D 0 1 1 2015-12-02
1 8 L 0 1 0 2015-12-01
2 1 L 4 2 1 2015-12-07
2 3 W 4 1 1 2015-12-06
2 4 W 3 1 1 2015-12-05
2 5 W 2 1 1 2015-12-04
2 6 L 1 1 1 2015-12-03
2 7 D 1 0 1 2015-12-02
2 8 W 1 0 0 2015-12-01
This can be done using the code on this SQL Fiddle. Problem solved.
BUT I am having trouble modifying this SQL to populate the columns in a slightly different way. In this new situation, I'd like to show the competitor's Outcome before the conclusion of the game. It would show what the player's record was going into the chess match. In other words, how can I come up with this:
P1_id P2_id Outcome_for_P1 P1_W P1_L P1_D Day
1 2 W 1 2 3 2015-12-07
1 3 W 0 2 3 2015-12-06
1 4 D 0 2 2 2015-12-05
1 5 L 0 1 2 2015-12-04
1 6 D 0 1 1 2015-12-03
1 7 D 0 1 0 2015-12-02
1 8 L 0 0 0 2015-12-01
2 1 L 4 1 1 2015-12-07
2 3 W 3 1 1 2015-12-06
2 4 W 2 1 1 2015-12-05
2 5 W 1 1 1 2015-12-04
2 6 L 1 0 1 2015-12-03
2 7 D 1 0 0 2015-12-02
2 8 W 0 0 0 2015-12-01
I'm struggling a bit because I don't think it can be done by the code on the Fiddle. I feel like a whole new approach might be necessary, but I'm not sure the best approach.
To resolve this issue you can implement an unique id that increments by 1 with each match, then only add up stats for matches that have an ID less than the current one
The sample from your fiddle-link doesn't work for me.
But here is an other solution: Join your matches with all previous matches from the same person and count the outcomes using SUM
SELECT c.P1_id, c.P2_id, c.Outcome_for_P1,
SUM(IF(c1.Outcome_for_P1='W',1,0)) P1_W,
SUM(IF(c1.Outcome_for_P1='L',1,0)) P1_L,
SUM(IF(c1.Outcome_for_P1='D',1,0)) P1_D,
c.Day
FROM chess c
LEFT JOIN chess c1
ON c1.P1_id = c.P1_id
AND c1.Day < c.Day
GROUP BY c.P1_id, c.P2_id, c.Outcome_for_P1, c.Day
ORDER BY c.P1_id ASC, c.Day DESC;
http://sqlfiddle.com/#!9/641efb/44

MYSQL Stored procedure, approvals and approvers

Im kinda new reading stored procedure.
I was thinking if this is posible to do in store procedure in mysql.
I have sequence of approval process called step. approved column; 1 is yes 0 is no.
Basically I have Step 1 to 3..in my approval sequence.
if step 1 approved status is 0 he will be the first to approved or see the table.
if step 1 approve is 1. step 2 can now see the table.
Transaction Steps Table:
id transaction_id approver_id step approved
1 1 1 1 1
2 1 2 2 0
3 1 3 3 0
4 2 3 1 1
5 2 1 2 1
6 2 2 3 0
7 3 2 1 0
8 3 3 2 0
9 3 1 3 0
10 4 1 1 1
11 4 3 2 0
12 4 2 3 0
Example If my Approval id = 2
In My View:I can only see all those next in que approvals
id transaction_id approver_id step approved
2 1 2 2 0
6 2 2 3 0
7 3 2 1 0
pls let me know if this is possible. thank you
If I understand correctly, you want rows that are the first non-approved for each transaction and the approver is 2.
Try this:
select ts.*
from transactionsteps ts join
(select transaction_id, min(step) as minstep
from transactionsteps
where approved = 0
group by transaction_id
) t
on ts.transaction_id = t.transaction_id and
ts.step = t.minstep
where approver_id = 2;

How to get differents values 'types' from table creating virtual columns - MySQL

I have these tables.
tb_employee:
ID_EMP NAME_EMP
1 Employee 1
2 Employee 2
3 Employee 3
4 Employee 4
tb_requirements:
ID_REQ DESCRIPTION_REQ TYPE_REQ
1 Requirement 1 1
2 Requirement 2 1
3 Requirement 3 1
4 Requirement 4 2
5 Requirement 5 2
6 Requirement 6 2
7 Requirement 7 2
tb_detail:
ID_DET ID_EMP ID_REQ
1 1 1
2 1 2
3 1 4
4 2 1
5 2 6
6 3 4
7 3 7
I need to make a SELECT QUERY to count how many requirements each employee has got and which type, like this:
ID_EMP NAME_EMP TYPE_REQ1(virtual column) TYPE_REQ2 (virt. c.)
1 Employee 1 2 4
2 Employee 2 1 1
3 Employee 3 0 2
4 Employee 4 0 0
I really don't know how to do it.
Try this one
SELECT
e.ID_EMP
,e.NAME_EMP
,(CASE WHEN SUM(r.TYPE_REQ=1) IS NULL THEN 0 ELSE SUM(r.TYPE_REQ=1) END ) TYPE_REQ1
,(CASE WHEN SUM(r.TYPE_REQ=2) IS NULL THEN 0 ELSE SUM(r.TYPE_REQ=2) END ) TYPE_REQ2
FROM
tb_employee e
LEFT JOIN tb_detail d ON (e.ID_EMP=d.ID_EMP)
LEFT JOIN tb_requirements r ON (d.ID_REQ=r.ID_REQ)
GROUP BY e.ID_EMP

Sort a table based on one condition and the list remaining rows accordingly

I had a table with following details
cID sID Name pID childrenCount
1 1 Site 1 5
2 1 Safty 2 4
3 1 Archit 3 3
4 1 Civil 1 0
5 1 Concs 1 0
6 1 Pavm 1 0
7 1 Paint 3 0
8 1 Alum 3 0
9 1 Doors 3 0
10 1 Highw 1 0
11 1 Road 1 0
12 1 Alarm 2 0
13 1 Safty 2 0
14 1 Fence 2 0
15 1 Beaco 2 0
What I want is to write a select query to order the above table first by their childrenCount values in descending order and the list the corresponding rows according as below
cID sID Name pid childrenCount
1 1 Site 1 5
4 1 Civil 1 0
5 1 Conc 1 0
6 1 Pavm 1 0
10 1 Highw 1 0
11 1 Road 1 0
2 1 Safty 2 4
12 1 Alarm 2 0
13 1 Safty 2 0
14 1 Fence 2 0
15 1 Beacon 2 0
3 1 A WRK 3 3
7 1 Paint 3 0
8 1 Alumin 3 0
9 1 Doors 3 0
Thanks In Advance
Try this...first order by pid then childrenCount desc.
SELECT * FROM TABLENAME order by pid, childrenCount desc
Try this:
SELECT * FROM tableA ORDER BY pid, childrenCount DESC, cid