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)
Related
I'm getting an error of group function while trying to get the volatage stability per hour.
table as an image below.
table-image
SELECT ip,
SUM(CASE HOUR(time) WHEN '1' THEN CAST(AVG(IF(volt=0,0,1)) AS DECIMAL(2,1)) ELSE 0 END) AS '1',
SUM(CASE HOUR(time) WHEN '2' THEN CAST(AVG(IF(volt=0,0,1)) AS DECIMAL(2,1)) ELSE 0 END) AS '2',
SUM(CASE HOUR(time) WHEN '3' THEN CAST(AVG(IF(volt=0,0,1)) AS DECIMAL(2,1)) ELSE 0 END) AS '3'
FROM UPS_Status
WHERE time BETWEEN NOW() - INTERVAL 24 hour AND NOW()
GROUP BY ip, HOUR(time)
HOUR(time) does not belong in the GROUP BY clause. Try removing it:
SELECT
ip,
SUM(CASE HOUR(time) WHEN 1 THEN CAST(IF(volt=0, 0, 1) AS DECIMAL(2,1)) ELSE 0 END) AS `1`,
SUM(CASE HOUR(time) WHEN 2 THEN CAST(IF(volt=0, 0, 1) AS DECIMAL(2,1)) ELSE 0 END) AS `2`,
SUM(CASE HOUR(time) WHEN 3 THEN CAST(IF(volt=0, 0, 1) AS DECIMAL(2,1)) ELSE 0 END) AS `3`
FROM UPS_Status
WHERE
time BETWEEN NOW() - INTERVAL 24 hour AND NOW()
GROUP BY ip;
I want to calculate 2 sum() i have tried query as below it works but very slowly. Any ideas to make it better?
SELECT customer,
SUM(CASE WHEN book_day BETWEEN '2020-01-01' AND '2020-01-31'
THEN pax+free
ELSE 0
END) AS January,
SUM(CASE WHEN book_day BETWEEN '2020-02-01' AND '2020-02-31'
THEN pax+free
ELSE 0
END) AS February,
( SUM(CASE WHEN book_day BETWEEN '2020-01-01' AND '2020-01-31'
THEN pax+free
ELSE 0
END) +
SUM(CASE WHEN book_day BETWEEN '2020-02-01' AND '2020-02-31'
THEN pax+free
ELSE 0
END) ) AS total
FROM rezervations
How can i make simplier like January + February as total
keep BETWEEN '2020-01-01' AND '2020-02-29' in where clause.
make sure there is index on book_day column
SELECT customer,
SUM(CASE WHEN book_day BETWEEN '2020-01-01' AND '2020-01-31' THEN pax+free ELSE 0 END) as January,
SUM(CASE WHEN book_day BETWEEN '2020-02-01' AND '2020-02-29' THEN pax+free ELSE 0 END) as February,
(SUM(CASE WHEN book_day BETWEEN '2020-01-01' AND '2020-02-29' THEN pax+free ELSE 0 END) ) as total
FROM rezervations
WHERE
book_day BETWEEN '2020-01-01' AND '2020-02-29'
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
I have two queries which basically groups the home team for a selected month to get their statistics and then the other groups the away team for the selected month to get their statistics but the third query I want an overall set of statistics so the two of the combined but I am running into a brick wall with everything I try!
SELECT HomeTeam,
SUM(CASE WHEN Month = $FixSelMonth THEN 1 ELSE 0 END) AS Played,
SUM(CASE WHEN Month = $FixSelMonth AND ftr = 'Home' THEN 1 ELSE 0 END) Won,
SUM(CASE WHEN Month = $FixSelMonth AND ftr = 'Draw' THEN 1 ELSE 0 END) Draw,
SUM(CASE WHEN Month = $FixSelMonth AND ftr = 'Away' THEN 1 ELSE 0 END) Lost,
ROUND(SUM(CASE WHEN Month = $FixSelMonth AND ftr = 'Home' THEN 1 ELSE 0 END) /
SUM(CASE WHEN Month = $FixSelMonth THEN 1 ELSE 0 END),2) AS StrikeRate,
ROUND(SUM(CASE WHEN Month = $FixSelMonth AND ftr <> 'TBP' THEN lsph ELSE 0 END),2) AS WinLSP,
ROUND(SUM(CASE WHEN Month = $FixSelMonth AND ftr <> 'TBP' THEN lspd ELSE 0 END),2) AS DrawLSP,
ROUND(SUM(CASE WHEN Month = $FixSelMonth AND ftr <> 'TBP' THEN lspa ELSE 0 END),2) AS LoseLSP
FROM Results_Football
GROUP BY HomeTeam
HAVING Played >= 20 AND WinLSP > 0
ORDER BY StrikeRate DESC
LIMIT 10
SELECT AwayTeam,
SUM(CASE WHEN Month = $FixSelMonth THEN 1 ELSE 0 END) AS Played,
SUM(CASE WHEN Month = $FixSelMonth AND ftr = 'Away' THEN 1 ELSE 0 END) Won,
SUM(CASE WHEN Month = $FixSelMonth AND ftr = 'Draw' THEN 1 ELSE 0 END) Draw,
SUM(CASE WHEN Month = $FixSelMonth AND ftr = 'Home' THEN 1 ELSE 0 END) Lost,
ROUND(SUM(CASE WHEN Month = $FixSelMonth AND ftr = 'Home' THEN 1 ELSE 0 END) /
SUM(CASE WHEN Month = $FixSelMonth THEN 1 ELSE 0 END),2) AS StrikeRate,
ROUND(SUM(CASE WHEN Month = $FixSelMonth AND ftr <> 'TBP' THEN lspa ELSE 0 END),2) AS WinLSP,
ROUND(SUM(CASE WHEN Month = $FixSelMonth AND ftr <> 'TBP' THEN lspd ELSE 0 END),2) AS DrawLSP,
ROUND(SUM(CASE WHEN Month = $FixSelMonth AND ftr <> 'TBP' THEN lsph ELSE 0 END),2) AS LoseLSP
FROM Results_Football
GROUP BY AwayTeam
HAVING Played >= 20 AND WinLSP > 0
ORDER BY StrikeRate DESC
LIMIT 10
So basically what I am trying to do is group by the teams and also get their statistics, if there a way of combining the two queries to achieve this?
Thank you in advance!
Use UNION ALL to combine both the query.
can someone teach me or help me to figure out how to display amount within a fiscal year?. The fiscal year I am using is November to October. So let say Fiscal Year 2016 is from November 2015 to October 2016.
Now my problem is I can only display the amount per month within the chosen year. How can I do this in mysql?
This is the query I have tried, but it works only within a year.
select a.account, a.region, sum(n.amount) as 'Total Net',
sum(case when n.savings_date between date_format(n.savings_date, '%Y-11-01') and last_day(date_format(savings_date, '%Y-11-01')) then n.amount end) as `Nov`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-12-01') and last_day(date_format(savings_date, '%Y-12-01')) then n.amount end) as `Dec`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-01-01') and last_day(date_format(savings_date, '%Y-01-01')) then n.amount end) as `Jan`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-02-01') and last_day(date_format(savings_date, '%Y-02-01')) then n.amount end) as `Feb`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-03-01') and last_day(date_format(savings_date, '%Y-03-01')) then n.amount end) as `Mar`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-04-01') and last_day(date_format(savings_date, '%Y-04-01')) then n.amount end) as `Apr`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-05-01') and last_day(date_format(savings_date, '%Y-05-01')) then n.amount end) as `May`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-06-01') and last_day(date_format(savings_date, '%Y-06-01')) then n.amount end) as `Jun`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-07-01') and last_day(date_format(savings_date, '%Y-07-01')) then n.amount end) as `Jul`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-08-01') and last_day(date_format(savings_date, '%Y-08-01')) then n.amount end) as `Aug`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-09-01') and last_day(date_format(savings_date, '%Y-09-01')) then n.amount end) as `Sep`,
sum(case when n.savings_date between date_format(n.savings_date, '%Y-10-01') and last_day(date_format(savings_date, '%Y-10-01')) then n.amount end) as `Oct`
from
net_savings n left join accounts a
on a.id = n.account_id
where year(n.savings_date) = '2016'
group by a.account
order by a.account desc
I am needing this kind of output also:
Appreciate your answers or suggestions! :)
Try this
SELECT
a.account,
a.region,
YEAR(n.savings_date),
MONTH(n.savings_date),
SUM(n.amount)
FROM
net_savings n
LEFT JOIN
accounts a
ON
a.id = n.account_id
WHERE
n.savings_date >= '2015-11-01' and n.savings_date < '2016-09-01'
GROUP BY
a.account,
a.region,
YEAR(n.savings_date),
MONTH(n.savings_date),
ORDER BY
a.account DESC