Update certain columns based on date. Best way to approach this issue - mysql

I have a table I need to update. In this table there is a column for each month. If someone's start date is 2/13/2015 I need to update the February column with some data from a calculation.
So if Start_Date is 2/17/2015 then the value for the February column called FTEFeb needs to be populated. If the Start_Date is 3/09/2015 then the FTEMar column needs to be updated etc...
I was thinking a CASE statment would work but the SET column would be different based on the value in Start Date.
I've looked online for anything similar and nothing came up.
Thanks in advance!

You can use a case statement.
For your query, instead of preforming a CASE on the month, just write a case to update each column, and if the column is not the month you want you can set it to 0, or whatever your business rule is. Try this:
UPDATE myTable
SET
FTEJan = (CASE WHEN MONTH(startDate) = 1 THEN 1 ELSE 0 END),
FTEFeb = (CASE WHEN MONTH(startDate) = 2 THEN 1 ELSE 0 END),
FTEMar = (CASE WHEN MONTH(startDate) = 3 THEN 1 ELSE 0 END),
FTEApr = (CASE WHEN MONTH(startDate) = 4 THEN 1 ELSE 0 END),
FTEMay = (CASE WHEN MONTH(startDate) = 5 THEN 1 ELSE 0 END),
FTEJun = (CASE WHEN MONTH(startDate) = 6 THEN 1 ELSE 0 END),
FTEJul = (CASE WHEN MONTH(startDate) = 7 THEN 1 ELSE 0 END),
FTEAug = (CASE WHEN MONTH(startDate) = 8 THEN 1 ELSE 0 END),
FTESep = (CASE WHEN MONTH(startDate) = 9 THEN 1 ELSE 0 END),
FTEOct = (CASE WHEN MONTH(startDate) = 10 THEN 1 ELSE 0 END),
FTENov = (CASE WHEN MONTH(startDate) = 11 THEN 1 ELSE 0 END),
FTEDec = (CASE WHEN MONTH(startDate) = 12 THEN 1 ELSE 0 END);
Here is an SQL Fiddle example.

Related

SSRS calculate % of desired colums in column group in matrix

SELECT PatchPhase.PhaseName, MAX(PatchPhase.Sequence) AS seq,
PatchState.Application, PatchState.Objectname, PatchState.Jobname,
PatchState.Streamname, COUNT(*) AS Total,
PatchState.Jobdescription,
PatchState.Status, PatchState.Timestamp
FROM PatchState
INNER JOIN PatchPhase ON PatchState.Phase = PatchPhase.PhaseTech
WHERE (PatchPhase.PhaseName IN (#Phase))
AND (PatchState.Application IN (#Application)) AND (PatchState.Timestamp >= #StartDate)
AND (PatchState.Timestamp <= #EndDate)
GROUP BY PatchPhase.PhaseName, PatchState.Application, PatchState.Objectname,
PatchState.Jobname, PatchState.Streamname, PatchState.Jobdescription, PatchState.Status,
PatchState.Timestamp
ORDER BY PatchState.Application
I am working on matrix and I have column group of status which contains 3 columns (planned, running,completed). I want to sum planned+completed and divide by total column.
Total column is outside the column group.
I do find some answers but I did not get how should I use in my code
can someone please help?
Try using this to aggregate, and then use a table instead of a matrix. You will not need a column group now. As I cannot run the sql I can't promise it is perfect but perhaps you could have a little play with it if not. It should offer a guide if nothing else.
SELECT
PatchPhase.PhaseName,
MAX(PatchPhase.Sequence) AS seq,
PatchState.Application,
PatchState.Objectname,
PatchState.Jobname,
PatchState.Streamname,
PatchState.Jobdescription,
SUM(case when PatchState.Status = 'Planned' then 1 else 0 end) as 'Planned',
SUM(case when PatchState.Status = 'Running' then 1 else 0 end) as 'Running',
SUM(case when PatchState.Status = 'Completed' then 1 else 0 end) as 'Completed',
(SUM(case when PatchState.Status = 'Planned' then 1 else 0 end) + SUM(case when PatchState.Status = 'Running' then 1 else 0 end)) /
(SUM(case when PatchState.Status = 'Planned' then 1 else 0 end) + SUM(case when PatchState.Status = 'Running' then 1 else 0 end) + SUM(case when PatchState.Status = 'Completed' then 1 else 0 end)) as totalPercentage
PatchState.Timestamp
FROM
PatchState
INNER JOIN PatchPhase
ON PatchState.Phase = PatchPhase.PhaseTech
WHERE
(PatchPhase.PhaseName IN (#Phase))
AND (PatchState.Application IN (#Application))
AND (PatchState.Timestamp >= #StartDate)
AND (PatchState.Timestamp <= #EndDate)
GROUP BY
PatchPhase.PhaseName,
PatchState.Application,
PatchState.Objectname,
PatchState.Jobname,
PatchState.Streamname,
PatchState.Jobdescription,
PatchState.Status,
PatchState.Timestamp
ORDER BY
PatchState.Application
You can get a value displayed in a text box from expression =Reportitems!Textbox133.Value
instead of textbox133 in the above expression you have to give the textbox name of the textbox from which you want the value. In your case the total column

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))

UPDATE a field with sum of other fields with conditions MYSQL

My Table structure is as follows
counter1|counter1_status|counter2|counter2_status|counter3|counter3_status|valid_counter
-----------------------------------------------------------------------------------------
5 0 6 1 3 1 XXXX
I want a single update query to update valid_counter as 6 + 3 = 9
As counter1_status = 0, counter1 should not be added
Tried following query, but it gives error.
UPDATE counter_table
SET valid_contact =
SUM((CASE WHEN counter1_status=1 THEN counter1 ELSE 0 END)
+ (CASE WHEN counter2_status=1 THEN counter2 ELSE 0 END)
+ (CASE WHEN counter3_status=1 THEN counter3 ELSE 0 END))
I can get the sum by using SELECT query without any error, but Update query failed.
if you want to conditionally store sum of (counter1,counter2,counter3) in the valid_contace field, you may use:
UPDATE counter_table
SET valid_contact =
(CASE WHEN counter1_status=1 THEN counter1 ELSE 0 END)
+ (CASE WHEN counter2_status=1 THEN counter2 ELSE 0 END)
+ (CASE WHEN counter3_status=1 THEN counter3 ELSE 0 END)
where id=5
UPDATE counter_table AS c1 JOIN
(SELECT id, SUM((CASE WHEN counter1_status=1 THEN counter1 ELSE 0 END)
+ (CASE WHEN counter2_status=1 THEN counter2 ELSE 0 END)
+ (CASE WHEN counter3_status=1 THEN counter3 ELSE 0 END)) AS m
FROM counter_table) AS c2
USING (id)
SET c1.`valid_counter` = c2.m;
Sample fiddle
Finally got the solution for error :
SUM should be removed from the query.
It should be SUM(a,b,c)
or a+b+c.
UPDATE counter_table
SET valid_contact =
((CASE WHEN counter1_status=1 THEN counter1 ELSE 0 END)
+ (CASE WHEN counter2_status=1 THEN counter2 ELSE 0 END)
+ (CASE WHEN counter3_status=1 THEN counter3 ELSE 0 END))

COUNT data using case when with mysql

SELECT NAMA_DUN,
COUNT(case when ((RIGHT(noMyKid, 1))% 2) = 0 then 1 else 0 end) AS FEMALE,
COUNT(case when ((RIGHT(noMyKid, 1))% 2) = 1 then 1 else 0 end) AS MALE,
COUNT(DISTINCT(noMyKid)) as jumlah
FROM mohon
LEFT JOIN dun ON dun.KOD_DUN=mohon.dun_nama
WHERE status_proses = 'diproses'
AND concat('20', substr(noMyKid, 1, 2)) = '2008'
AND status_mohon = 'Layak'
AND status_semak = '1'
AND (
status_bayar = ''
OR status_bayar = 'Belum'
OR status_bayar = 'Sudah')
AND (
status_terima = ''
OR status_terima = 'Terima'
) GROUP BY dun_nama
ORDER BY NAMA_DUN
This is my mysql code. why is my 'COUNT CASE WHEN' give the same output for female and male column.
Usually COUNT() is used to count rows and is therefore very often used in the form COUNT(*). When you use a field (or anything else) as a parameter into the COUNT() it counts 1 for every no NULL value.
In your case all your values are not NULL (they are either 1 or 0) and consequently you end up with the same results.
So Abhik Chakraborty is right, use SUM() and everything should be fine.
The COUNT() function does only recognize non-null values, so when using case expressions such as you have here, you can explicitly return NULL where needed
SELECT NAMA_DUN,
COUNT(case when ((RIGHT(noMyKid, 1))% 2) = 0 then 1 else NULL end) AS FEMALE,
COUNT(case when ((RIGHT(noMyKid, 1))% 2) = 1 then 1 else NULL end) AS MALE,
COUNT(DISTINCT(noMyKid)) as jumlah
OR, implicitly return NULL by ignoring the else condition
SELECT NAMA_DUN,
COUNT(case when ((RIGHT(noMyKid, 1))% 2) = 0 then 1 end) AS FEMALE,
COUNT(case when ((RIGHT(noMyKid, 1))% 2) = 1 then 1 end) AS MALE,
COUNT(DISTINCT(noMyKid)) as jumlah
OR, mimic the effect of count by using SUM() provided you do use 1 and 0 (or 1 and NULL)
SELECT NAMA_DUN,
SUM(case when ((RIGHT(noMyKid, 1))% 2) = 0 then 1 else 0 end) AS FEMALE,
SUM(case when ((RIGHT(noMyKid, 1))% 2) = 1 then 1 else 0 end) AS MALE,
COUNT(DISTINCT(noMyKid)) as jumlah

Is it possible to use SUM - CASE - IF together?

When I try to use this:
SUM(CASE WHEN IF(SUM(CASE WHEN "b.count_students_status" = 1 THEN 1 ELSE 0 END) >= 1, 1, 0) = 1 THEN 1 ELSE 0 END)
It says Query Error: Improper usage of Group Function
Below is the complete code:
SELECT
"a.batch" AS Batch,
SUM(
CASE
WHEN IF(
SUM(
CASE
WHEN "b.count_students_status" = 1
THEN 1
ELSE 0
END
) >= 1,
1,
0
) = 1
THEN 1
ELSE 0
END
) AS Payments_Not_Received
FROM
"DBU - Complete"
WHERE "a.suspended" = 'no'
GROUP BY "a.batch"
I wanted to convert the status to 1, if there are multiple occurrences and then sum the total occurrences.
Any help please - I am using ZOHO to build the query?
You're trying to reference the result of the GROUP BY before is has run. Try something like this:
select t.BATCH,
sum(case when t.pnr >=1 then 1 else 0 end) as Payments_Not_Received
from
(
select
a.batch as BATCH,
SUM(CASE WHEN "b.count_students_status" = 1 THEN 1
ELSE 0
END) as pnr
from yourTable a
WHERE a.suspended = 'no'
group by a.batch
) t
group by t.BATCH;