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.
Related
I have a target where I have this DUPLICATE checker with COUNT for multiple arrays. As we can see. Any suggestions on how I make this work? Thank you and have a nice day.
DATA:
id
value
1
[5,6,8,4,2]
2
[2,3,4,1,8]
3
[9,3,2,1,10]
Normal result:
number
count
1
2
2
3
3
2
4
2
5
1
6
1
7
0
8
2
9
1
10
1
This is my target result with sorting (Highest count):
number
count
2
3
1
2
3
2
4
2
8
2
5
1
6
1
10
1
9
1
I have the following tables:
customers
id name
1 customer 1
stores
id name customer_id
1 store 1 1
2 store 2 1
3 store 3 1
items
id name store_id customer_id inventory
1 item 1 1 1 10
2 item 2 2 1 12
3 item 3 2 1 18
item_attributes
id item_id store_id customer_id inventory
1 1 2 1 4
2 1 3 1 3
3 2 1 1 7
4 2 3 1 0
5 3 1 1 9
What I want is this:
item ID Item Name store 1 Store 2 Store 3
1 Item 1 10 4 3
2 Item 2 7 12 0
3 Item 3 9 18 0
A customer can have many stores.
Each store can have many items.
Other stores may or may not have the entry in the item_attributes table.
I want to get an consolidated report on item inventory.
My approach is:
Get all the items for the customer 1.
Get all the stores for the customer 1.
Get all records from the item_attributes.
foreach($i=0; $i<count($items); $i++){
foreach($j=0; $j<count($stores); $j++){
// now I get the item_id and store_id.
// check if exists in the $item_attr array with this combination.
// return the inventory if yes, 0 otherwise.
}
}
Is there any easier approach than the one I mentioned above? Any thoughts would appreciated.
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;
Consider the following table:
id a b
--------------
1 5 1
2 2 3
3 4 2
4 3 6
5 0 1
6 2 2
I would like to order it by max(a,b) in descending order, so that the result will be:
id a b
--------------
4 3 6
1 5 1
3 4 2
2 2 3
6 2 2
5 0 1
What will be the SQL query to perform such ordering ?
Use GREATEST :
SELECT *
FROM table
ORDER BY GREATEST(a, b) DESC