Finding percentage difference from different values in the same SQL Table? - mysql

I have a table that tracks total values for months against years in a particular location.
Desired Outcome: I wanted to compare a month's value for the current year against last years value. I then wanted to check for a percentage increase.
e.g. 2014 (January) = 140 - 2013 (January) = 150 * 100 = - 6.67
Table Name- donation_tracker
Thank you in advance.

As I understood, You want to get the percent of increase from last year to current year for the Same month for a Particular location. Use the query.
SELECT D1.month, ROUND((D2.Donation_amount- D1.Donation_amount) * 100 /
D1.Donation_amount, 2)
FROM donation_tracker D1
INNER JOIN donation_tracker D2
ON d1.month = D2.month AND D1.year = D2.year - 1
AND D1.Location_ID = D2.Location_ID;

Let's say you need to compare the immediately-completed twelve months with the twelve months prior to that, month-by-month. I am guessing at your table and column names because, well, I don't know them.
Let's build this from the ground up.
Here's a query that will find the most recent twelve months of donations month by month.
SELECT YEAR(donation_date) AS donation_year,
MONTH(donation_date) AS donation_month,
SUM(donation_amount) AS donation_amount
FROM donations
WHERE donation_date >= LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 13 MONTH
AND donation_date < LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 1 MONTH
GROUP BY YEAR(donation_date), MONTH(donation_date)
That gives you a twelve-row result set like this (when NOW() happens to be in the middle of November 2014):
2013 11 145
2013 12 220
2014 1 123
2014 2 11
...
2014 10 45
The trick is picking the right range of donation_date values.
So, now you need two of those result sets, one for mostly-2014 and one for mostly-2013. The one for mostly-2013 looks very similar. You simply back up one more year like this.
SELECT YEAR(donation_date) AS donation_year,
MONTH(donation_date) AS donation_month,
SUM(donation_amount) AS donation_amount
FROM donations
WHERE donation_date >= LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 25 MONTH
AND donation_date < LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 13 MONTH
GROUP BY YEAR(donation_date), MONTH(donation_date)
This is going to be one of those notorious club-sandwich queries, made of those two basic queries. You join them by month like so, then do the percentage computation in the SELECT clause.
SELECT a.donation_month,
a.donation_amount AS this_year,
b.donation_amount AS last_year,
100.0 * (a.donation_amount - b.donation_amount) / b.donation_amount as pct_increase
FROM (
/* this year's query */
) AS a
JOIN (
/* last year's query */
) AS b ON a.donation_month = b.donation_month
ORDER BY a.donation_year, a.donation_month
Here's the whole club sandwich for your server to chew on. Yummy!
SELECT a.donation_month,
a.donation_amount AS this_year,
b.donation_amount AS last_year,
100.0 * (a.donation_amount - b.donation_amount) / b.donation_amount as pct_increase
FROM (
SELECT YEAR(donation_date) AS donation_year,
MONTH(donation_date) AS donation_month,
SUM(donation_amount) AS donation_amount
FROM donations
WHERE donation_date >= LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 13 MONTH
AND donation_date < LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 1 MONTH
GROUP BY YEAR(donation_date), MONTH(donation_date)
) AS a
JOIN (
SELECT YEAR(donation_date) AS donation_year,
MONTH(donation_date) AS donation_month,
SUM(donation_amount) AS donation_amount
FROM donations
WHERE donation_date >= LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 25 MONTH
AND donation_date < LAST_DAY(NOW()) + INTERVAL 1 DAY - INTERVAL 13 MONTH
GROUP BY YEAR(donation_date), MONTH(donation_date)
) AS b ON a.donation_month = b.donation_month
ORDER BY a.donation_year, a.donation_month
Once you stack up the whole club sandwich, it look complicated. But it's actually a stack of simple subqueries.

This should give you an idea :)
Sample data:
CREATE TABLE t
(`month` varchar(3), `year` int, `amount` int)
;
INSERT INTO t
(`month`, `year`, `amount`)
VALUES
('jan', 2013, 150),
('feb', 2013, 180),
('jan', 2014, 140),
('feb', 2014, 160)
;
Query:
select
t1.month, round((t2.amount - t1.amount) * 100 / t1.amount, 2)
from
t t1
inner join t t2 on t1.month = t2.month and t1.year < t2.year;
Result:
| MONTH | ROUND((T2.AMOUNT - T1.AMOUNT) * 100 / T1.AMOUNT, 2) |
|-------|-----------------------------------------------------|
| jan | -6.67 |
| feb | -11.11 |

Related

Query data in time interval that spans two days in mysql

I need to count data for a given <week | month | custom> interval grouped by a given time schedule that possibly spans 2 days. The chunks depends on customers working schedules.
Possible cases (for one month interval) :
all data from June 01, 2022 to July 01, 2022, each day between 08:00 pm and 04:00 am (overnight)
all data from June 01, 2022 to June 30, 2022, each day between 04:00 am and 08:00 pm
all data from June 01, 2022 to June 30, 2022, each day between 00:00 am and 23:59 pm
Here's what I came up with:
WITH RECURSIVE seq AS (
SELECT
0 AS value
UNION ALL
SELECT
value + 1
FROM
seq
WHERE
value < 29
),
period AS (
SELECT
'2022-06-01 20:00' + INTERVAL (value * 24 * 60) MINUTE AS start,
'2022-06-01 20:00' + INTERVAL (value * 24 * 60) MINUTE + INTERVAL (8* 60) MINUTE AS end
FROM seq
ORDER BY value DESC
)
SELECT *
FROM (
SELECT
DATE(sd.timestamp - INTERVAL(LEAST(20, 4)) HOUR) as date,
SUM(...) as count,
FROM sensor_data sd
WHERE sd.timestamp BETWEEN '2022-06-01 20:00' AND '2022-07-01 04:00'
AND HOUR(sd.timestamp) >= 20 AND HOUR(sd.timestamp) < 4
GROUP BY
date
) main_data
INNER JOIN period ON DATE(period.start) = date
Unfortunately in doesn't work for the first case (spans two days). Any ideas?
WITH RECURSIVE
cte AS (
SELECT datetime_from range_from,
datetime_from + INTERVAL range_length HOUR range_till
UNION ALL
SELECT range_from + INTERVAL 1 DAY,
range_till + INTERVAL 1 DAY
FROM cte
WHERE range_till < datetime_till
)
SELECT range_from, range_till, COUNT(*) rows_amount
FROM cte
LEFT JOIN test ON test.dt BETWEEN range_from AND range_till
GROUP BY 1, 2;
DEMO with some explanations.

Select the next day occurance from the table

This is my db structure of the table game days. Every game ends at 8:00 pm . Here all the game id is same.So now i want to query by the game id like this way that if today is monday and it is before 8:00 pm so it will fetch only the game_days with value of day='Monday'.Once 8:00pm is over then it will show the row with the value of day='Wednesday' until wednesday 8:00pm and after 8:00 pm it will show the row with the value of day='Friday' until Friday 8:00pm and then again after Friday 8:00pm it will show Monday...
So what will be the query for this ?
I think you just want the date offset by 4 hours. One method is:
where date(updated_at + interval 4 hour) = curdate()
I am guessing that updated_at is the column you want to reference, but it can be any date/time column.
I prefer to express this as:
where updated_at >= curdate() - interval 4 hour and
updated_at < curdate() + interval 1 day - interval 4 hour
This version can make use of an index on the date/time column.
Please try this (updated based on comments, replace table_name with appropriate table name is your db):
SELECT *
FROM `table_name`
WHERE day = (
SELECT t3.day FROM `table_name` as t3
LEFT JOIN (
SELECT HOUR(NOW()),
CASE
WHEN HOUR(NOW()) <= 19 THEN DAYNAME(NOW() + INTERVAL t.p DAY)
ELSE DAYNAME(NOW() + INTERVAL (t.p + 1) DAY)
END AS day,
CASE
WHEN HOUR(NOW()) <= 19 THEN t.p
ELSE t.p + 1
END as p
FROM (
SELECT 0 as p
UNION SELECT 1 as p
UNION SELECT 2 as p
UNION SELECT 3 as p
UNION SELECT 4 as p
UNION SELECT 5 as p
UNION SELECT 6 as p
) t
) t2 on t3.day = t2.day
ORDER BY t2.p ASC LIMIT 1
)

Introducing a new column for previous month sum

I am trying to introduce a new column for my query, it currently counts sum of expenses in this month, the new column should display last months expences. I am not quite sure where to place it.
SELECT cat.id_kat,
cat.nazwa,
coalesce(exp.tot, 0) AS PriceTotal
FROM wydatki_kategorie cat
LEFT JOIN
(SELECT wydatki_wpisy.kategoria,
sum(wydatki_wpisy.brutto) AS tot
FROM wydatki_wpisy
LEFT JOIN wydatki ON wydatki_wpisy.do_wydatku = wydatki.id_wydatku
WHERE MONTH(wydatki.data_zakupu) = MONTH(CURRENT_DATE())
AND wydatki.id_kupujacy = 1
GROUP BY wydatki_wpisy.kategoria) exp ON cat.id_kat = exp.kategoria
Possibly might be needed ( if I'm not wrong ) - Where clause for the previous month.
wydatki.data_zakupu >= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY) AND
wydatki.data_zakupu<= DATE_SUB(NOW(), INTERVAL 1 MONTH)
SQL Fiddle example
Two things.
First, if you stop using WHERE MONTH(wydatki.data_zakupu) = MONTH(CURRENT_DATE()) to choose your dates you'll get three benefits.
Your date searching will become sargable: an index will speed it up.
You'll get a more general scheme for choosing months.
If you have multiple years' worth of data in your tables, things will work better.
Instead, in general use this sort of expression to search for the present month. You already figured out most of this.
WHERE wydatki.data_zakupu >= LAST_DAY(CURRENT_DATE()) + INTERVAL 1 DAY - INTERVAL 1 MONTH
AND wydatki.data_zakupu < LAST_DAY(CURRENT_DATE()) + INTERVAL 1 DAY - INTERVAL 0 MONTH
This looks for all datetime values on or after midnight at the first day of the present month, and before, but not on <, midnight at the first day of next month.
It generalizes to any month you want. For example,
WHERE wydatki.data_zakupu >= LAST_DAY(CURRENT_DATE()) + INTERVAL 1 DAY - INTERVAL 2 MONTH
AND wydatki.data_zakupu < LAST_DAY(CURRENT_DATE()) + INTERVAL 1 DAY - INTERVAL 1 MONTH
gets you last month. This also works when the current month is January, and it works when you have multiple years' worth of data in your tables.
These expressions are a little verbose because MySQL doesn't have a FIRST_DAY(date) function, only a LAST_DAY(date) function. So we need all that + INTERVAL 1 DAY monkey business.
Second, pulling out a previous month's data is as simple as adding another LEFT JOIN ( SELECT... clause to your table, like so. (http://sqlfiddle.com/#!9/676df4/13)
SELECT ...
coalesce(month1.tot, 0) AS LastMonth
FROM wydatki_kategorie cat
LEFT JOIN
...
LEFT JOIN
(SELECT wydatki_wpisy.kategoria,
sum(wydatki_wpisy.brutto) AS tot
FROM wydatki_wpisy
LEFT JOIN wydatki ON wydatki_wpisy.do_wydatku = wydatki.id_wydatku
WHERE wydatki.data_zakupu >= LAST_DAY(CURRENT_DATE()) + INTERVAL 1 DAY - INTERVAL 2 MONTH
AND wydatki.data_zakupu < LAST_DAY(CURRENT_DATE()) + INTERVAL 1 DAY - INTERVAL 1 MONTH
AND wydatki.id_kupujacy = 1
GROUP BY wydatki_wpisy.kategoria
) month1 ON cat.id_kat = month1.kategoria
As you can see, the date range WHERE clause here gets the previous month's rows.

Percentage change from previous row when multiple rows have the same date?

I have a query that calculates ratios and returns them for each hour and server on a given day:
SELECT a.day,
a.hour,
Sum(a.gemspurchased),
Sum(b.gems),
Sum(b.shadowgems),
( Sum(b.gems) / Sum(a.gemspurchased) ) AS GemRatio,
( Sum(b.shadowgems) / Sum(a.gemspurchased) ) AS ShadowGemRatio
FROM (SELECT Date(Date_sub(createddate, INTERVAL 7 hour)) AS day,
Hour(Date_sub(createddate, INTERVAL 7 hour)) AS hour,
serverid,
Sum(gems) AS GemsPurchased
FROM dollartransactions
WHERE Date(Date_sub(createddate, INTERVAL 7 hour)) BETWEEN
Curdate() - INTERVAL 14 day AND Curdate()
GROUP BY 1,
2,
3) a,
/*Gems recorded from DollarTransactions Table after purchasing gem package*/
(SELECT Date(Date_sub(createddate, INTERVAL 7 hour)) AS day,
Hour(Date_sub(createddate, INTERVAL 7 hour)) AS hour,
serverid,
Sum(acceptedamount) AS Gems,
Sum(acceptedshadowamount) AS ShadowGems
FROM gemtransactions
WHERE Date(Date_sub(createddate, INTERVAL 7 hour)) BETWEEN
Curdate() - INTERVAL 14 day AND Curdate()
AND transactiontype IN ( 990, 2 )
AND fullfilled = 1
AND gemtransactionid >= 130000000
GROUP BY 1,
2,
3) b
/*Gems & Shadow Gems spent, recorded from GemTransactions Table */
WHERE a.day = b.day
AND a.serverid = b.serverid
GROUP BY 1,
2
This code returns the component parts of the ratios, as well as the ratios themselves (which are sometimes null):
day hour sum(a.GemsPurchased) sum(b.Gems) sum(b.ShadowGems) GemRatio ShadowGemRatio
9/5/2014 0 472875 465499 60766 0.9844 0.1285
9/5/2014 1 350960 371092 45408 1.0574 0.1294
9/5/2014 2 472985 509618 58329 1.0775 0.1233
9/5/2014 3 1023905 629310 71017 0.6146 0.0694
9/5/2014 4 1273170 628697 74896 0.4938 0.0588
9/5/2014 5 998920 637709 64145 0.6384 0.0642
9/5/2014 6 876470 651451 68977 0.7433 0.0787
9/5/2014 7 669100 667217 81599 0.9972 0.122
What I'd like to do is create an 8th and 9th column which calculate the % change from previous row for both GemRatio and ShadowGemRatio. I've seen other threads here on how to do this for specific queries, but I couldn't get it to work for my particular MySQL query...
Ok first create a view for that query. Let's call it v1:
CREATE VIEW v1 AS SELECT YOUR QUERY HERE;
Now here is the query to have the ratios. I assumed a day has 24 hours. The first row ratio change will be zero.
select now.*,
CASE
WHEN yesterday.gemRatio is null THEN 0
ELSE 100*(now.gemRatio-yesterday.gemRatio)/yesterday.gemRatio
END as gemChange,
CASE
WHEN yesterday.ShadowGemRatio is null THEN 0
ELSE 100*(now.ShadowGemRatio-yesterday.ShadowGemRatio)/yesterday.ShadowGemRatio
END as shadowGemChange
from v1 now left outer join v1 yesterday on
((now.day = yesterday.day && now.hour = yesterday.hour+1) ||
(DATEDIFF(now.day,yesterday.day) = 1 && now.hour = 0 && yesterday.hour=23))

Changing start-date in MySQL for week

I found the following code to help in creating a weekly report based on a start date of Friday. The instructions say to replace ".$startWeekDay." with a 4. When I put '".$startDay."' as '2013-01-30', I get errors.
Also I get a report by day rather than week as I desire.
SELECT SUM(cost) AS total,
CONCAT(IF(date - INTERVAL 6 day < '".$startDay."',
'".$startDay."',
IF(WEEKDAY(date - INTERVAL 6 DAY) = ".$startWeekDay.",
date - INTERVAL 6 DAY,
date - INTERVAL ((WEEKDAY(date) - ".$startWeekDay.")) DAY)),
' - ', date) AS week,
IF((WEEKDAY(date) - ".$startWeekDay.") >= 0,
TO_DAYS(date) - (WEEKDAY(date) - ".$startWeekDay."),
TO_DAYS(date) - (7 - (".$startWeekDay." - WEEKDAY(date)))) AS sortDay
FROM daily_expense
WHERE date BETWEEN '".$startDay."' AND '".$endDay."'
GROUP BY sortDay;
The following code is what I am using
SELECT count(DISTINCT (
UserID)
) AS total, CONCAT(IF(date(LastModified) - INTERVAL 6 day < date(LastModified),
date(LastModified),
IF(WEEKDAY(date(LastModified) - INTERVAL 6 DAY) = 4,
date(LastModified) - INTERVAL 6 DAY,
date(LastModified) - INTERVAL ((WEEKDAY(date(LastModified)) - 4)) DAY)),
' - ', date(LastModified)) AS week
FROM `Purchase`
WHERE `OfferingID` =87
AND `Status`
IN ( 1, 4 )
GROUP BY week
The output I get is
total week
3 2013-01-30 - 2013-01-30
1 2013-01-31 - 2013-01-31
I'm not sure exactly how you want to display your week, the sql above is attempting to display date ranges. If this isn't a requirement, your query could be very simple, you can just offset your time by two days (since friday is two days away from the natural star of the week) and use the week function to get the week number.
The query would look like this:
select count(distinct (UserID)) as total
, year( LastModified + interval 2 day ) as year
, week( LastModified + interval 2 day ) as week_number
FROM `Purchase`
WHERE `OfferingID` =87
AND `Status`
IN ( 1, 4 )
group by year, week_number;