SELECT mf.fm_id,ema.asc_code as 'Xaxis', count(*) as 'Total'
from els_jobsheet_data as jd
join master_asc as ma on (jd.asc_id=ma.asc_id )
join els_master_asc_l3l4 as ema on jd.els_asc_code =ema.asc_code
join master_city as mc on mc.city_id=ma.city_id
join master_state as ms on ms.state_id=mc.state_id
join master_region as mr on mr.region_id=ms.region_id
join master_fso as mf on mf.fm_id=mr.fso_id
where jd.insert_time is not null AND jd.insert_time>='2015-12-01'
AND jd.insert_time<='2015-12-31'
group by Xaxis
order by Xaxis ASC
A query returned-
fm_id Xaxis Total
2 DOA 43
3 DSRO 12
1 TRC 12
9 3423 10
5 5455 11
2 2443 13
7 55AS 2
But I want to group it like
fm_id Xaxis Total
2 DOA 43
3 DSRO 12
1 TRC 12
9 Other 342
(whatever be its total)
i m newbie.Pls help..
Use a case expression instead of the raw data.
SELECT
max(mf.fm_id) as fm_id
, CASE
WHEN ema.asc_code IN ('DOA', 'DSRO', 'TRC') THEN ema.asc_code
ELSE 'Other'
END AS Xaxis
, COUNT(*) AS Total
FROM els_jobsheet_data AS jd
JOIN master_asc AS ma ON (jd.asc_id = ma.asc_id)
JOIN els_master_asc_l3l4 AS ema ON jd.els_asc_code = ema.asc_code
JOIN master_city AS mc ON mc.city_id = ma.city_id
JOIN master_state AS ms ON ms.state_id = mc.state_id
JOIN master_region AS mr ON mr.region_id = ms.region_id
JOIN master_fso AS mf ON mf.fm_id = mr.fso_id
WHERE jd.insert_time IS NOT NULL
AND jd.insert_time >= '2015-12-01'
AND jd.insert_time <= '2015-12-31'
GROUP BY
CASE
WHEN ema.asc_code IN ('DOA', 'DSRO', 'TRC') THEN ema.asc_code
ELSE 'Other'
END
ORDER BY
Xaxis ASC
Related
I am looking to generate the results into the horizontal union. I could able to combine together and generate the results. However, I need to show the results in the expected results format.
SQL
(SELECT
kpa_id AS 'KPA', SUM(weightage) AS 'Total'
FROM
pmm_question_details
WHERE
weightage NOT LIKE '%-%'
GROUP BY kpa_id) UNION (SELECT
kpa_id AS 'KPA', SUM(weightage_value) AS 'Acheived'
FROM
pmm_answer_details
WHERE
application_id = 2
AND archive_value = 'No'
GROUP BY kpa_id)
Actual Results
1 14
2 37
3 19
4 40
5 51
6 24
1 12
2 19
3 0
6 2
Expected Results
1 14 1 12
2 37 2 19
3 19 3 0
4 40 6 2
5 51
6 24
If we can assume pmm_question_Details will always have as many or more records than pmm_answer_details... then two subqueries and a left join should do the trick with a join on a uservariable rownum (RN)
SELECT A.KPA, A.Total, B.KPA, B.Acheived
FROM (SELECT kpa_id AS 'KPA'
, SUM(weightage) AS 'Total'
, #RN1 := #RN1 + 1 as RN
FROM pmm_question_details
CROSS JOIN (SELECT #RN1 :=0) r1
WHERE weightage NOT LIKE '%-%'
ORDER BY KPA
GROUP BY kpa_id) A
LEFT JOIN (SELECT kpa_id AS 'KPA'
, SUM(weightage_value) AS 'Acheived'
, #RN1 := #RN2 + 1 as RN
FROM pmm_answer_details
CROSS JOIN (SELECT #RN2 :=0) r2
WHERE application_id = 2
AND archive_value = 'No'
ORDER BY KPA
GROUP BY kpa_id) B
on A.RN = B.RN
ORDER BY A.KPA
Though I must admit I don't see why a rownumber is needed if you could just left join on the KPA_ID in the first place...
if this could be the Expected results... (and again assuming pmm_question has all the IDs which could be in pmm_answer... )
Expected Results
1 14 1 12
2 37 2 19
3 19 3 0
4 40
5 51
6 24 6 2
Then the query would just be...
SELECT A.KPA, A.Total, B.KPA, B.Acheived
FROM (SELECT kpa_id AS 'KPA', SUM(weightage) AS 'Total'
FROM pmm_question_details
WHERE weightage NOT LIKE '%-%'
GROUP BY kpa_id) A
LEFT JOIN (SELECT kpa_id AS 'KPA', SUM(weightage_value) AS 'Acheived'
FROM pmm_answer_details
WHERE application_id = 2
AND archive_value = 'No'
GROUP BY kpa_id) B
on A.KPA = B.KPA
I have a working query, but there has to be a better way to do this.
Here is the working query
SELECT gameid FROM(
SELECT gameid, SUM(count) as total FROM (
SELECT IF(vanguard = 1, 30, gameid) as gameid, count FROM (
SELECT opserv_operation.gameid, opserv_games.vanguard, COUNT(opserv_games.gameid) AS count FROM opserv_operation_attendees INNER JOIN opserv_operation ON opserv_operation_attendees.operationid = opserv_operation.operationid INNER JOIN opserv_games on opserv_operation.gameid = opserv_games.gameid WHERE (start_time >= '2015-11-11' || FIND_IN_SET(opserv_operation.operationid, '17951,17701,17702,17775,17969,17890,17958,17966,17900')) AND completed = 1 AND opserv_operation_attendees.userid = 5750 AND opserv_operation_attendees.status = 4 AND opserv_operation.type <> 5 AND opserv_operation.completed = 1 GROUP BY opserv_operation.gameid) as m
) as l
GROUP BY gameid
ORDER BY total DESC LIMIT 1) as k
The main query
SELECT opserv_operation.gameid, opserv_games.vanguard, COUNT(opserv_games.gameid) AS count FROM opserv_operation_attendees INNER JOIN opserv_operation ON opserv_operation_attendees.operationid = opserv_operation.operationid INNER JOIN opserv_games on opserv_operation.gameid = opserv_games.gameid WHERE (start_time >= '2015-11-11' || FIND_IN_SET(opserv_operation.operationid, '17951,17701,17702,17775,17969,17890,17958,17966,17900')) AND completed = 1 AND opserv_operation_attendees.userid = 5750 AND opserv_operation_attendees.status = 4 AND opserv_operation.type <> 5 AND opserv_operation.completed = 1 GROUP BY opserv_operation.gameid
gameid vanguard
16 0
36 1
36 1
36 1
16 0
36 1
27 0
16 0
36 1
36 1
36 1
30 0
36 1
36 1
27 0
36 1
36 1
36 1
So here is the tricky part. I need it to group by and count all the values however if gameid = 30 or vanguard = 1 then they should be all counted together.
So with the above data, the only thing I need is the gameid returned in this case gameid 30. It would have the most counts which would be 13.
There has to be a better way than I have done it.
Edit: So far this one works great thanks to Ed Gibbs. However is still returns two values, I'm guessing only way is to have another subquery? to get just gameid.
SELECT
CASE WHEN vanguard = 1 THEN 30 ELSE gameid END AS gameid,
SUM(count) AS total
FROM (SELECT opserv_operation.gameid, opserv_games.vanguard, COUNT(opserv_games.gameid) AS count FROM opserv_operation_attendees INNER JOIN opserv_operation ON opserv_operation_attendees.operationid = opserv_operation.operationid INNER JOIN opserv_games on opserv_operation.gameid = opserv_games.gameid WHERE (start_time >= '2015-11-11' || FIND_IN_SET(opserv_operation.operationid, '17951,17701,17702,17775,17969,17890,17958,17966,17900')) AND completed = 1 AND opserv_operation_attendees.userid = 5750 AND opserv_operation_attendees.status = 4 AND opserv_operation.type <> 5 AND opserv_operation.completed = 1 GROUP BY opserv_operation.gameid) as l
GROUP BY CASE WHEN vanguard = 1 THEN 30 ELSE gameid END
ORDER BY total DESC
LIMIT 1;
If bottom line you mean "1" and "30" have to be counted together then this should work:
SELECT
CASE WHEN vanguard = 1 THEN 30 ELSE gameid END AS gameid
FROM (... main query ...)
GROUP BY CASE WHEN gameid = 1 THEN 30 ELSE gameid END
ORDER BY SUM(count) DESC
LIMIT 1;
I've two tables as below:
1. tbl_student:
id name
1 raj
2 raja
3 raju
4 rajan
tbl_attendance
id student_id month attended_days
1 1 1 6
2 1 2 16
3 8 1 8
4 7 2 14
5 8 2 13
6 7 1 11
I need to join these two tables combining multiple rows for each month for each student from tbl_attendance into a single row to obtain a result like this:
id name month attended_days month attended_days
1 raj 1 6 2 16
7 raja 1 11 2 14
8 rajan 1 8 2 13
Thanks in advance for any help.
Instead of displaying month value in each of the records,
you can use them as column headers and attendance as their value.
Use pivot type solution to achieve required solution.
Example:
select s.id as student_id
, s.name as student_name
, max( case when a.month = 1 then a.attended_days else null end ) as month_1
, max( case when a.month = 2 then a.attended_days else null end ) as month_2
, max( case when a.month = 3 then a.attended_days else null end ) as month_3
-- ...
, max( case when a.month = 12 then a.attended_days else null end ) as month_12
from table_student s
left join table_attendance a on s.id = a.student_id
group by s.id, s.name
Your question is not very complete, but i think you want something like this:
select s.*,
coalesce(a1.month, a2.month, a3.month) as month,
coalesce(a1.attended_days , a2.attended_days , a3.attended_days ) as attended_days
from table_student s
left join table_attendance a1 on s.id = a1.student_id and a1.month = 1
left join table_attendance a2 on s.id = a2.student_id and a2.month = 2
left join table_attendance a3 on s.id = a3.student_id and a3.month = 3
The previous code is used if you want to show all months in one column. For multiple columns, you can use this example:
select s.*,
a1.month as month_1,
a2.month as month_2,
a3.month as month_3,
a1.attended_days as attended_days_1,
a2.attended_days as attended_days_2,
a3.attended_days as attended_days_3
from table_student s
left join table_attendance a1 on s.id = a1.student_id and a1.month = 1
left join table_attendance a2 on s.id = a2.student_id and a2.month = 2
left join table_attendance a3 on s.id = a3.student_id and a3.month = 3
Do this for all the 12 months. I used 3 as example.
I have a table like following columns:
id date price pid
---------------------------
1 2015-02-01 34 2
2 2015-02-02 34 2
3 2015-02-03 34 2
4 2015-02-04 78 2
5 2015-02-05 78 2
6 2015-02-06 78 2
7 2015-02-07 52 2
8 2015-02-08 52 2
9 2015-02-09 52 2
10 2015-02-10 34 2
11 2015-02-11 34 2
12 2015-02-12 34 2
Now I want following result:
date_from date_to price pid
-------------------------------------
2015-02-01 2015-02-03 34 2
2015-02-04 2015-02-06 78 2
2015-02-07 2015-02-09 52 2
2015-02-10 2015-02-12 34 2
IMPORTANT:
I don't want to group the price "34" in this case.
One solution i can think of using user defined variables also i assume the id part is set to auto_increment
select min(t1.date) date_from,
max(t1.date) date_to,
t1.price,
t1.pid
from (
select t.*,
#r:= case when #g = price then #r else #r + 1 end r,
#g:= price g
from test t
cross join (select #g:=null,#r:=0) t
order by id
) t1
group by t1.r
DEMO
Borrowing M Khalid Junaid's fiddle...
SELECT a.date date_from
, MIN(c.date) date_to
, price
, pid
FROM test a
LEFT
JOIN test b
ON b.pid = a.pid
AND b.price = a.price
AND b.id = a.id - 1
LEFT
JOIN test c
ON c.pid = a.pid
AND c.price = a.price
AND c.id >= a.id
LEFT
JOIN test d
ON d.pid = a.pid
AND d.price = a.price
AND d.id = c.id + 1
WHERE b.id IS NULL
AND c.id IS NOT NULL
AND d.id IS NULL
GROUP
BY a.id, a.price, a.pid;
http://sqlfiddle.com/#!2/478f9/6
Try below query, it will group by three records on basis of date and price
SELECT min(created) AS date_from, max(created) AS date_to, price, pid
FROM t1
GROUP BY price, floor(DATEDIFF("2015-02-01", created) / 3);
Try this in MySQL. Here app_date is Date in your question:
set #rownum=1;
select min(app_date),max(app_date),price,pid from
(select t.id1 id,t.app_date app_date,t.price1 price,t.pid pid,
case when (t.price1=t.price2 or t.price2 is null) then #rownum else #rownum:=#rownum+1 end temp_num from
(select a.id id1,b.id id2,a.app_date app_date,a.price price1,b.price price2,a.pid pid from
test a left outer join test b on a.id=b.id+1) t
order by id1) temp
group by price,pid,temp_num
order by min(app_date);
--work--
day hours
4 8
5 9
6 8
--crew--
employee day
2 4
3 4
6 4
2 5
3 5
4 5
--break--
hours day employee
1 4 2
.5 5 3
Here is the sql query i have:
SELECT w.hours, w.hours-b.hours, c.employee
FROM work w
LEFT JOIN crew c ON w.day = c.day
LEFT JOIN break b ON w.day = b.day
WHERE w.day = 4
this is subtracting 1 hour from every employee
i want the break.hours to be subtracted only if that employee has taken a break.
if he hasn't, i simply want work.hours.
COALESCE is all you need.
SELECT a.employee,
a.day,
b.hours - COALESCE(c.hours,0) totalHours
FROM crew a
INNER JOIN work b
ON a.day = b.day
LEFT JOIN `break` c
ON a.employee = c.employee AND
a.day = c.day
-- WHERE b.day = 4
SQLFiddle Demo Link
SQLFiddle Demo Link (with WHERE clause)