I have 3 tables, user_table to classify as a teacher or student, subject_table to mention different subject, and enrolled_table stating who registered as teacher/student and for which subject.
I want to have count of student, teacher count for each subject!
user_table
user_id | user_name | contact_no | password | flag
6 | Abhis | 123456788 | 123 | s
5 | Abhish | 123456789 | 123 | s
8 | Sneha | 1111111111 | 123 | s
7 | Snehil | 1111112222 | 123 | s
1 | Narsingh | 1234567890 | 123 | t
2 | Abhinav | 1234567891 | 1234 | t
3 | Abhi | 1234567892 | 123 | s
4 | Abhishek | 1234567893 | 123 | s
subject_table
sub_id | sub_name
3 | CSS
1 | HTML
2 | JQUERY
enrolled_table
enr_id | sub_id | user_id | date | start_time | end_time
1 | 1 | 1 | 2016-04-01 | 09:00:00 | 10:00:00
2 | 2 | 1 | 2016-04-01 | 10:00:00 | 11:00:00
3 | 3 | 1 | 2016-04-01 | 11:00:00 | 12:00:00
4 | 1 | 5 | 2016-04-01 | 09:00:00 | 10:00:00
5 | 1 | 6 | 2016-04-01 | 12:00:00 | 13:00:00
6 | 1 | 7 | 2016-04-01 | 12:00:00 | 13:00:00
7 | 1 | 2 | 2016-04-01 | 13:00:00 | 14:00:00
8 | 2 | 2 | 2016-04-01 | 13:00:00 | 14:00:00
SELECT
st.sub_name,
COUNT(CASE WHEN ut.flag = 's' THEN 1 END) AS students,
COUNT(CASE WHEN ut.flag = 't' THEN 1 END) AS teachers
FROM
subject_table st
INNER JOIN enrolled_table et ON et.sub_id = st.sub_id
INNER JOIN user_table ut ON ut.user_id = et.user_id
GROUP BY
st.sub_name
Related
**NOTE:** $cur_time = Current Time
I want to select the current schedule of a professor based on the current time by selecting the data where tbl_from_time >= $cur_time <= tbl_to_time.
i.e.
DATABASE TABLE: 'schedule'
| sched_id | weekday | from_time | to_time | room | prof_id | date_created |
+----------+---------+-----------+----------+------------+---------+---------------------+
| 7 | 1 | 07:30:00 | 09:00:00 | SR12 | 1 | 2017-05-14 11:16:54 |
| 2 | 1 | 12:00:00 | 13:30:00 | ORC 12 | 1 | 2017-05-14 11:21:47 |
| 3 | 1 | 10:30:00 | 12:00:00 | Comp Lab 6 | 1 | 2017-05-14 11:22:20 |
| 4 | 1 | 09:00:00 | 10:30:00 | Comp Lab 4 | 1 | 2017-05-14 13:00:57 |
| 8 | 1 | 07:30:00 | 08:32:00 | Comp Lab 3 | 1 | 2017-05-15 14:42:28 |
| 9 | 1 | 06:45:00 | 07:45:00 | Comp Lab 3 | 1 | 2017-05-15 14:45:45 |
| 10 | 1 | 18:00:00 | 19:07:00 | Comp Lab 3 | 1 | 2017-05-15 18:07:48 |
| 11 | 2 | 12:02:00 | 01:56:00 | Sample | 1 | 2017-05-16 10:17:45 |
MY QUERY
function get_current_schedule($prof_id, $cur_time, $weekday) {
$this->db->select()->from('schedule');
$this->db->where('prof_id', $prof_id);
$this->db->where('weekday', $weekday);
$where = "'$cur_time' BETWEEN from_time and to_time";
$this->db->where($where);
$result = $this->db->get();
return $result->first_row();
}
Here is the query to get data between form and to.
function get_current_schedule($prof_id, $cur_time, $weekday) {
$result = $this->db->select()
->from('schedule')
->where('prof_id', $prof_id)
->where('weekday', $weekday)
->where('from_time >=', $cur_time)
->where('tbl_to_time <=', $cur_time)
->get();
return $result->first_row();
}
I have a set of posts on monthly basis. i need an array which contains total records of posts posted in each month (including zeros).
I fail to write it in dql :( Any ideas plz ?
Sample Data:
+----+---------------------+
| id | date |
+----+---------------------+
| 24 | 2012-12-16 16:29:56 |
| 1 | 2013-02-25 14:57:09 |
| 2 | 2013-02-25 14:59:37 |
| 4 | 2013-02-25 15:12:44 |
| 5 | 2013-02-25 15:14:18 |
| 7 | 2013-02-26 11:31:31 |
| 8 | 2013-02-26 11:31:59 |
| 10 | 2013-02-26 11:34:47 |
| 14 | 2013-03-04 04:39:02 |
| 15 | 2013-03-04 05:44:44 |
| 16 | 2013-03-04 05:48:29 |
| 19 | 2013-03-07 15:22:34 |
| 20 | 2013-03-15 12:24:43 |
| 21 | 2013-03-16 16:27:43 |
| 22 | 2013-03-16 16:29:28 |
| 23 | 2013-03-16 16:29:56 |
| 11 | 2013-03-17 11:35:12 |
+----+---------------------+
SQL query:
SELECT count(b.id) as totalRec
FROM (
SELECT 'January' mnth
UNION ALL
SELECT 'February' mnth
UNION ALL
SELECT 'March' mnth
) a
LEFT JOIN post b
ON a.mnth = DATE_FORMAT(b.date, '%M') AND
year(b.date) = '2013' AND
DATE_FORMAT(b.date, '%M') IN ('January', 'February', 'March')
GROUP BY year(b.date)-month(b.date)
ORDER BY b.date ASC
OUTPUT
+----------+
| totalRec |
+----------+
| 0 |
| 7 |
| 9 |
+----------+
Count all posts grouped by month, and filter out the months you want:
SELECT count(*) as totalRec, DATE_FORMAT(b.date, '%m%Y') as month
FROM post b
WHERE month IN('012013', '022013', '032013')
GROUP BY month
ORDER BY b.date ASC
Will result in:
+----------+----------+
| totalRec | month |
+----------+----------+
| 0 | 012013 |
| 7 | 022013 |
| 9 | 032013 |
+----------+----------+
I am looking to set start time for first 20 students of a particular subject
at 9:00 AM and end time 10:00 AM, for the next 20 i want the time for the same subject 10:00 AM- 11 AM , this should happen for each subject, also i want that only 1 teacher for that particular subject should be alloted for each time slot,
i have tried a lot but still unable to come up with the solution.
user_table
user_id | user_name | contact_no | password | flag
6 | Abhis | 123456788 | 123 | s
5 | Abhish | 123456789 | 123 | s
8 | Sneha | 1111111111 | 123 | s
7 | Snehil | 1111112222 | 123 | s
1 | Narsingh | 1234567890 | 123 | t
2 | Abhinav | 1234567891 | 1234 | t
3 | Abhi | 1234567892 | 123 | s
4 | Abhishek | 1234567893 | 123 | s
subject_table
sub_id | sub_name
3 | CSS
1 | HTML
2 | JQUERY
enr_id | sub_id | user_id | date |
1 | 1 | 1 | 2016-04-01 |
2 | 2 | 1 | 2016-04-01 |
3 | 3 | 1 | 2016-04-01 |
4 | 1 | 5 | 2016-04-01 |
5 | 1 | 6 | 2016-04-01 |
6 | 1 | 7 | 2016-04-01 |
7 | 1 | 2 | 2016-04-01 |
8 | 2 | 2 | 2016-04-01 |
I have the following 3 tables: unit, stage, stats.
unit stage
+----+--------+ +----+-------+---------------------+
| id | status | | id |unit_id| date |
+----+--------+ +----+-------+---------------------+
| 1 | 2 | | 1 | 2 | 2013-11-22 00:00:00 |
| 2 | 3 | | 2 | 2 | 2013-11-26 12:00:00 |
| 3 | 3 | | 3 | 3 | 2013-10-11 00:00:00 |
| 4 | 0 | | 4 | 1 | 2013-12-29 00:00:00 |
+----+--------+ +----+-------+---------------------+
stats
+----+----------+---------------------+-------+
| id | stage_id | date | clicks|
+----+----------+---------------------+-------+
| 1 | 1 | 2013-11-22 00:00:00 | 10 |
| 2 | 1 | 2013-11-23 00:00:00 | 20 |
| 3 | 1 | 2013-11-24 00:00:00 | 25 |
| 4 | 2 | 2013-11-26 00:00:00 | 15 |
| 5 | 2 | 2013-11-27 12:00:00 | 21 |
| 6 | 3 | 2013-12-29 00:00:00 | 8 |
+----+----------+---------------------+-------+
I need a request, that will produce the following response:
+---------+---------------------+-----------------------+
| unit.id | stage.min.date | sum(stats.max.clicks) |
+---------+---------------------+-----------------------+
| 2 | 2013-11-22 00:00:00 | 46 |
| 3 | 2013-12-29 00:00:00 | 8 |
+---------+---------------------+-----------------------+
by the following rules:
1) unit.id - show only units with unit.status=3
2) stage.min.date - minimal stage.date for corresponding unit_id
3) sum(stats.max.clicks) - sum of stats.clicks with max dvalues for each stage_id associated with corresponding unit_id. In my example 46 = 25(stage_id=1) + 21(stage_id=2)
The problem is in min.date and sum of clicks - I have no idea how to get it in one query. Definitely it`s not a problem to do it using php code and several requests.
Schema in SQL Fiddle
Thanks in advance.
I just ask myself, why I do this? Your example resonse has an error, and does not match your fiddle... but:
SELECT
cc.unit_id, MIN(cc.date) as stage_min_date , SUM(dd.clicks) as stats_max_clicks
FROM
stage cc
LEFT JOIN (
SELECT
bb.stage_id, bb.clicks
FROM
stats bb LEFT JOIN (
SELECT id, stage_id, MAX(date) AS max_date
FROM stats
GROUP BY stage_id
) aa
ON
aa.max_date = bb.date
WHERE
aa.max_date IS NOT NULL
) dd
ON cc.id = dd.stage_id
LEFT JOIN unit ee
ON ee.id = cc.unit_id
WHERE ee.status = 3
GROUP BY cc.unit_id
...
I have two tables tc_users and tc_timeclock. I want to grab the current status of each active user. But only those that have made an update (clocked in or out) within the last 12 hours. I have gotten as far as figuring out how to grab all of the updates within the last 12 hours but not how to narrow it down to just the last one for each user.
Here are my tables:
tc_users:
user_id | first_name | last_name | active
-----------------------------------------
1 | Frank | Zappa | 1
2 | John | Mcneely | 1
3 | Bill | Mckenna | 1
4 | Mark | langdon | 1
5 | Steve | Mcalister | 0
6 | William | Stevens | 1
7 | John | Jones | 0
tc_timeclock:
tc_id | user_id | status | time_stamp
-------------------------------------
1 | 1 | IN | 2012-11-28 09:00:25
2 | 2 | IN | 2012-11-28 09:01:25
3 | 3 | IN | 2012-11-26 09:03:25
4 | 4 | IN | 2012-11-28 09:21:25
5 | 5 | IN | 2012-11-28 09:01:12
6 | 6 | IN | 2012-11-28 09:47:13
7 | 7 | IN | 2012-11-12 09:00:22
8 | 7 | OUT | 2012-11-12 09:03:28
9 | 5 | OUT | 2012-11-28 09:21:25
10 | 6 | OUT | 2012-11-28 11:47:13
11 | 3 | OUT | 2012-11-26 11:03:25
12 | 2 | OUT | 2012-11-28 11:01:25
13 | 1 | OUT | 2012-11-28 11:27:25
14 | 4 | OUT | 2012-11-28 11:21:25
15 | 4 | IN | 2012-11-28 12:21:25
16 | 1 | IN | 2012-11-28 12:27:25
17 | 3 | IN | 2012-11-26 12:03:25
18 | 6 | IN | 2012-11-28 12:47:13
19 | 2 | IN | 2012-11-28 12:01:25
20 | 1 | OUT | 2012-11-28 17:27:25
21 | 4 | OUT | 2012-11-28 17:21:25
22 | 3 | OUT | 2012-11-26 17:03:25
23 | 2 | OUT | 2012-11-28 17:01:25
So far This is the query I have come up with :
if now = 2012-11-28 18:00:00
SELECT first_name, last_name, status, time_stamp
FROM tc_timeclock
INNER JOIN tc_users
ON tc_timeclock.user_id = tc_users.user_id
WHERE tc_users.active = 1
AND tc_timeclock.time_stamp BETWEEN '2012-11-28 06:00:00' AND '2012-11-28 18:00:00'
This displays everything within the last 12 hours like so:
first_name | last_name | status | time_stamp
-----------------------------------------------------
Frank | Zappa | IN | 2012-11-28 09:00:25
John | Mcneely | IN | 2012-11-28 09:01:25
Mark | langdon | IN | 2012-11-28 09:21:25
William | Stevens | IN | 2012-11-28 09:47:13
William | Stevens | OUT | 2012-11-28 11:47:13
John | Mcneely | OUT | 2012-11-28 11:01:25
Frank | Zappa | OUT | 2012-11-28 11:27:25
Mark | langdon | OUT | 2012-11-28 11:21:25
Mark | langdon | IN | 2012-11-28 12:21:25
Frank | Zappa | IN | 2012-11-28 12:27:25
William | Stevens | IN | 2012-11-28 12:47:13
John | Mcneely | IN | 2012-11-28 12:01:25
Frank | Zappa | OUT | 2012-11-28 17:27:25
Mark | langdon | OUT | 2012-11-28 17:21:25
John | Mcneely | OUT | 2012-11-28 17:01:25
The logic I am looking for:
Grab the first and last name, current status and time the status was logged for each active user within the last 12 hours.
So output should be:
first_name | last_name | status | time_stamp
Frank | Zappa | OUT | 2012-11-28 17:27:25
John | Mcneely | OUT | 2012-11-28 17:01:25
Mark | langdon | OUT | 2012-11-28 17:21:25
William | Stevens | IN | 2012-11-28 12:47:13
User 5 (Steve Mcalister) and 7 (John Jones) are not active so they are not displayed.
User 3 (Bill Mckenna)has no activity within the last 12 hours so he is also no diplayed.
Am I missing something really simple? (I am sure I am)
I have a work around if there is not a simple solution, I could always add two columns to the tc_users table: (tc_users.current_status, and tc_users.status_time) and simply make those the same as the last entry into the tc_timeclock table.
That way I could easily select them, I don't like the idea of making the data redundant though if I don't have to.
Something like this might work for you.
SQL:
SELECT `tc_users`.`first_name`, `tc_users`.`last_name`, `tc_timeclock`.`status`, `temp_table`.`last_updated`
FROM `tc_users`
INNER JOIN `tc_timeclock`
ON `tc_users`.`user_id`=`tc_timeclock`.`user_id`
INNER JOIN (
SELECT `user_id`, MAX(`time_stamp`) AS `last_updated` FROM `tc_timeclock` GROUP BY `user_id`
) AS `temp_table`
ON `tc_users`.`user_id`=`temp_table`.`user_id`
AND `tc_timeclock`.`time_stamp`=`temp_table`.`last_updated`
WHERE `tc_users`.`active`!=0
AND `temp_table`.`last_updated`>(NOW() - INTERVAL 12 HOUR);
OUT:
+------------+-----------+--------+---------------------+
| first_name | last_name | status | last_updated |
+------------+-----------+--------+---------------------+
| William | Stevens | IN | 2012-11-28 12:47:13 |
| Frank | Zappa | OUT | 2012-11-28 17:27:25 |
| Mark | langdon | OUT | 2012-11-28 17:21:25 |
| John | Mcneely | OUT | 2012-11-28 17:01:25 |
+------------+-----------+--------+---------------------+
HTH
Query:
SQLFIDDLEExample
SELECT first_name, last_name, tml.status, tml.time_stamp
FROM (SELECT *
FROM tc_timeclock tl
WHERE tl.time_stamp = (SELECT MAX(time_stamp)
FROM tc_timeclock
WHERE tl.user_id = user_id )) tml
INNER JOIN tc_users
ON tml.user_id = tc_users.user_id
WHERE tc_users.active = 1
AND tml.time_stamp BETWEEN '2012-11-28 06:00:00' AND '2012-11-28 18:00:00'
Result:
| FIRST_NAME | LAST_NAME | STATUS | TIME_STAMP |
---------------------------------------------------------------------
| Frank | Zappa | OUT | November, 28 2012 17:27:25+0000 |
| John | Mcneely | OUT | November, 28 2012 17:01:25+0000 |
| Mark | langdon | OUT | November, 28 2012 17:21:25+0000 |
| William | Stevens | IN | November, 28 2012 12:47:13+0000 |