Adapting two queries - mysql

Hello again community.
I recently received help with the solution to a problem that consisted of List grouped data by month including months in which there are no results
Now I have the same scenario but with the difference that there is a condition to evaluate. I need to adapt this query
select
date_format(a.date_created, '%b') month,
month(a.date_created) pivot,
sum(case when a.state = 'created' then 1 else 0 end) created,
sum(case when a.state = 'notified' then 1 else 0 end) notified,
sum(case when a.state = 'confirmed' then 1 else 0 end) confirmed,
sum(case when a.state = 'approved' then 1 else 0 end) approved,
sum(case when a.state = 'authorized' then 1 else 0 end) authorized,
sum(case when a.state = 'attended' then 1 else 0 end) attended,
sum(case when a.state = 'canceled' then 1 else 0 end) canceled,
count(a.id) as total
from
activities a
inner join
employees e on a.employee_id = e.id
where
e.id = 8
group by 1, 2 order by pivot desc;
To this query
select
s.name month,
s.m pivot,
sum(case when a.state = 'created' then 1 else 0 end) created,
sum(case when a.state = 'notified' then 1 else 0 end) notified,
sum(case when a.state = 'confirmed' then 1 else 0 end) confirmed,
sum(case when a.state = 'approved' then 1 else 0 end) approved,
sum(case when a.state = 'authorized' then 1 else 0 end) authorized,
sum(case when a.state = 'attended' then 1 else 0 end) attended,
sum(case when a.state = 'canceled' then 1 else 0 end) canceled,
count(a.id) as total
from (
SELECT 1 m, 'Jan' AS name
UNION SELECT 2, 'Feb'
UNION SELECT 3, 'Mar'
UNION SELECT 4, 'Apr'
UNION SELECT 5, 'May'
UNION SELECT 6, 'Jun'
UNION SELECT 7, 'Jul'
UNION SELECT 8, 'Aug'
UNION SELECT 9, 'Sep'
UNION SELECT 10, 'Oct'
UNION SELECT 11, 'Nov'
UNION SELECT 12, 'Dec'
) s
LEFT JOIN activities a
ON s.m = month(date_created)
group by 1, 2 order by pivot desc;
I do not know how in the context of the solution that worked in the previous question add the condition
I have tested the following but the months in which there are no results are not listed
select
s.name month,
s.m pivot,
sum(case when a.state = 'created' then 1 else 0 end) created,
sum(case when a.state = 'notified' then 1 else 0 end) notified,
sum(case when a.state = 'confirmed' then 1 else 0 end) confirmed,
sum(case when a.state = 'approved' then 1 else 0 end) approved,
sum(case when a.state = 'authorized' then 1 else 0 end) authorized,
sum(case when a.state = 'attended' then 1 else 0 end) attended,
sum(case when a.state = 'canceled' then 1 else 0 end) canceled,
count(a.id) as total
from (
SELECT 1 m, 'Jan' AS name
UNION SELECT 2, 'Feb'
UNION SELECT 3, 'Mar'
UNION SELECT 4, 'Apr'
UNION SELECT 5, 'May'
UNION SELECT 6, 'Jun'
UNION SELECT 7, 'Jul'
UNION SELECT 8, 'Aug'
UNION SELECT 9, 'Sep'
UNION SELECT 10, 'Oct'
UNION SELECT 11, 'Nov'
UNION SELECT 12, 'Dec'
) s
LEFT JOIN activities a
ON s.m = month(date_created)
inner join employees e on a.employee_id = e.id
group by 1, 2 order by pivot desc;
I share the following sqlfiddle that includes the relationship between employee and activity
I am expecting for the user with id 2 the results should be as follows
Again, thank you very much.

You just had the joins muddled. For the join between activities and employees to be considered as INNER join it has to be enclosed in brackets. Let's know whether it works!
Here is the solution
select
s.name month,
s.m pivot,
sum(case when a.state = 'created' then 1 else 0 end) created,
sum(case when a.state = 'notified' then 1 else 0 end) notified,
sum(case when a.state = 'confirmed' then 1 else 0 end) confirmed,
sum(case when a.state = 'approved' then 1 else 0 end) approved,
sum(case when a.state = 'authorized' then 1 else 0 end) authorized,
sum(case when a.state = 'attended' then 1 else 0 end) attended,
sum(case when a.state = 'canceled' then 1 else 0 end) canceled,
count(a.id) as total
from (
SELECT 1 m, 'Jan' AS name
UNION SELECT 2, 'Feb'
UNION SELECT 3, 'Mar'
UNION SELECT 4, 'Apr'
UNION SELECT 5, 'May'
UNION SELECT 6, 'Jun'
UNION SELECT 7, 'Jul'
UNION SELECT 8, 'Aug'
UNION SELECT 9, 'Sep'
UNION SELECT 10, 'Oct'
UNION SELECT 11, 'Nov'
UNION SELECT 12, 'Dec'
) s
LEFT JOIN ( activities a INNER JOIN employees e
ON a.employee_id = e.id AND e.id = 2)
ON (s.m = month(a.date_created))
group by 1, 2 order by pivot desc;

I think this version does what you want:
select s.name as month, s.m as pivot,
sum(case when a.state = 'created' then 1 else 0 end) created,
sum(case when a.state = 'notified' then 1 else 0 end) notified,
sum(case when a.state = 'confirmed' then 1 else 0 end) confirmed,
sum(case when a.state = 'approved' then 1 else 0 end) approved,
sum(case when a.state = 'authorized' then 1 else 0 end) authorized,
sum(case when a.state = 'attended' then 1 else 0 end) attended,
sum(case when a.state = 'canceled' then 1 else 0 end) canceled,
count(a.id) as total
from (SELECT 1 m, 'Jan' AS name UNION ALL
SELECT 2, 'Feb' UNION ALL
SELECT 3, 'Mar' UNION ALL
SELECT 4, 'Apr' UNION ALL
SELECT 5, 'May' UNION ALL
SELECT 6, 'Jun' UNION ALL
SELECT 7, 'Jul' UNION ALL
SELECT 8, 'Aug' UNION ALL
SELECT 9, 'Sep' UNION ALL
SELECT 10, 'Oct' UNION ALL
SELECT 11, 'Nov' UNION ALL
SELECT 12, 'Dec'
) s LEFT JOIN activities a
ON s.m = month(date_created) left join
employees e
on a.employee_id = e.id AND e.id = 2
group by 1, 2
order by pivot desc;
Here is the SQL Fiddle.
The major change is moving the condition on e.id to the ON clause and changing the second join to a LEFT JOIN. In addition, I changed the UNION to UNION ALL, to eliminate the overhead of removing duplicates.

Related

sql How to hide row if entire row value is 0

My Query :
SELECT
(ref_intake.intakeno) AS intake,
SUM(CASE WHEN(student.intakeid AND student.status = '3' AND (student.courseid=3 OR student.courseid=17)) THEN 1 else 0 END) as active,
SUM(CASE WHEN(student.intakeid AND student.status = '5' AND (student.courseid=3 OR student.courseid=17)) THEN 1 else 0 END) as deffered,
SUM(CASE WHEN(student.intakeid AND student.status = '16' AND (student.courseid=3 OR student.courseid=17)) THEN 1 else 0 END) as inactive,
SUM(CASE WHEN(student.intakeid AND student.status = '9' AND (student.courseid=3 OR student.courseid=17)) THEN 1 else 0 END) as missing,
SUM(CASE WHEN(student.intakeid AND student.status = '6' AND (student.courseid=3 OR student.courseid=17)) THEN 1 else 0 END) as widthdrawn,
SUM(CASE WHEN(student.intakeid AND student.status IN (18,7,36) AND (student.courseid=3 OR student.courseid=17)) THEN 1 else 0 END) as dis_ter_dereg
FROM student
LEFT JOIN ref_intake ON student.intakeid = ref_intake.intakeid
GROUP BY student.intakeid ORDER BY ref_intake.intakeno ASC
The the image is my result:
I do suspect that your query can be simplified as follows:
SELECT
s.intakeid AS intake,
SUM(CASE WHEN s.status = 3 THEN 1 ELSE 0 END) as active,
SUM(CASE WHEN s.status = 5 THEN 1 ELSE 0 END) as deffered,
SUM(CASE WHEN s.status = 16 THEN 1 ELSE 0 END) as inactive,
SUM(CASE WHEN s.status = 9 THEN 1 ELSE 0 END) as missing,
SUM(CASE WHEN s.status = 6 THEN 1 ELSE 0 END) as widthdrawn,
SUM(CASE WHEN s.status IN (18, 7, 36) THEN 1 ELSE 0 END) as dis_ter_dereg
FROM student s
INNER JOIN ref_intake ri ON s.intakeid = ri.intakeid
WHERE
s.intakeid
AND s.status IN (3, 5, 16, 9, 6, 18, 7, 36)
AND s.courseid IN (3, 17)
GROUP BY s.intakeid
ORDER BY ri.intakeno ASC
That is:
most conditions can be moved to the WHERE clause - this should actually remove rows where no condition matches
it seems like you actually want an INNER JOIN rather than a LEFT JOIN - but you can revert that change if needed
table aliases make the query easier to read and write
ORed conditions can be simplified to use IN

aggregating values by month, Group by statement in mysql

I have a question on how to group the following statement into months.
Database is MYSQL
Desired result is:
DRNAME, Jan, Feb, Mar, April, May ,June
SISHEN, 0, 0, 100, 250, 450, 500, 0
Result I get is:
DRNAME, Jan, Feb, Mar, April, May ,June
SISHEN, 0, 0, 100, 0, 0, 0,0
SISHEN, 0, 0,0,250,0,0,0
SISHEN, 0, 0, 0,0 , 450, 0, 0
query is
select drname,
case when month(loaddate) = 1 then sum(drvalue) end as 'Jan',<br>
case when month(loaddate) = 2 then sum(drvalue) end as 'Feb',<br>
case when month(loaddate) = 3 then sum(drvalue) end as 'March',<br>
case when month(loaddate) = 4 then sum(drvalue) end as 'April',<br>
case when month(loaddate) = 5 then sum(drvalue) end as 'May',<br>
case when month(loaddate) = 6 then sum(drvalue) end as 'June'<br>
from tblloadschedule<br>
where cancelloadflag = 'N' and drname like 'sish%'<br>
group by drname,month(loaddate)
You need to remove month(loaddate) from the group by clause and then use conditional aggregation instead:
select drname,
sum(case when month(loaddate) = 1 then drvalue else 0 end) as 'Jan',
sum(case when month(loaddate) = 2 then drvalue else 0 end) as 'Feb',
sum(case when month(loaddate) = 3 then drvalue else 0 end) as 'March',
sum(case when month(loaddate) = 4 then drvalue else 0 end) as 'April',
sum(case when month(loaddate) = 5 then drvalue else 0 end) as 'May',
sum(case when month(loaddate) = 6 then drvalue else 0 end) as 'June'
from tblloadschedule
where cancelloadflag = 'N' and drname like 'sish%'
group by drname
Modify your query to take aggregates of the conditional CASE expressions:
SELECT
drname,
SUM(CASE WHEN MONTH(loaddate) = 1 THEN drvalue END) AS Jan,
SUM(CASE WHEN MONTH(loaddate) = 2 THEN drvalue END) AS Feb,
SUM(CASE WHEN MONTH(loaddate) = 3 THEN drvalue END) AS March,
SUM(CASE WHEN MONTH(loaddate) = 4 THEN drvalue END) AS April,
sum(CASE WHEN MONTH(loaddate) = 5 THEN drvalue END) AS May,
sum(CASE WHEN MONTH(loaddate) = 6 THEN drvalue END) AS June
FROM tblloadschedule
WHERE
cancelloadflag = 'N' AND drname LIKE 'sish%'
GROUP BY
drname;
You don't need case which is expensive:
select drname,
sum(if(month(loaddate) = 1 ,drvalue,0) as 'Jan',
sum(if(month(loaddate) = 2 , drvalue ,0) as 'Feb',
sum(if(month(loaddate) = 3 , drvalue ,0) as 'March',
sum(if(month(loaddate) = 4 , drvalue ,0) as 'April',
sum(if(month(loaddate) = 5 , drvalue ,0) as 'May',
sum(if(month(loaddate) = 6 , drvalue ,0) as 'June'
from tblloadschedule
where cancelloadflag = 'N' and drname like 'sish%'
group by drname
EDIT without use of IF:
select drname,
sum((month(loaddate) = 1 ) * drvalue) as 'Jan',
sum((month(loaddate) = 2 ) * drvalue) as 'Feb',
sum((month(loaddate) = 3 ) * drvalue) as 'March',
sum((month(loaddate) = 4 ) * drvalue) as 'April',
sum((month(loaddate) = 5 ) * drvalue) as 'May',
sum((month(loaddate) = 6 ) * drvalue) as 'June'
from tblloadschedule
where cancelloadflag = 'N' and drname like 'sish%'
group by drname
The method from A. Colonna with the IF definitely worked out more efficient on the server and returned the desired results.
Many thanks to all who commented.
The code I used is:
select drname,
sum((month(loaddate) = 1 ) * drvalue) as 'Jan',
sum((month(loaddate) = 2 ) * drvalue) as 'Feb',
sum((month(loaddate) = 3 ) * drvalue) as 'March',
sum((month(loaddate) = 4 ) * drvalue) as 'April',
sum((month(loaddate) = 5 ) * drvalue) as 'May',
sum((month(loaddate) = 6 ) * drvalue) as 'June'
from tblloadschedule
where cancelloadflag = 'N'
group by drname

SQL subquery in query

I have a table with payment:
worker_id, amount, payed, date
Table workers:
id, name, lname
I need to write SQL that will give me name, lname and sum for jun, july, august, september.
Name | Lname | Sum_JUN | Sum_JULY | Sum_AUG | Sum_SEP
I'm trying with subqueries but can't do it. Can you help me?
I created SQL (example). I will replace dates in PHP.
select w.name, w.lname,
sum(case when p.payed_date between '2014-06-01' and '2014-06-31' then p.amount else 0 end) `sum_june`,
sum(case when p.payed_date between '2014-07-01' and '2014-07-31' then p.amount else 0 end) `sum_july`,
sum(case when p.payed_date between '2014-08-01' and '2014-08-31' then p.amount else 0 end) `sum_august`,
sum(case when p.payed_date between '2014-09-01' and '2014-09-31' then p.amount else 0 end) `sum_september`,
sum(case when p.payed_date between '2014-10-01' and '2014-10-31' then p.amount else 0 end) `sum_november`
from worker w
left join worker_sum p on(w.id = p.worker_id)
group by w.id
You can use conditional aggregation for your desired sum,But this will give you the sum for months from all years exist in your table
select w.*,
sum(case when month(p.date) = 6 then p.amount else 0 end) `sum_june`,
sum(case when month(p.date) = 7 then p.amount else 0 end) `sum_july`,
sum(case when month(p.date) = 8 then p.amount else 0 end) `sum_august`,
sum(case when month(p.date) = 9 then p.amount else 0 end) `sum_september`
from workers w
left join payment p on(w.id = p.worker_id)
group by w.id

MySQL optimize multiple subquery into nested inner join?

I have 3 tables; contracts, dealers, and users.
users have many dealers and dealers have many contracts but the contracts are not directly associated with the users.
I am trying to build a report that gets me a monthly count of completed contracts grouped by user for the last 12 months.
So far I have built a multiple subquery, which is very slow: SQL Fiddle
SELECT *,
( SELECT count(*) FROM contracts
WHERE
dealer_id IN
( SELECT id FROM dealers WHERE user_id = User.id )
AND status = 'Paid'
AND completion_date BETWEEN
'2012-08-01 00:00:00' AND '2012-08-31 23:59:59'
) AS Aug_2012,
( SELECT count(*) FROM contracts
WHERE
dealer_id IN
( SELECT id FROM dealers WHERE user_id = User.id )
AND status = 'Paid'
AND completion_date BETWEEN
'2012-09-01 00:00:00' AND '2012-09-30 23:59:59'
) AS Sep_2012
FROM users AS User
WHERE
id IN( SELECT user_id FROM dealers WHERE active = 1 AND user_id IS NOT NULL GROUP BY user_id )
AND id != 1
ORDER BY User.name ASC
Instead of the subquery which selects each month I'd like to use something like this:
COUNT(*) as last_12_months,
SUM(case when MONTH(completion_date) = 8 then 1 else 0 end) as Aug_2012,
SUM(case when MONTH(completion_date) = 9 then 1 else 0 end) as Sep_2012,
etc.
Since I'd be returning multiple columns I would have to restructure it, but I'm not sure how. If I use an INNER JOIN what clause do I join on?
Here's the final query based on Mikhail's answer below:
SELECT
User.*,
SUM(case when MONTH(completion_date) = 8 then 1 else 0 end) AS Aug_2012,
SUM(case when MONTH(completion_date) = 9 then 1 else 0 end) AS Sep_2012,
SUM(case when MONTH(completion_date) = 10 then 1 else 0 end) AS Oct_2012,
SUM(case when MONTH(completion_date) = 11 then 1 else 0 end) AS Nov_2012,
SUM(case when MONTH(completion_date) = 12 then 1 else 0 end) AS Dec_2012,
SUM(case when MONTH(completion_date) = 1 then 1 else 0 end) AS Jan_2013,
SUM(case when MONTH(completion_date) = 2 then 1 else 0 end) AS Feb_2013,
SUM(case when MONTH(completion_date) = 3 then 1 else 0 end) AS Mar_2013,
SUM(case when MONTH(completion_date) = 4 then 1 else 0 end) AS Apr_2013,
SUM(case when MONTH(completion_date) = 5 then 1 else 0 end) AS May_2013,
SUM(case when MONTH(completion_date) = 6 then 1 else 0 end) AS Jun_2013,
SUM(case when MONTH(completion_date) = 7 then 1 else 0 end) AS Jul_2013,
SUM(case when completion_date BETWEEN '2012-08-01 00:00:00' AND '2013-07-31 23:59:59' then 1 else 0 end) as last_12_months
FROM users AS User
LEFT OUTER JOIN
(
SELECT id, user_id FROM dealers
WHERE active = 1 AND user_id IS NOT NULL
) AS Dealer ON User.id = Dealer.user_id
LEFT OUTER JOIN
(
SELECT completion_date, status, dealer_id FROM contracts
WHERE completion_date BETWEEN '2012-08-01 00:00:00' AND '2013-07-31 23:59:59' AND status = 'Paid' AND cancelled = 0
) AS Contract on Dealer.id = Contract.dealer_id
WHERE
User.id IN
(
SELECT user_id FROM dealers
WHERE active = 1 AND user_id IS NOT NULL
GROUP BY user_id
)
GROUP BY
User.id order by User.name asc
This is about 4 times faster.
Try this:
select
User.id, User.name,
sum(case when MONTH(completion_date) = 8 and Year(completion_date)=2012 then 1 else 0 end) as Aug_2012,
sum(case when MONTH(completion_date) = 9 and Year(completion_date)=2012 then 1 else 0 end) as Sep_2012,
sum(case when MONTH(completion_date) = 10 and Year(completion_date)=2012 then 1 else 0 end) as Oct_2012,
sum(case when MONTH(completion_date) = 11 and Year(completion_date)=2012 then 1 else 0 end) as Nov_2012,
sum(case when MONTH(completion_date) = 12 and Year(completion_date)=2012 then 1 else 0 end) as Dec_2012,
sum(case when MONTH(completion_date) = 1 and Year(completion_date)=2013 then 1 else 0 end) as Jan_2012,
sum(case when MONTH(completion_date) = 2 and Year(completion_date)=2013 then 1 else 0 end) as Feb_2012,
sum(case when MONTH(completion_date) = 3 and Year(completion_date)=2013 then 1 else 0 end) as Mar_2012,
sum(case when MONTH(completion_date) = 4 and Year(completion_date)=2013 then 1 else 0 end) as Apr_2012,
sum(case when MONTH(completion_date) = 5 and Year(completion_date)=2013 then 1 else 0 end) as May_2012,
sum(case when MONTH(completion_date) = 6 and Year(completion_date)=2013 then 1 else 0 end) as Jun_2012,
sum(case when MONTH(completion_date) = 7 and Year(completion_date)=2013 then 1 else 0 end) as Jul_2012
from users AS User
left outer join dealers on
User.id=dealers.user_id
left outer join contracts on
dealers.id=contracts.dealer_id
group by
User.id,
contracts.status
having
contracts.status='Paid'
order by
User.name asc;

Counting number of Sales from two tables

I am using SUM ( CASE WHEN ) to count number of Yes and No, it work fine.
I am havin problem counting number of matching mobile number from two tables. It dont seem to be counting correctly.
There is a MobileNO field in dairy table and mobile field in the sales table
SELECT
D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'No' THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other,
(SELECT SUM(CASE WHEN D.MobileNo = S.mobile THEN 1 ELSE 0 END) from sales as S) as Sales,
COUNT(*) as TOTAL FROM dairy as D
WHERE source = 'Company' AND UNIX_TIMESTAMP(CheckDate) >= 1293840000 AND UNIX_TIMESTAMP(CheckDate) <= 1322697600
group by D.Username order by TOTAL DESC
SELECT
D.Username,
SUM(CASE WHEN D.type = 'Yes' THEN 1 ELSE 0 END) as Yes,
SUM(CASE WHEN D.type = 'No' THEN 1 ELSE 0 END) as No,
SUM(CASE WHEN D.type = '' THEN 1 ELSE 0 END) as Other,
SUM(CASE WHEN S.mobile IS NULL THEN 0 ELSE 1 END) as Sales,
COUNT(*) as TOTAL
FROM dairy as D
LEFT JOIN (SELECT DISTINCT mobile FROM sales) as S ON D.MobileNo = S.mobile
WHERE source = 'Company' AND UNIX_TIMESTAMP(CheckDate) >= 1293840000 AND UNIX_TIMESTAMP(CheckDate) <= 1322697600
group by D.Username order by TOTAL DESC