MySql union Sum(Case()) - mysql

How can I do a "union" in order to get only distinct values for "ACTIVITY" field, but with the variables started by "OUTPUT_" from "OUTPUT_TAB" appearing in the header. (Like "INPUT_" variables from "INPUT_TAB") .
It's possible ? I can not do it.
Check my Query PIC_QUERY:
SELECT
ACTIVIDADE,
SUM(CASE WHEN DATA_VAL_REGISTO = '2017-06-25' THEN 1 ELSE 0 END) INPUT_ACT_DATE_VOL,
SUM(CASE WHEN DATA_VAL_REGISTO = '2017-06-24' THEN 1 ELSE 0 END) PREV_DATE_VOL,
SUM(CASE WHEN WEEK(DATA_VAL_REGISTO,7) = '25' AND YEAR(DATA_VAL_REGISTO) = '2017' THEN 1 ELSE 0 END) INPUT_ACT_WEEK_VOL,
SUM(CASE WHEN WEEK(DATA_VAL_REGISTO,7) = '24' AND YEAR(DATA_VAL_REGISTO) = '2017' THEN 1 ELSE 0 END) INPUT_PREV_WEEK_VOL,
SUM(CASE WHEN MONTH(DATA_VAL_REGISTO) = '6' AND YEAR(DATA_VAL_REGISTO) = '2017' THEN 1 ELSE 0 END) INPUT_ACT_MONTH_VOL,
SUM(CASE WHEN MONTH(DATA_VAL_REGISTO) = '5' AND YEAR(DATA_VAL_REGISTO) = '2017' THEN 1 ELSE 0 END) INPUT_PREV_MONTH_VOL
FROM INPUT_TAB
GROUP BY ACTIVIDADE
UNION
SELECT
ACTIVIDADE,
SUM(CASE WHEN DATA_FIM_REP = '2017-06-25' THEN 1 ELSE 0 END) OUTPUT_ACT_DATE_VOL,
SUM(CASE WHEN DATA_FIM_REP = '2017-06-24' THEN 1 ELSE 0 END) OUTPUT_PREV_DATE_VOL,
SUM(CASE WHEN WEEK(DATA_FIM_REP,7) = '25' AND YEAR(DATA_FIM_REP) = '2017' THEN 1 ELSE 0 END) OUTPUT_ACT_WEEK_VOL,
SUM(CASE WHEN WEEK(DATA_FIM_REP,7) = '24' AND YEAR(DATA_FIM_REP) = '2017' THEN 1 ELSE 0 END) OUTPUT_PREV_WEEK_VOL,
SUM(CASE WHEN MONTH(DATA_FIM_REP) = '6' AND YEAR(DATA_FIM_REP) = '2017' THEN 1 ELSE 0 END) OUTPUT_ACT_MONTH_VOL,
SUM(CASE WHEN MONTH(DATA_FIM_REP) = '5' AND YEAR(DATA_FIM_REP) = '2017' THEN 1 ELSE 0 END) OUTPUT_PREV_MONTH_VOL
FROM OUTPUT_TAB
GROUP BY ACTIVIDADE
RESULT: PIC_RESULT

You don't want a union of two query results, but a join:
SELECT
actividade,
i.input_act_date_vol, i.input_prev_date_vol, i.input_act_week_vol,
i.input_prev_week_vol, i.input_act_month_vol, i.input_prev_month_vol,
o.output_act_date_vol, o.output_prev_date_vol, o.output_act_week_vol,
o.output_prev_week_vol, o.output_act_month_vol, o.output_prev_month_vol
FROM
(
SELECT
actividade,
SUM(data_val_registo = '2017-06-25') AS input_act_date_vol,
SUM(data_val_registo = '2017-06-24') AS input_prev_date_vol,
SUM(WEEK(data_val_registo, 7) = 25 AND YEAR(data_val_registo) = 2017) AS input_act_week_vol,
SUM(WEEK(data_val_registo, 7) = 24 AND YEAR(data_val_registo) = 2017) AS input_prev_week_vol,
SUM(MONTH(data_val_registo) = 6 AND YEAR(data_val_registo) = 2017) AS input_act_month_vol,
SUM(MONTH(data_val_registo) = 5 AND YEAR(data_val_registo) = 2017) AS input_prev_month_vol
FROM input_tab
GROUP BY actividade
) i
JOIN
(
SELECT
actividade,
SUM(data_fim_rep = '2017-06-25') AS output_act_date_vol,
SUM(data_fim_rep = '2017-06-24') AS output_prev_date_vol,
SUM(WEEK(data_fim_rep, 7) = 25 AND YEAR(data_fim_rep) = 2017) AS output_act_week_vol,
SUM(WEEK(data_fim_rep, 7) = 24 AND YEAR(data_fim_rep) = 2017) AS output_prev_week_vol,
SUM(MONTH(data_fim_rep) = 6 AND YEAR(data_fim_rep) = 2017) AS output_act_month_vol,
SUM(MONTH(data_fim_rep) = 5 AND YEAR(data_fim_rep) = 2017) ASoutput_prev_month_vol
FROM OUTPUT_TAB
GROUP BY actividade
) o USING (actividade);
In case there can be input without output or vice versa, you'll have to use an outer join instead. In case there can even be both, input without output and output without input, you'll have to look up how to emulate a full outer join in MySQL.
I've removed CASE WHEN .. THEN 1 ELSE 0 END, because a boolean expression results in true or false which equal to 1 and 0 in MySQL.
BTW: As this is all about 2017, you can get your queries simpler and faster by using WHERE YEAR(data_val_registo) and WHERE YEAR(data_fim_rep) = 2017 and removing the condition from the sum expressions.

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

Slow MySQL queries using SUM()

I have to run two queries in my code to get my tenants balance. However, these queries are too slow.
First query, I get all the tenants and it's unit name:
SELECT t.TenantID
FROM Tenants t
JOIN Units u
ON t.UnitID = u.UnitID
Where t.Prospect = 2
AND t.PropertyID = 8
ORDER
BY CONCAT(Left(Replace(UnitName,'-',''),2),
REPEAT('0', (10-CHAR_LENGTH(UnitName))),
Right(Replace(UnitName,'-',''),
CHAR_LENGTH(Replace(UnitName,'-',''))-2
) )
It returns 500 rows
Then I get the balances in 4 conditions. This query will be inside of first query loop:
Select
SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) AS TotalDebit,
SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) AS HousingDebit,
SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) AS TotalCredit,
SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) AS HousingCredit
From TenantTransactions
Where TenantID= FirstQuery.TenantID
Am I doing the queries wrong? It's taking like 1 minute to run.
Do this in a single query with GROUP BY.
Try something like this:
SELECT t.TenantID, TotalDebit, HousingDebit, TotalCredit, HousingCredit
FROM Tenants t
JOIN Units u ON t.UnitID = u.UnitID
LEFT JOIN (
Select
TenantID,
SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) AS TotalDebit,
SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) AS HousingDebit,
SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) AS TotalCredit,
SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) AS HousingCredit
From TenantTransactions
Group By TenantID
) sums ON sums.TenantID = t.TenantID
Where t.Prospect = 2
AND t.PropertyID = 8
ORDER
BY CONCAT(Left(Replace(UnitName,'-',''),2),REPEAT('0', (10-CHAR_LENGTH(UnitName))),Right(Replace(UnitName,'-',''),CHAR_LENGTH(Replace(UnitName,'-',''))-2))
The inner query may still run for a while but it will only run once.
Try a compound covering index on TenantTransactions containing these columns: (TenantID, TransactionTypeID, ChargeTypeID, TransactionAmount) to optimize the query with the SUMs in it.
Try a compound index on Tenants with the columns (PropertyID, Prospect) in it.
Here's another way to do it with a subquery. You know, the performance problem might not be database performance, but the back and forth between your database and application server. So that is where a single query will help.
SELECT t.TenantID,
(SELECT SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) From TenantTransactions TT WHERE TT.TenantID=t.TenantID) AS TotalDebit,
(SELECT SUM(CASE WHEN TransactionTypeID = 1 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) From TenantTransactions TT WHERE TT.TenantID=t.TenantID) AS HousingDebit,
(SELECT SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID != 6 THEN TransactionAmount ELSE 0 END) From TenantTransactions TT WHERE TT.TenantID=t.TenantID) AS TotalCredit,
(SELECT SUM(CASE WHEN TransactionTypeID = 2 AND ChargeTypeID = 6 THEN TransactionAmount ELSE 0 END) From TenantTransactions TT WHERE TT.TenantID=t.TenantID) AS HousingCredit
FROM Tenants t
JOIN Units u
ON t.UnitID = u.UnitID
Where t.Prospect = 2
AND t.PropertyID = 8
ORDER
BY CONCAT(Left(Replace(UnitName,'-',''),2),REPEAT('0', (10-CHAR_LENGTH(UnitName))),Right(Replace(UnitName,'-',''),CHAR_LENGTH(Replace(UnitName,'-',''))-2))

Need help in Merging Two query for HP ALM

I have two different query in HP ALM, but i want to merge and get it into one. I am not that good in SQL query so I am facing hard time in merging the query.
Query 1: Getting the execution Count for the tester
Select
TESTCYCL.TC_ACTUAL_TESTER as 'Tester',
sum(case when TC_Status In('Blocked','Passed','Failed','Not Completed') then 1 else 0 end) as 'Total',
sum(case when TC_Status = 'Passed' then 1 else 0 end) as 'Pass',
sum(case when TC_Status = 'Failed' then 1 else 0 end) as 'Fail',
sum(case when TC_Status = 'Blocked' then 1 else 0 end) as 'Blocked',
sum(case when TC_Status In('Not Completed','Defferred','N/A') then 1 else 0 end) as 'Others'
From TESTCYCL
Where
TESTCYCL.TC_EXEC_DATE = CAST(CURRENT_TIMESTAMP AS DATE)
And
TESTCYCL.TC_ACTUAL_TESTER in ('Username1')
Group by TC_ACTUAL_TESTER
Query 2: Getting the Defect raise by the tester
SELECT
BG_DETECTED_BY,
Sum(case when BG_Status Not in ('Closed','Defect Resolved','Rejected')then 1 else 0 end) as 'Defect Raised'
FROM BUG
Where BUG.BG_DETECTED_BY in ('username1')
AND BUG.BG_DETECTION_DATE = CAST(CURRENT_TIMESTAMP AS DATE)
Group by BG_DETECTED_BY
I have tried inner join/ Left Join but the count of the defect raised by the user is not matching
Query3 : That i have tried:
Select
TESTCYCL.TC_ACTUAL_TESTER as 'Tester',
sum(case when TC_Status In('Blocked','Passed','Failed','Not Completed') then 1 else 0 end) as 'Total',
sum(case when TC_Status = 'Passed' then 1 else 0 end) as 'Pass',
sum(case when TC_Status = 'Failed' then 1 else 0 end) as 'Fail',
sum(case when TC_Status = 'Blocked' then 1 else 0 end) as 'Blocked',
sum(case when TC_Status In('Not Completed','Defferred','N/A') then 1 else 0 end) as 'Others',
Sum(case when BG_Status Not in ('Closed','Defect Resolved','Rejected')then 1 else 0 end) as 'Defect Raised'
From TESTCYCL
Left Join BUG
on TESTCYCL.TC_ACTUAL_TESTER = BUG.BG_DETECTED_BY
AND BUG.BG_DETECTION_DATE = CAST(CURRENT_TIMESTAMP AS DATE)
Where TESTCYCL.TC_EXEC_DATE = CAST(CURRENT_TIMESTAMP AS DATE)
And TESTCYCL.TC_ACTUAL_TESTER in ('username1')
Group by TC_ACTUAL_TESTER
Out is mentioned as below:
Expected Output:
Tester Total Execution Passed Failed ... Defect Raised
A 5 3 2 10
Actual Output:
Tester Total Execution Passed Failed ... Defect Raised
A 56 3 2 45
The problem is you are doing the COUNT() over the product cartesian. instead of concatenating the result.
Right now you are doing
COUNT(A*B) instead of COUNT(A) || COUNT(B)
general example, if you have two queries
SELECT * FROM A (ex: 10 rows)
SELECT * FROM B (ex: 10 rows)
What you need is:
SELECT temp1.*, temp2.*
FROM (SELECT * FROM A) as temp1
JOIN (SELECT * FROM B) as temp2
ON temp1.ID = temp2.ID

Calculate date difference in sql for specific year

I have two dates, say start_date is 20141215 and end_date = 20150115. I would like to use SQL DATEDIFF to only count the dates within the year 2015 which I will specify in the query. Here is the current SQL I have written:
SELECT COUNT(leave_id),
sum(case when leave_status = 1 then 1 else 0 end) pending,
sum(case when leave_status = 2 then 1 else 0 end) declined,
sum(case when leave_status = 3 then 1 else 0 end) approved,
sum(case when leave_status = 4 then 1 else 0 end) rostered,
SUM(DATEDIFF(end_date, start_date)+1) as datetotals
FROM employee_leave WHERE
((YEAR(start_date) = :year) OR (YEAR(end_date) = :year))
AND employee_id = :emp_id
Thanks
You need to fix datediff() to only consider dates during the year. I think this does what you want:
SELECT COUNT(leave_id),
sum(case when leave_status = 1 then 1 else 0 end) pending,
sum(case when leave_status = 2 then 1 else 0 end) declined,
sum(case when leave_status = 3 then 1 else 0 end) approved,
sum(case when leave_status = 4 then 1 else 0 end) rostered,
SUM(DATEDIFF(least(end_date, date(concat_ws('-', :year, 12, 31))),
greatest(start_date, date(concat_ws('-', :year, 1, 1)))
) + 1) as datetotals
FROM employee_leave
WHERE ((YEAR(start_date) = :year) OR (YEAR(end_date) = :year)) AND
employee_id = :emp_id
Make it a AND condition rather like
WHERE
((YEAR(start_date) = :year) AND (YEAR(end_date) = :year))

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;