MySQL Query - Join/Union? - mysql

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.

Related

Mysql - Payments table by month column

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`

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'

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)

Get Data for Grouping all Months in a year using MySql and DB2

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

Change my MySQL SQL statement to MS SQL

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]