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.
Related
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.
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 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
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