How do I select alias as a 'date1' column value?I want to select the alias that will show month & year format like 'Jan-18'. MySQL ver 5.0.
I tried this but didnt work:
SUM(CASE WHEN MONTH(date1) = 1 THEN quantity/1000 ELSE NULL END) AS date1
My query :
SELECT
SUM(CASE WHEN MONTH(date1) = 1 THEN quantity/1000 ELSE NULL END) AS jan
,SUM(CASE WHEN MONTH(date1) = 2 THEN quantity/1000 ELSE NULL END) AS feb
,SUM(CASE WHEN MONTH(date1) = 3 THEN quantity/1000 ELSE NULL END) AS mar
,SUM(CASE WHEN MONTH(date1) = 4 THEN quantity/1000 ELSE NULL END) AS apr
,SUM(CASE WHEN MONTH(date1) = 5 THEN quantity/1000 ELSE NULL END) AS may
,SUM(CASE WHEN MONTH(date1) = 6 THEN quantity/1000 ELSE NULL END) AS jun
,SUM(CASE WHEN MONTH(date1) = 7 THEN quantity/1000 ELSE NULL END) AS jul
,SUM(CASE WHEN MONTH(date1) = 8 THEN quantity/1000 ELSE NULL END) AS aug
,SUM(CASE WHEN MONTH(date1) = 9 THEN quantity/1000 ELSE NULL END) AS sep
,SUM(CASE WHEN MONTH(date1) = 10 THEN quantity/1000 ELSE NULL END) AS octo
,SUM(CASE WHEN MONTH(date1) = 11 THEN quantity/1000 ELSE NULL END) AS nov
,SUM(CASE WHEN MONTH(date1) = 12 THEN quantity/1000 ELSE NULL END) AS dece
FROM
data2
WHERE
date1 >= DATE_SUB('2018-09-11', INTERVAL 11 MONTH) AND date1 <= DATE_FORMAT('2018-09-11', '%Y-%m-31')
GROUP BY unit
It works as follows
sum(case when expression = true then 100 else 0 end) -- this will work fine
sum(case when expression = true then 100 else null end) -- this will also work -
but gives you nulls even
if there is one row
where the expression is
false
Related
I'm struggling - I have a table payments
Date
Ref
1.1.22
1
1.2.22
2
1.3.22
1
1.4.22
3
1.3.22
2
1.2.22
3
and a table of ref
Name
Ref
jo
1
Steve
2
Chris
3
I'm trying to get my sql to state
Name
Jan
Feb
Mar
Apr
Jo
Paid
Not
Paid
Not
Chris
Not
Paid
Not
Paid
Etc, so far I can but each name has one line for each month, when I want them in one row
`SELECT m.memID,m.Pay_Ref, p.ref,
(case when MONTHNAME(p.DATE) = 'January' then 'Paid' else 'Not' end) as January,
(case when MONTHNAME(p.DATE) = 'February' then 'Paid' else 'Not' end) as February,
(case when MONTHNAME(p.DATE) = 'March' then 'Paid' else 'Not' end) as March,
(case when MONTHNAME(p.DATE) = 'April' then 'Paid' else 'Not' end) as April,
(case when MONTHNAME(p.DATE) = 'May' then 'Paid' else 'Not' end) as May,
(case when MONTHNAME(p.DATE) = 'June' then 'Paid' else 'Not' end) as June,
(case when MONTHNAME(p.DATE) = 'July' then 'Paid' else 'Not' end) as July,
(case when MONTHNAME(p.DATE) = 'August' then 'Paid' else 'Not' end) as August,
(case when MONTHNAME(p.DATE) = 'September' then 'Paid' else 'Not' end) as September,
(case when MONTHNAME(p.DATE) = 'October' then 'Paid' else 'Not' end) as October,
(case when MONTHNAME(p.DATE) = 'November' then 'Paid' else 'Not' end) as November,
(case when MONTHNAME(p.DATE) = 'December' then 'Paid' else 'Not' end) as December
FROM Members AS m
INNER JOIN AllPayments AS p ON m.Pay_Ref = p.ref`
I have this
SELECT order_customFields.order_customFields_delivery_method,
sum(case `order`.order_status when 'paid' then 1 else 0 end) paid,
sum(case `order`.order_status when 'later' then 1 else 0 end) later,
sum(case `order`.order_status when 'delivery-approved' then 1 else 0 end) deliveryapproved,
sum(case `order`.order_status when 'problem' then 1 else 0 end) problem
FROM order_customFields
INNER JOIN `order` ON order_customFields.order_id = `order`.order_id
WHERE
order_customFields.order_customFields_order_date >= '2016-12-01' AND
order_customFields.order_customFields_order_date <= '2016-12-31'
AND order_customFields.order_customFields_delivery_method is not null
GROUP BY
order_customFields.order_customFields_delivery_method
Look like
How get another string in query, like 'Europe', in which the cells be summ of couriers(all 'eu')? Forexample Europe - paid 1471
you could use union and a proper finter
SELECT order_customFields.order_customFields_delivery_method,
sum(case `order`.order_status when 'paid' then 1 else 0 end) paid,
sum(case `order`.order_status when 'later' then 1 else 0 end) later,
sum(case `order`.order_status when 'delivery-approved' then 1 else 0 end) deliveryapproved,
sum(case `order`.order_status when 'problem' then 1 else 0 end) problem
FROM order_customFields
INNER JOIN `order` ON order_customFields.order_id = `order`.order_id
WHERE
order_customFields.order_customFields_order_date >= '2016-12-01' AND
order_customFields.order_customFields_order_date <= '2016-12-31'
AND order_customFields.order_customFields_delivery_method is not null
GROUP BY
order_customFields.order_customFields_delivery_method
UNION
SELECT 'EUROPE',
sum(case `order`.order_status when 'paid' then 1 else 0 end) paid,
sum(case `order`.order_status when 'later' then 1 else 0 end) later,
sum(case `order`.order_status when 'delivery-approved' then 1 else 0 end) deliveryapproved,
sum(case `order`.order_status when 'problem' then 1 else 0 end) problem
FROM order_customFields
INNER JOIN `order` ON order_customFields.order_id = `order`.order_id
WHERE
order_customFields.order_customFields_order_date >= '2016-12-01' AND
order_customFields.order_customFields_order_date <= '2016-12-31'
AND order_customFields.order_customFields_delivery_method is not null
AND substr(order_customFields.order_customFields_delivery_method,1,3) ='eu_'
GROUP BY
substr(order_customFields.order_customFields_delivery_method,1,3)
I have the following mySQL query which generates a table listing the individual failure data and groups by the month and frequency of different types of failures.
SELECT (CASE WHEN MONTH(runtable.time) = 1 THEN 'Jan'
WHEN MONTH(runtable.time) = 2 THEN 'FEB'
WHEN MONTH(runtable.time) = 3 THEN 'MAR'
WHEN MONTH(runtable.time) = 4 THEN 'APR'
WHEN MONTH(runtable.time) = 5 THEN 'MAY'
WHEN MONTH(runtable.time) = 6 THEN 'JUN'
WHEN MONTH(runtable.time) = 7 THEN 'JUL'
WHEN MONTH(runtable.time) = 8 THEN 'AUG'
WHEN MONTH(runtable.time) = 9 THEN 'SEP'
WHEN MONTH(runtable.time) = 10 THEN 'OCT'
WHEN MONTH(runtable.time) = 11 THEN 'NOV'
WHEN MONTH(runtable.time) = 12 THEN 'DEC'
ELSE 'Error' END) as date_month, runtable.operation, pareto.failure
FROM runtable
JOIN pareto ON pareto.run_id = runtable.id
WHERE runtable.time BETWEEN '2016-01-01 00:00:00' AND '2016-12-13 23:59:59'
AND runtable.operation IN ('d','hr')
GROUP BY MONTH(runtable.time), pareto.failure
ORDER BY MONTH(runtable.time) desc, n desc;
I would like to be able to have the months as separate columns in in the table along with the total for the year, but am unaware of how to do this. My current issue is the only way I have tried of doing this involves using sum or case for each month but then I am unable to group the count of different failure types together for each month.
The way is exactly what you described: you need to use CASE WHEN and SUM functions to solve this. Take a look at this thread:
how to select 2 table like this
By the way, you have easier ways to describe months without a case function as your example code. Take a look:
MySQL DATE_FORMAT() Function
Something like: select DATE_FORMAT(now(),'%m')
I didn't understand your problem with the error column. Be specify what problems are you run into.
Good luck!
As far as I understand you're looking for the sum(pareto.failues) by month of each operation.
Check it here: http://rextester.com/MTDK27603
SELECT
runtable.operation,
pareto.failure,
COUNT(CASE WHEN MONTH(runtable.time) = 1 THEN 1 else NULL end) as 'Jan',
COUNT(CASE WHEN MONTH(runtable.time) = 2 THEN 1 else NULL end) as 'Feb',
COUNT(CASE WHEN MONTH(runtable.time) = 3 THEN 1 else NULL end) as 'Mar',
COUNT(CASE WHEN MONTH(runtable.time) = 4 THEN 1 else NULL end) as 'Apr',
COUNT(CASE WHEN MONTH(runtable.time) = 5 THEN 1 else NULL end) as 'May',
COUNT(CASE WHEN MONTH(runtable.time) = 6 THEN 1 else NULL end) as 'Jun',
COUNT(CASE WHEN MONTH(runtable.time) = 7 THEN 1 else NULL end) as 'Jul',
COUNT(CASE WHEN MONTH(runtable.time) = 8 THEN 1 else NULL end) as 'Aug',
COUNT(CASE WHEN MONTH(runtable.time) = 9 THEN 1 else NULL end) as 'Sep',
COUNT(CASE WHEN MONTH(runtable.time) = 10 THEN 1 else NULL end) as 'Oct',
COUNT(CASE WHEN MONTH(runtable.time) = 11 THEN 1 else NULL end) as 'Nov',
COUNT(CASE WHEN MONTH(runtable.time) = 12 THEN 1 else NULL end) as 'Dec',
COUNT(pareto.failure) as 'Total year'
FROM
runtable
JOIN pareto
ON pareto.run_id = runtable.id
WHERE
runtable.time BETWEEN '2016-01-01 00:00:00' AND '2016-12-13 23:59:59'
AND runtable.operation IN ('d','hr')
GROUP BY
runtable.operation, pareto.failure
;
below is my query
SELECT DISTINCT e2.status as status, e2.year as year, e2.month as month,e2.Jan,e2.Feb,e2.Mar,e2.Apr,e2.May,e2.Jun,e2.Jul,e2.Aug,e2.Sep,e2.Oct,e2.Nov, e2.Dece, e2.countTotal FROM tranx_history e1 INNER JOIN (
SELECT YEAR(e.create_dt) AS year, MONTH(e.create_dt) AS month,
SUM(CASE WHEN MONTH(e.create_dt) = 01 THEN 1 ELSE 0 END) AS Jan,
SUM(CASE WHEN MONTH(e.create_dt) = 02 THEN 1 ELSE 0 END) AS Feb,
SUM(CASE WHEN MONTH(e.create_dt) = 03 THEN 1 ELSE 0 END) AS Mar,
SUM(CASE WHEN MONTH(e.create_dt) = 04 THEN 1 ELSE 0 END) AS Apr,
SUM(CASE WHEN MONTH(e.create_dt) = 05 THEN 1 ELSE 0 END) AS May,
SUM(CASE WHEN MONTH(e.create_dt) = 06 THEN 1 ELSE 0 END) AS Jun,
SUM(CASE WHEN MONTH(e.create_dt) = 07 THEN 1 ELSE 0 END) AS Jul,
SUM(CASE WHEN MONTH(e.create_dt) = 08 THEN 1 ELSE 0 END) AS Aug,
SUM(CASE WHEN MONTH(e.create_dt) = 09 THEN 1 ELSE 0 END) AS Sep,
SUM(CASE WHEN MONTH(e.create_dt) = 10 THEN 1 ELSE 0 END) AS Oct,
SUM(CASE WHEN MONTH(e.create_dt) = 11 THEN 1 ELSE 0 END) AS Nov,
SUM(CASE WHEN MONTH(e.create_dt) = 12 THEN 1 ELSE 0 END) AS Dece,
SUM(CASE WHEN MONTH(e.create_dt) >= 01 AND MONTH(e.create_dt) <= 12 THEN 1 ELSE 0 END) as countTotal,
e.trnx_status as status
from tranx_history e
GROUP BY e.trnx_status, MONTH(e.create_dt),YEAR(e.create_dt) ) e2 ON YEAR(e1.create_dt) = e2.year AND MONTH(e1.create_dt) = e2.month ORDER BY e2.year asc, e2.month asc
For the Above query, The Output we are getting is first it is grouping by Jan then in the next row it is grouping by Feb even though status and year are same
i want the output Jan and Feb with same status and year in a single row not in different rows
I am really new to sql
This is my query:
select rmname, sum(cast (PROPOSEDLIMIT as int)) as january
from los_ext
where applicationdate between '01-01-2016' and '01-31-2016'
group by RMNAME
I want this for every month in single query without using date between function always.
How can i do this?
I want my output as:
RMNAME JAN FEB MARCH APRIL ....
Nitin 2222 3333 33333 34422 333
where sum of propsed limit is to be taken out.
Try the following query:
select
rmname,
sum(CASE WHEN MONTH(applicationDate) = 1 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as january,
sum(CASE WHEN MONTH(applicationDate) = 2 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as February,
sum(CASE WHEN MONTH(applicationDate) = 3 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as March,
sum(CASE WHEN MONTH(applicationDate) = 4 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as April,
sum(CASE WHEN MONTH(applicationDate) = 5 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as May,
sum(CASE WHEN MONTH(applicationDate) = 6 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as June,
sum(CASE WHEN MONTH(applicationDate) = 7 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as July,
sum(CASE WHEN MONTH(applicationDate) = 8 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as August,
sum(CASE WHEN MONTH(applicationDate) = 9 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as September,
sum(CASE WHEN MONTH(applicationDate) = 10 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as October,
sum(CASE WHEN MONTH(applicationDate) = 11 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as November,
sum(CASE WHEN MONTH(applicationDate) = 12 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as December
from los_ext
group by RMNAME,MONTH(applicationDate)
You may gather some knowledge on Pivot Table.
Note: The above query will sum up the data of same months although the months may belong to different years.
For example 2015 February and 2014 February will be summed up to the same slot.
In order to avoid this you may add a date range in where clause like you did.
So adding that date range in the where clause the query would look like below:
select
rmname,
sum(CASE WHEN MONTH(applicationDate) = 1 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as january,
sum(CASE WHEN MONTH(applicationDate) = 2 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as February,
sum(CASE WHEN MONTH(applicationDate) = 3 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as March,
sum(CASE WHEN MONTH(applicationDate) = 4 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as April,
sum(CASE WHEN MONTH(applicationDate) = 5 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as May,
sum(CASE WHEN MONTH(applicationDate) = 6 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as June,
sum(CASE WHEN MONTH(applicationDate) = 7 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as July,
sum(CASE WHEN MONTH(applicationDate) = 8 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as August,
sum(CASE WHEN MONTH(applicationDate) = 9 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as September,
sum(CASE WHEN MONTH(applicationDate) = 10 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as October,
sum(CASE WHEN MONTH(applicationDate) = 11 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as November,
sum(CASE WHEN MONTH(applicationDate) = 12 THEN cast (PROPOSEDLIMIT as int) ELSE 0 END) as December
from los_ext
WHERE applicationdate between '01-01-2016' and '31-12-2016'
group by RMNAME,MONTH(applicationDate)
Do the cast and month stuff in a derived table (to save some typing, and become ANSI SQL compliant.):
select rmname,
sum(case when mnth = 1 then proplim else 0 end) as jan,
sum(case when mnth = 2 then proplim else 0 end) as feb,
...
sum(case when mnth = 12 then proplim else 0 end) as dec
from
(
select rmname, cast (PROPOSEDLIMIT as int) proplim, MONTH(applicationDate) mnth
from los_ext
where applicationdate between '01-01-2016' and '01-31-2016'
) dt
group by RMNAME