Change my MySQL SQL statement to MS SQL - mysql

Can anyone help me to convert this MySQL statement, to MS SQL to run on SQL 2008 R2?
SELECT UNIX_TIMESTAMP(summary.date) as epoch_date,
round(SUM(CASE WHEN meters.group != '' THEN kWh ELSE 0 END),2) as total,
round(SUM(CASE WHEN meters.group = 'eastern' THEN kWh ELSE 0 END),2) as eastern,
round(SUM(CASE WHEN meters.group = 'western' THEN kWh ELSE 0 END),2) as western,
round(SUM(CASE WHEN meters.group = 'central' THEN kWh ELSE 0 END),2) as central
FROM summary, meters
WHERE summary.webmeterID = meters.webmeterID
AND date BETWEEN
DATE_SUB(CURRENT_DATE, INTERVAL 3 YEAR)
AND DATE_SUB(CURRENT_DATE, INTERVAL 1 DAY)
GROUP by date

SELECT
CASE
WHEN DATEDIFF(second,'1970-01-01 00:00:00',summary.[date])>0
THEN DATEDIFF(second,'1970-01-01 00:00:00',summary.[date])
ELSE
0
END as epoch_date,
round(SUM(CASE WHEN meters.[group] != '' THEN kWh ELSE 0 END),2) as total,
round(SUM(CASE WHEN meters.[group] = 'eastern' THEN kWh ELSE 0 END),2) as eastern,
round(SUM(CASE WHEN meters.[group] = 'western' THEN kWh ELSE 0 END),2) as western,
round(SUM(CASE WHEN meters.[group] = 'central' THEN kWh ELSE 0 END),2) as central
FROM summary, meters
WHERE summary.webmeterID = meters.webmeterID
AND [date] BETWEEN
DATEADD(year,-3,GETDATE())
AND DATEADD(day,-1,GETDATE())
GROUP by [date]

Related

Mysql calculate multiple SUM() varialbe

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'

MySQL Query - Join/Union?

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.

Sum of sum in query

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)

MySQL select by month returns NULL for all months

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

Calculate AVG of each Month excluding Months w/zeroes

I have the following working query that sums the length column/values for each month.
SELECT SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 1 THEN (tblcm.Lgth) ELSE 0 END) AS 'Jan',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 2 THEN (tblcm.Lgth) ELSE 0 END) AS 'Feb',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 3 THEN (tblcm.Lgth) ELSE 0 END) AS 'Mar',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 4 THEN (tblcm.Lgth) ELSE 0 END) AS 'Apr',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 5 THEN (tblcm.Lgth) ELSE 0 END) AS 'May',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 6 THEN (tblcm.Lgth) ELSE 0 END) AS 'Jun',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 7 THEN (tblcm.Lgth) ELSE 0 END) AS 'Jul',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 8 THEN (tblcm.Lgth) ELSE 0 END) AS 'Aug',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 9 THEN (tblcm.Lgth) ELSE 0 END) AS 'Sep',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 10 THEN (tblcm.Lgth) ELSE 0 END) AS 'Oct',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 11 THEN (tblcm.Lgth) ELSE 0 END) AS 'Nov',
SUM(CASE WHEN DATE_FORMAT(tblcm.omActCompDate, '%m') = 12 THEN (tblcm.Lgth) ELSE 0 END) AS 'Dec',
SUM(tblcm.Lgth) AS Total
giving me the following:
Jan 13050
Feb 5200
Mar 48450
Apr 34041
May 38000
Jun 0
Jul 0
Aug 0
Sep 0
Oct 0
Nov 0
Dec 0
How can I get the AVG of only the months greater than zero?
I tried: avg(nullif(tblcm.Lgth, 0)) as m_Avg but get 1825
I also tried: avg(case when tblcm.Lgth = 0 then null else tblcm.Lgth end) as m_Avg but also get 1825
I need to get 27748 (which is the SUM of Jan thru May totals, divided by 5 months)
I'm pretty sure that you'll need a subquery to do that. The following assumes that your original query is correct as posted and functions as you said. This is untested, and your probably could have included some more details in your question, so let me now if this doesn't suit your needs.
SELECT AVG(sumLgth) AS avgLgth, SUM(sumLgth) AS totalLgth,
SUM(CASE WHEN theMonth = 1 THEN (tbl.sumLgth) ELSE 0 END) AS 'Jan',
SUM(CASE WHEN theMonth = 2 THEN (tbl.sumLgth) ELSE 0 END) AS 'Feb',
...
FROM (
SELECT MONTH(omActCompDate) AS theMonth, SUM(lgth) AS sumLgth
FROM tblcm
GROUP BY MONTH(omActCompDate)
HAVING sumLgth > 0
) tbl