Greetings, I've got a query that I'm struggling with, this is the first time that I am encountering this type of query.
I have two table as shown below.
xid is the primary key in parent_tbl1, while xid is the foreign key in child_tbl2
parent_tbl1
xid pub
1 1
2 1
3 0
4 1
child_tbl2
id ttype fno xid qnty
1 A 0 1 0
2 A 1 1 3
3 B 1 1 4
4 A 1 2 1
5 A 1 3 2
6 A 1 4 3
7 A 1 4 1
8 A 1 1 1
Below is the exlanation of the query in parts, which will then need to make up the whole query.
I need the SUM of qnty in child_tbl2:
1) Who's parent's pub is '1'
Therefore, id 5 is eliminated from child_tbl2, this is because xid 3 is 0 in parent_tbl1
Results:
child_tbl2
id ttype fno xid qnty
1 A 0 1 0
2 A 1 1 3
3 B 1 1 4
4 A 1 2 1
6 A 1 4 3
7 A 1 4 1
8 A 1 1 1
2) AND who's parent table has ttype 'A' in the child table
Therefore, id 3 is eliminated from the existing results because id 3's ttype is B
Results:
child_tbl2
id ttype fno xid qnty
1 A 0 1 0
2 A 1 1 3
4 A 1 2 1
6 A 1 4 3
7 A 1 4 1
8 A 1 1 1
3) AND who's parent has '0' as one it's fno's in the child_tbl2
Therefore, id 4, 6 & 7 are eliminated from the existing results, this is because 0 was not found in one of their fno's, while 0 was found as one of xid 1's fno
Results:
child_tbl2
id ttype fno xid qnty
1 A 0 1 0
2 A 1 1 3
8 A 1 1 1
The answer for the query should be 4
Below is what i've got.
SELECT sum(child_tbl2.qnty), parent_tbl1.xid, parent_tbl1.pub, child_tbl2.ttype, child_tbl2.fno, child_tbl2.xid
FROM parent_tbl1, child_tbl2
WHERE parent_tbl1.xid = child_tbl2.xid
AND parent_tbl1.pub = '1'
AND child_tbl2.ttype = 'A'
AND child_tbl2.fno ?
If it is possible, I do not know how to tell the dbms (MySQL) to check if Zero is one of the fno's.
If I say "AND child_tbl2.fno = '0'", I will be saying that the result's fno should be 0. I do not want that, I need zero to be one of the fno's in order for the query to SUM all the qnty in that particular xid
SELECT SUM(DISTINCT src.qnty) as qnty
FROM tbl2 AS src
INNER JOIN tbl1 AS pub
ON src.xid=pub.xid
INNER JOIN tbl2 AS fno
ON pub.xid=fno.xid
WHERE pub.pub=1
AND src.ttype='A'
AND fno.fno=0
Related
How dose MySql sort data when the data of sort filed is duplicate?
The table:
CREATE TABLE `orderby_test` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`sort1` bigint(20) NOT NULL,
`sort2` bigint(20) NOT NULL,
`a` bigint(20) NOT NULL ,
`b` bigint(20) NOT NULL ,
PRIMARY KEY (`id`),
KEY `idx_sort` (`sort1`, `sort2`)
) ENGINE=InnoDB AUTO_INCREMENT=1;
sql :
select id, sort1,sort2,a,b from orderby_test where sort1 = 1 and a = 1 and b = 1 order by sort2 desc limit 0, 1000
then insert data 3 times:
insert into orderby_test (sort1,sort2,a,b) values (1,3,1,1);
then select, result is:
id sort1 sort2 a b
1 1 3 1 1
2 1 3 1 1
3 1 3 1 1
the id is ASC
then insert data 20 times:
insert into orderby_test (sort1,sort2,a,b) values (1,3,1,1);
then select, result is:
id sort1 sort2 a b
12 1 3 1 1
23 1 3 1 1
22 1 3 1 1
21 1 3 1 1
20 1 3 1 1
19 1 3 1 1
18 1 3 1 1
17 1 3 1 1
16 1 3 1 1
15 1 3 1 1
14 1 3 1 1
13 1 3 1 1
1 1 3 1 1
11 1 3 1 1
10 1 3 1 1
9 1 3 1 1
8 1 3 1 1
7 1 3 1 1
6 1 3 1 1
5 1 3 1 1
4 1 3 1 1
3 1 3 1 1
2 1 3 1 1
id is not sorted!
why?
if you do more insert
the result :
select id, sort1,sort2,a,b from orderby_test where sort1 = 1 and a = 1 and b = 1 order by sort2 desc limit 0, 20
id sort1 sort2 a b
282 1 3 1 1
281 1 3 1 1
280 1 3 1 1
279 1 3 1 1
278 1 3 1 1
277 1 3 1 1
276 1 3 1 1
275 1 3 1 1
274 1 3 1 1
273 1 3 1 1
272 1 3 1 1
271 1 3 1 1
270 1 3 1 1
269 1 3 1 1
268 1 3 1 1
267 1 3 1 1
266 1 3 1 1
265 1 3 1 1
259 1 3 1 1
258 1 3 1 1
id is sorted again!
because the implement of B+ tree in innodb? how does mysql do it?
mysql version : 5.7.21-21-log
How does MySql sort data when the data of the sort filed is duplicate?
It doesn't.
Once MySQL has enough information to arrange the result-set in such a way that it complies with the ORDER BY clause, it just packs the rows with ties as fast as it can, not caring at all about its relative order.
This is not specific to MySQL, it's how SQL is designed to behave. Sorting has a cost (time, CPU, memory...); there's no point in paying such cost when sorting is not needed.
You need to specify the second ordering preference:
...ORDER BY sort2 DESC, id ASC ...
I have 1 table called itemmovement : It has Item Id , Quantity In , Quantity Out , Invoice Id, Date. I need to make in one query to show how many pieces are sold and beside the sold column there will be the current on hand quantity .
itemmovement
Id itemid qtyin qtyout invid purchasereturnid date
1 1 10 2019-01-04
2 2 8 2019-01-06
3 2 2 1 2019-01-08
4 1 3 2 2019-01-12
5 2 1 2019-02-04
6 3 4 2019-03-04
7 1 1 3 2019-04-04
8 1 1 1 2019-04-14
9 3 1 2 2019-04-24
I need the query to show this result
Id itemid Sold Quantity OnHandQty
1 1 4 5
2 2 2 7
3 3 0 3
I'm Trying to use this query but not working
SELECT *
FROM
(SELECT itmv.itemid,
sum(itmv.qtyout)-sum(itmv.qtyin)
FROM itemmovement itmv
WHERE (itmv.systemdate BETWEEN '2019-01-01' AND '2019-06-01')
AND invid>0
GROUP BY itmv.itemid) AS result1,
(SELECT sum(itmv2.qtyin)-sum(itmv2.qtyout)
FROM itemmovement itmv2
WHERE itmv.itemid=itmv2.itemid
GROUP BY itmv2.itemid) AS result2
ORDER BY sum(itmv.qtyin)-sum(itmv.qtyout)
I'm getting :
Unknown column 'itmv.itemid' in 'where clause it for this syntax :
where itmv.itemid = itmv2.itemid
Here's your query.
select itemid
, sum(case when COALESCE(invid,0) > 0 then qtyout else 0 end) as Sold_Qantity
, sum(qtyin)-sum(qtyout) as OnHandQty
from itemmovement
group by itemid
I need to select user_id & quiz_id, for users which their count of questions in their quiz = sum of correct, this mean they answer 100% correct
answers table:
quiz_id question_id user_id answer_id correct
1 1 1 1 1
1 2 1 6 0
1 3 1 9 1
2 1 2 1 1
2 2 2 5 1
3 4 1 17 1
3 5 1 21 1
3 6 1 25 1
4 1 3 1 1
5 4 4 18 0
6 1 5 1 1
6 2 5 5 1
7 1 3 2 0
7 2 3 7 0
ex 1:
user 1 took "quiz_id" = 1
count of questions in "quiz_id = 1" = 3
sum of correct = 2
so it's not 100%
user_id = 1 in quiz_id = 1 => will not selected
but user_id = 1 will be selected with quiz_id = 3 cause he got 100%
expected results:
quiz_id user_id
2 2
3 1
4 3
6 5
notes:
quiz could be taken with different users with different number of
questions
quiz_id, user_id unique together (user can not take same quiz twice)
thanks,
You should use an aggregate query with HAVING clause:
SELECT quiz_id, user_id
FROM quiz_answer -- or whatever the name is
GROUP BY quiz_id, user_id
HAVING COUNT(question_id) = SUM(correct)
here you must use HAVING instead of WHERE because
The HAVING clause can refer to aggregate functions, which the WHERE
clause cannot
as specified in the docs.
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;
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