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;
Related
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'
Why am I getting an error with the following code:
SELECT
Hour,
SUM( CASE col WHEN '1' THEN data ELSE 0 END ) AS 'Sun',
SUM( CASE col WHEN '2' THEN data ELSE 0 END ) AS 'Mon',
SUM( CASE col WHEN '3' THEN data ELSE 0 END ) AS 'Tues',
SUM( CASE col WHEN '4' THEN data ELSE 0 END ) AS 'Wed',
SUM( CASE col WHEN '5' THEN data ELSE 0 END ) AS 'Thur',
SUM( CASE col WHEN '6' THEN data ELSE 0 END ) AS 'Fri',
SUM( CASE col WHEN '7' THEN data ELSE 0 END ) AS 'Sat'
FROM (
SELECT
HOUR(arrPurch.PurchaseDate) as HOUR,
DAYOFWEEK(arrPurch.PurchaseDate) as col,
SUM(ROUND(arrPurch.Credits * 1.85 + arrPurch.Price,0)) AS Data
FROM praisecharts_main_new.arrangementPurchases AS arrPurch
INNER JOIN praisecharts_main_new.catalog_dev_arrangements
AS catDEVarr ON arrPurch.ArrangementID = catDEVarr.ArrangementID
INNER JOIN praisecharts_main_new.members
ON arrPurch.MemberID = members.MemberID
GROUP BY HOUR(arrPurch.PurchaseDate), DAYOFWEEK(arrPurch.PurchaseDate)
) AS stats
WHERE
Hour BETWEEN 0 AND 23
-- AND arrPurch.PurchaseDate BETWEEN (CURDATE() - INTERVAL 29 DAY)
-- AND (CURDATE() - INTERVAL 1 DAY)
-- AND arrPurch.PurchaseDate > (CURDATE() - INTERVAL 1 DAY)
GROUP BY Hour WITH ROLLUP
When I don't try to PIVOT the results with the CASE clauses, I don't get an error. Here is an example of a query that works:
SELECT
DAYOFWEEK(arrPurch.PurchaseDate) AS DayWeek,
FORMAT(SUM(arrPurch.Credits*1.85+arrPurch.Price)/4,0) AS `c.Rev`
FROM
praisecharts_main_new.arrangementPurchases AS arrPurch
INNER JOIN praisecharts_main_new.catalog_dev_arrangements AS catDEVarr ON
arrPurch.ArrangementID = catDEVarr.ArrangementID
INNER JOIN praisecharts_main_new.members
ON arrPurch.MemberID = members.MemberID
WHERE arrPurch.PurchaseDate BETWEEN (CURDATE() - INTERVAL 29 DAY)
AND (CURDATE() - INTERVAL 1 DAY)
GROUP BY DayWeek WITH ROLLUP;
LIMIT 14
I was able to find the answer. I moved the WHERE clause up to the embedded SELECT cause above, rather than at the bottom. Here is an example of the code that works.
SELECT
Hour,
SUM( CASE col WHEN '1' THEN data ELSE 0 END ) AS 'Sun',
SUM( CASE col WHEN '2' THEN data ELSE 0 END ) AS 'Mon',
SUM( CASE col WHEN '3' THEN data ELSE 0 END ) AS 'Tues',
SUM( CASE col WHEN '4' THEN data ELSE 0 END ) AS 'Wed',
SUM( CASE col WHEN '5' THEN data ELSE 0 END ) AS 'Thur',
SUM( CASE col WHEN '6' THEN data ELSE 0 END ) AS 'Fri',
SUM( CASE col WHEN '7' THEN data ELSE 0 END ) AS 'Sat'
FROM (
SELECT
HOUR(arrPurch.PurchaseDate) as HOUR,
DAYOFWEEK(arrPurch.PurchaseDate) as col,
SUM(ROUND((arrPurch.Credits * 1.85 + arrPurch.Price)/4,0)) AS data
FROM praisecharts_main_new.arrangementPurchases AS arrPurch
INNER JOIN praisecharts_main_new.catalog_dev_arrangements
AS catDEVarr ON arrPurch.ArrangementID = catDEVarr.ArrangementID
INNER JOIN praisecharts_main_new.members
ON arrPurch.MemberID = members.MemberID
WHERE arrPurch.PurchaseDate BETWEEN (CURDATE() - INTERVAL 29 DAY)
AND (CURDATE() - INTERVAL 1 DAY)
GROUP BY HOUR(arrPurch.PurchaseDate),
DAYOFWEEK(arrPurch.PurchaseDate)
) AS stats
WHERE
Hour BETWEEN 0 AND 23
GROUP BY Hour WITH ROLLUP
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 want to count how many values of 1 and 0 i have between 7am and 7am next day.
think here a bar that opens at 10am but closes next day at 7am, they want their 'report' to be for this specific day.
so all data before 7am needs to be added to the day before.
SELECT DATE(delivered), COUNT(*) total,
sum(case when isbag = '0' THEN 1 ELSE 0 END) CloakCount,
sum(case when isbag = '1' THEN 1 ELSE 0 END) BagCount
FROM Wardrobe_CloakTable GROUP BY DATE(delivered)
this will give me almost what i want, the problem is that i need to count all those data before 7am, to the day before.
Something like this should work:
SELECT
CASE WHEN HOUR(delivered) < 7
THEN CONCAT(DATE(DATE_ADD(delivered, INTERVAL -1 day)), ' 07:00:00')
ELSE CONCAT(DATE(delivered), ' 07:00:00')
END as startTime,
CASE WHEN HOUR(delivered) < 7
THEN CONCAT(DATE(delivered), ' 07:00:00')
ELSE CONCAT(DATE(DATE_ADD(delivered, INTERVAL 1 day)), ' 07:00:00')
END as endTime,
COUNT(*) total,
sum(case when isbag = '0' THEN 1 ELSE 0 END) CloakCount,
sum(case when isbag = '1' THEN 1 ELSE 0 END) BagCount
FROM Wardrobe_CloakTable GROUP BY startTime, endTime;
Using MySQL 5.6 I am trying to select the total inspections for each month in the past 12 months with an output of 0 if there are none. I appear to be missing something here as the output is all NULL. The date_inspected field is just a regular SQL date
From what I understood, the structure should be [conditional statement, output variable, default value] but even the 0 gets ignored in favor of NULL. I am trying to understand what I am doing wrong here.
Table:
Column Type Null
id int(11) No
inspector_id int(11) Yes
company_id int(11) Yes
date_inspected date No
start_time datetime No
end_time datetime No
Query:
SELECT
SUM(IF(MONTH = 'Jan', total, 0)) AS 'Januari',
SUM(IF(MONTH = 'Feb', total, 0)) AS 'Februari',
SUM(IF(MONTH = 'Mar', total, 0)) AS 'Maart',
SUM(IF(MONTH = 'Apr', total, 0)) AS 'April',
SUM(IF(MONTH = 'May', total, 0)) AS 'Mei',
SUM(IF(MONTH = 'Jun', total, 0)) AS 'Juni',
SUM(IF(MONTH = 'Jul', total, 0)) AS 'Juli',
SUM(IF(MONTH = 'Aug', total, 0)) AS 'Augustus',
SUM(IF(MONTH = 'Sep', total, 0)) AS 'September',
SUM(IF(MONTH = 'Oct', total, 0)) AS 'Oktober',
SUM(IF(MONTH = 'Nov', total, 0)) AS 'November',
SUM(IF(MONTH = 'Dec', total, 0)) AS 'December',
SUM(total) AS all_months
FROM (
SELECT MONTH(date_inspected) AS MONTH, COUNT(*) AS total
FROM inspection
WHERE date_inspected BETWEEN NOW() AND Date_add(NOW(), interval - 12 month)
GROUP BY MONTH
) AS SubTable
Output
{ ["Januari"]=> NULL
["Februari"]=> NULL
["Maart"]=> NULL
["April"]=> NULL
["Mei"]=> NULL
["Juni"]=> NULL
["Juli"]=> NULL
["Augustus"]=> NULL
["September"]=> NULL
["Oktober"]=> NULL
["November"]=> NULL
["December"]=> NULL
["all_months"]=> NULL }
Update:
SOLUTION by Gordon Linoff
SELECT
SUM(CASE WHEN MONTH(date_inspected) = 1 THEN 1 ELSE 0 END) AS 'Januari',
SUM(CASE WHEN MONTH(date_inspected) = 2 THEN 1 ELSE 0 END) AS 'Februari',
SUM(CASE WHEN MONTH(date_inspected) = 3 THEN 1 ELSE 0 END) AS 'Maart',
SUM(CASE WHEN MONTH(date_inspected) = 4 THEN 1 ELSE 0 END) AS 'April',
SUM(CASE WHEN MONTH(date_inspected) = 5 THEN 1 ELSE 0 END) AS 'Mei',
SUM(CASE WHEN MONTH(date_inspected) = 6 THEN 1 ELSE 0 END) AS 'Juni',
SUM(CASE WHEN MONTH(date_inspected) = 7 THEN 1 ELSE 0 END) AS 'Juli',
SUM(CASE WHEN MONTH(date_inspected) = 8 THEN 1 ELSE 0 END) AS 'Augustus',
SUM(CASE WHEN MONTH(date_inspected) = 9 THEN 1 ELSE 0 END) AS 'September',
SUM(CASE WHEN MONTH(date_inspected) = 10 THEN 1 ELSE 0 END) AS 'Oktober',
SUM(CASE WHEN MONTH(date_inspected) = 11 THEN 1 ELSE 0 END) AS 'November',
SUM(CASE WHEN MONTH(date_inspected) = 12 THEN 1 ELSE 0 END) AS 'December'
FROM inspection
WHERE date_inspected BETWEEN Date_add(NOW(), interval - 12 month) AND NOW()
As I understand it, we've given the SUM() a conditional CASE statement that if the current record's date_inspected's MONTH is equal to the MySQL constant for that value, return true and add it to the total, else do nothing.
More on MySQL CASE
between must use the smaller value first and not the other way around. Change
BETWEEN NOW() AND Date_add(NOW(), interval - 12 month)
to
BETWEEN Date_add(NOW(), interval - 12 month) and NOW()
And month() returns a number of the month and not the name.
Juergen is correct on one problem in your query. Another is that MONTH() returns a number, not a string.
And, you can further simplify the query. A subquery is not needed:
SELECT SUM(CASE WHEN MONTH(date_inspected) = 1 THEN 1 ELSE 0 END)) AS 'Januari',
SUM(CASE WHEN MONTH(date_inspected) = 2 THEN 1 ELSE 0 END)) AS 'Februari',
. . .
FROM inspection
WHERE date_inspected BETWEEN Date_add(NOW(), interval - 12 month) AND NOW();