MYSQL Stored procedure, approvals and approvers - mysql

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;

Related

Find record less than or equals 0 but no repeat record

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`;

How to recursively find the children of parent in MySQL

Unable to write out the SQL Query based on complex logic defined below.
Database: MySQL
Input :
id userId parentid
----------------------
1 1 0
2 2 1
3 3 1
4 4 1
5 5 2
6 6 3
7 7 0
8 8 0
Output Expected if userId = 1 :
id userId parentid
----------------------
1 1 0
2 2 1
3 3 1
4 4 1
5 5 2
6 6 3
Logic Used:
if userId = 1 then check for parentId whose value is 1, the records are (we have to include the record of userID =1 as well)
id userId parentid
----------------------
1 1 0
2 2 1
3 3 1
4 4 1
Now in above record set check user id again in this example apart form userId = 1, there are three userId's i.e. 2, 3, 4. Now we have to see whose parent id is 2, 3 and 4. Now records are.
id userId parentid
----------------------
1 1 0
2 2 1
3 3 1
4 4 1
5 5 2
6 6 3
if we userId column again new userid visible are 5 and 6, but there is no parent id for 5 and 6. So this the final result set.

Get most viewed url by users in a day

I have a table whose structure like this
id time userid ip course module cmid action url info
1 1441006367 2 110.142.152.217 1 user 0 update view.php?id=2&course=1
2 1441006397 2 110.142.152.217 1 course 0 view view.php?id=1 1
3 1441061491 2 110.142.152.217 1 user 0 view view.php?id=0&course=1 2
4 1441061491 2 110.142.152.217 1 course 0 view view.php?id=1 1
5 1441067607 2 110.142.152.217 1 course 0 view view.php?id=1 1
6 1441067617 2 110.142.152.217 1 course 0 view view.php?id=1 1
7 1441067646 2 110.142.152.217 1 course 0 view view.php?id=1 1
8 1441067681 2 110.142.152.217 1 course 0 view view.php?id=1 1
9 1441067774 2 110.142.152.217 1 course 0 view view.php?id=1 1
10 1441069218 2 110.142.152.217 1 course 0 view view.php?id=1 1
11 1441071815 2 110.142.152.217 1 course 0 view view.php?id=1 1
12 1441071815 2 110.142.152.217 1 course 0 login view.php?id=1 1
13 1441080275 2 110.142.152.217 1 user 0 view view.php?id=0&course=1 2
14 1441080275 2 110.142.152.217 1 course 0 view view.php?id=1 1
15 1441080275 2 110.142.152.217 1 course 0 view view.php?id=1 1
16 1441082380 2 110.142.152.217 1 course 0 view view.php?id=1 1
17 1441082494 2 110.142.152.217 1 course 0 view view.php?id=1 1
18 1441082498 2 110.142.152.217 1 user 0 logout view.php?id=2&course=1 2
19 1441082504 2 110.142.152.217 1 user 0 login view.php?id=0&course=1 2
20 1441082505 2 110.142.152.217 1 user 0 login view.php?id=0&course=1 2
21 1441082508 2 110.142.152.217 1 user 0 login view.php?id=0&course=2 2
22 1441082508 2 110.142.152.217 1 user 0 loam view.DhD?id=0&course=1 2
I want to get most viewed url by users in a day where each url belongs to different course. One user can view multiple url but not same course.
I tried this query
SELECT count(DISTINCT `course`),`userid`,`course`,`module`,`url`, FROM_UNIXTIME(`time`,'%Y-%m-%d') as date FROM `mdl_log` where `time`>unix_timestamp('2016-06-01 00:00:00') AND `time`<unix_timestamp('2016-06-02 00:00:00') GROUP BY `course`
When i run this it restrict same course by a user which is right but this restrict also for other user.
For example if one user viewed same course 2 time then it count one(that's right) but it not count for user who viewed 1 time.Means they not getting same course for other user. Please help me what i am doing wrong..
Because you are counting the column you are grouping by ! that is totally wrong.
You should count DISTINCT USER_ID :
SELECT count(DISTINCT `userid`),`userid`,`course`,`module`,`url`, FROM_UNIXTIME(`time`,'%Y-%m-%d') as date
FROM `mdl_log`
where `time`>unix_timestamp('2016-06-01 00:00:00') AND
`time`<unix_timestamp('2016-06-02 00:00:00')
GROUP BY `course`
I don't understand why are you even selecting userid ? It will be randomly picked because it's not a part of the group by clause.

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