Using MySQL. I want to get cumulative sum.
This is my table
CREATE TABLE `user_infos`
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
(..)
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`) )
And what I want to get is
+-------+-------+----------------+
| month | count | cumulative_sum |
+-------+-------+----------------+
| 01 | 100 | 100 |
| 02 | 101 | 201 |
| ... | 110 | 311 |
| 12 | 200 | 511 |
+-------+-------+----------------+
but the result is
+-------+-------+----------------+
| month | count | cumulative_sum |
+-------+-------+----------------+
| 01 | 100 | 100 |
| 02 | 101 | 101 |
| ... | 110 | 110 |
| 12 | 200 | 200 |
+-------+-------+----------------+
This is my wrong query..
select
T1.Month,T1.Count,
#runnung_total := (#running_total + T1.Count) as cumulative_sum
from (
select date_format(created_at,'%m') as Month,count(1) as Count from users
where date_format(created_at,'%Y')='2016'
group by(date_format(created_at,'%m'))
union
select date_format(created_at,'%m') as Month,count(1) as Count from users
where date_format(created_at,'%Y')='2017'
group by(date_format(created_at,'%m')) ) as T1
join (select #running_total := 0) as R1;
I referred to this. What's wrong in my code?
You can achieve that in two steps: first of all get the sum for each year and month
select concat(year(created_at), lpad(month(created_at), 2, '0')) as ye_mo,
count(*) as cnt
from users
group by concat(year(created_at), lpad(month(created_at), 2, '0'))
Then join it with itself, having each row matched with all previous ones
select t1.ye_mo, sum(t2.cnt)
from (
select concat(year(created_at), lpad(month(created_at), 2, '0')) as ye_mo,
count(*) as cnt
from users
group by concat(year(created_at), lpad(month(created_at), 2, '0'))
) t1
join (
select concat(year(created_at), lpad(month(created_at), 2, '0')) as ye_mo,
count(*) as cnt
from users
group by concat(year(created_at), lpad(month(created_at), 2, '0'))
) t2
on t1.ye_mo >= t2.ye_mo
group by t1.ye_mo
order by t1.ye_mo
Edit
The query above assumes you want the running sum to increase across different years. If you want to display the months only, and aggregate the values of different years in the same month, you can change id this way
select t1.mnt, sum(t2.cnt)
from (
select month(created_at) as mnt,
count(*) as cnt
from userss
group by month(created_at)
) t1
join (
select month(created_at) as mnt,
count(*) as cnt
from userss
group by month(created_at)
) t2
on t1.mnt >= t2.mnt
group by t1.mnt
order by t1.mnt
Finally, if you want the running sum to reset at the beginning of each year, you can do that like this
select t1.yr, t1.mn, sum(t2.cnt)
from (
select year(created_at) as yr, month(created_at) as mn,
count(*) as cnt
from userss
group by year(created_at), month(created_at)
) t1
join (
select year(created_at) as yr, month(created_at) as mn,
count(*) as cnt
from userss
group by year(created_at), month(created_at)
) t2
on t1.yr = t2.yr and
t1.mn >= t2.mn
group by t1.yr, t1.mn
order by t1.yr, t1.mn
All three versions can be seen in action here
Variables are the right way to go. You can simplify your query:
select m.Month, m.cnt,
(#running_total := (#running_total + m.cnt) ) as cumulative_sum
from (select month(created_at) as Month, count(*) as cnt
from users
where year(created_at) in (2016, 2017)
group by month(created_at)
) m cross join
(select #running_total := 0) params
order by m.Month;
Starting with MySQL 8, the ideal approach to calculate cumulative sums is by using SQL standard window functions rather than the vendor-specific, and not stricly declarative approach of using local variables. Your query can be written as follows:
WITH data(month, count) AS (
SELECT date_format(create_at, '%m') AS month, count(*) AS count
FROM users
GROUP BY date_format(create_at, '%m')
)
SELECT
month,
count,
sum(count) OVER (ORDER BY month) AS cumulative_sum
FROM data
Related
This is the query that I am using.
I need to join the three views to calculate the monthly total revenue.
How should I proceed?
With Txn as(
Select DATE_FORMAT(DATE_ADD(createdAt, interval 330 MINUTE), '%y-%m') as Month, Sum(netPrice/100) as TransactionRevenue from transactions
group by Month)
With Leaves as(
Select DATE_FORMAT(DATE_ADD(createdAt, interval -1 MONTH), '%y-%m') as Month, sum(amount/100) as LeaveRevenue from driverPaymentTransactions
group by Month)
With Sxn as(
Select DATE_FORMAT(DATE_ADD(createdAt, interval 330 MINUTE ), '%y-%m') as Month, sum(amount/100) as SubscribedRevenue from subscribedDriversDailyRevenues
group by MONTH)
Select * from Txn t
join Leaves l on t.Month = l.month
join Sxn s on t.month = s.month
With Txn as(
Select DATE_FORMAT(DATE_ADD(createdAt, interval 330 MINUTE), '%y-%m') as Month, Sum(netPrice/100) as TransactionRevenue from transactions
group by Month),
Leaves as(
Select DATE_FORMAT(DATE_ADD(createdAt, interval -1 MONTH), '%y-%m') as Month, sum(amount/100) as LeaveRevenue from driverPaymentTransactions
group by Month),
Sxn as(
Select DATE_FORMAT(DATE_ADD(createdAt, interval 330 MINUTE ), '%y-%m') as Month, sum(amount/100) as SubscribedRevenue from subscribedDriversDailyRevenues
group by MONTH)
Select * from Txn t
join Leaves l on t.Month = l.month
join Sxn s on t.month = s.month
You need to "join" the subqueries
CREATE VIEw myview
AS (With Txn as(
Select DATE_FORMAT(DATE_ADD(createdAt, interval 330 MINUTE), '%y-%m') as Month, Sum(netPrice/100) as TransactionRevenue from transactions
group by Month)
, Leaves as(
Select DATE_FORMAT(DATE_ADD(createdAt, interval -1 MONTH), '%y-%m') as Month, sum(amount/100) as LeaveRevenue from driverPaymentTransactions
group by Month)
, Sxn as(
Select DATE_FORMAT(DATE_ADD(createdAt, interval 330 MINUTE ), '%y-%m') as Month, sum(amount/100) as SubscribedRevenue from subscribedDriversDailyRevenues
group by MONTH)
Select * from Txn t
join Leaves l on t.Month = l.month
join Sxn s on t.month = s.month)
Do not join when you need to "pull" some different measures to the common attribute. Use union all, where you do not need to care about the most complete source of group values:
create table t1 as
select 1 as id, 10 as val union all
select 1, 20 union all
select 2, 30 union all
select 3, 49
create table t2 as
select 1 as id, 10 as val union all
select 3, 20 union all
select 3, 30 union all
select 5, 49
create table t3 as
select 4 as id, 10 as val union all
select 6, 20 union all
select 2, 30 union all
select 3, 49
with u as (
select
id
, val as t1_val
, cast(null as decimal) as t2_val
, cast(null as decimal) as t3_val
from t1
union all
select
id
, null as t1_val
, val as t2_val
, null as t3_val
from t2
union all
select
id
, null as t1_val
, null as t2_val
, val as t3_val
from t3
)
select
id
, sum(t1_val) as t1_val
, sum(t2_val) as t2_val
, sum(t3_val) as t3_val
from u
group by id
id | t1_val | t2_val | t3_val
-: | -----: | -----: | -----:
1 | 30 | 10 | null
2 | 30 | null | 30
3 | 49 | 50 | 49
5 | null | 49 | null
4 | null | null | 10
6 | null | null | 20
db<>fiddle here
I am looking to build a query that will give me the number of orders grouped by the quantity made plus I would like the revenue for those numbers.
So for example.
| Number of Orders | Numbers of Customers | Revenue Of Orders |
| 1 | 312 | 4350.88 |
| 2 | 208 | 3490.00 |
| 3 | 152 | 2240.50 |
I have got the first two columns working correctly.. This is that query
SELECT
r.num_of_orders ,
count(*) AS num_of_customers
FROM
(
SELECT
count(*) AS num_of_orders
FROM
reservations r
WHERE
created_at >= '2008-01-01 00:00:00'
AND `status` = 'closed'
GROUP BY
r.customer_id
) r
GROUP BY
r.num_of_orders
Trying to add revenue I have tried.
SELECT
r.num_of_orders ,
count(*) AS num_of_customers,
sum(b.total) as total_revenue
FROM
(
SELECT
count(*) AS num_of_orders
FROM
reservations r
WHERE
created_at >= '2008-01-01 00:00:00'
GROUP BY
r.customer_id
) r,
(
SELECT
sum(payments.total) AS total
FROM
reservations r
JOIN payments ON payments.id = r.reservation_id
WHERE
r.created_at >= '2008-01-01 00:00:00'
GROUP BY
r.customer_id
) b
GROUP BY
r.num_of_orders
But I know these numbers for revenue are out..
Hope you can advise.
Add it in the original subquery:
SELECT r.num_of_orders, count(*) AS num_of_customers,
SUM(revenue) as total_revenue
FROM (SELECT COUNT(DISTINCT r.reservation_id) AS num_of_orders,
SUM(p.total) as revenue
FROM reservations r JOIN
payments p
ON p.id = r.reservation_id
WHERE r.created_at >= '2008-01-01' AND
r.status = 'closed'
GROUP BY r.customer_id
) r
GROUP BY r.num_of_orders;
I have a table logins with the following schema:
| id | user_id | weekday |
|----|---------|---------|
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 2 |
...
Weekday is a number from 0 to 6.
I want to get which weekday has the highest count, for each user_id in the table.
I tried the following query:
SELECT MAX(num) as max_num, user_id, weekday
FROM (
SELECT COUNT(*) as num, user_id, weekday
FROM logins
GROUP BY user_id, weekday
) C
WHERE user_id = C.user_id AND num = C.num
GROUP BY user_id;
Which gets me weekday = 1 instead of 2. I think that I shouldn't use a WHERE clause here, but I couldn't manage to get the correct result.
I've checked other similar questions with no luck, such as:
MYSQL, Max,Group by and Max
Select first row in each GROUP BY group?
I created a SQL Fiddle with my example: http://sqlfiddle.com/#!9/e43a71/1
Here is a method:
SELECT user_id, MAX(num) as max_num,
SUBSTRING_INDEX(GROUP_CONCAT(weekday ORDER BY num DESC), ',', 1) as weekday_max
FROM (SELECT user_id, weekday, COUNT(*) as num
FROM logins l
GROUP BY user_id, weekday
) uw
GROUP BY user_id;
SELECT days.user_id, days.weekday, days.num
FROM (
SELECT user_id, MAX(num) AS num
FROM (
SELECT user_id, weekday, COUNT(*) AS num
FROM logins
GROUP BY user_id, weekday
) max
GROUP BY user_id
) nums
JOIN (
SELECT user_id, weekday, COUNT(*) as num
FROM logins
GROUP BY user_id, weekday
) days ON(days.user_id = nums.user_id AND days.num = nums.num);
-- With Mariadb 10.2 or MySQL 8.0.2
WITH days AS (
SELECT user_id, weekday, COUNT(*) as num
FROM logins
GROUP BY user_id, weekday
)
SELECT days.user_id, days.weekday, days.num
FROM (
SELECT user_id, MAX(num) AS num
FROM days
GROUP BY user_id
) nums
JOIN days ON(days.user_id = nums.user_id AND days.num = nums.num);
I'm calculating
the number of days a reservation took place over every month (for every month since the first record)
A total price based on the total # of days and rate.
INSERT INTO `reservations`
(`id`, `user_id`, `property_id`, `actual_check_in`,`actual_check_out`)
VALUES
(5148, 1, 2, '2014-01-01', '2014-01-10'),
(5149, 1, 2, '2014-02-03', '2014-02-10'),
(5151, 1, 2, '2014-02-02', '2014-02-15'),
(5153, 1, 2, '2014-03-05', '2014-03-10'),
(5153, 1, 2, '2014-02-20', '2014-03-30'),
SELECT
YEAR(month.d),
MONTHNAME(month.d),
r.property_id,
SUM(
DATEDIFF(LEAST(actual_check_out, LAST_DAY(month.d)), GREATEST(actual_check_in, month.d))
) AS days,
SUM(days*p.rate),
MIN(r.actual_check_in) as firstDate,
MAX(r.actual_check_out) as lastDate
FROM reservations as r
LEFT JOIN property as p on r.property_id=p.id
RIGHT JOIN (
select
DATE_FORMAT(m1, '%Y%m%d') as d
from
(
select
(firstDate - INTERVAL DAYOFMONTH(firstDate)-1 DAY)
+INTERVAL m MONTH as m1
from
(
select #rownum:=#rownum+1 as m from
(select 1 union select 2 union select 3 union select 4) t1,
(select 1 union select 2 union select 3 union select 4) t2,
(select 1 union select 2 union select 3 union select 4) t3,
(select 1 union select 2 union select 3 union select 4) t4,
(select #rownum:=-1) t0
) d1
) d2
where m1<=lastDate
order by m1
) AS month ON
actual_check_in <= LAST_DAY(month.d)
AND month.d <= actual_check_out
GROUP BY user_id, month.d
Troubles I'm having:
getting MySQL to accept a variable for firstDate & lastDate in the joined query
I want to sum the monthly number of days together, for reservations by the same user, for the same month. I'm trying to turn the proper parts into a subquery to calculate that but having trouble..
http://sqlfiddle.com/#!9/71e34/1
I would like to have the results like (if the property rate is 150/day):
DATE | USER | #Days | Total Rate
--------------------------------------
01/2014 | 1 | 9 | 1350
01/2014 | 2 | 0 | 0
02/2014 | 1 | 30 | 4500
02/2014 | 2 | 0 | 0
03/2014 | 1 | 35 | 5250
03/2014 | 2 | 0 | 0
04/2014 | 1 | 0 | 0
04/2014 | 2 | 0 | 0
* # days can be more than the # of days in a month because there might be multiple reservations existing during that month
UPDATE---- This almost solved the problem, but I'm having trouble in the second large select statement to actually calculate the prices properly. The query is only taking in to account the first property rate, and not selecting them as per the join statement. Any help?
select
r.user_id,
DATE_FORMAT(m1, '%b %Y') as date,
(SELECT
SUM(
DATEDIFF(LEAST(actual_check_out, LAST_DAY(m1)), GREATEST(actual_check_in, m1))
) AS numdays
FROM reservations
where actual_check_in <= LAST_DAY(m1)
AND m1 <= actual_check_out
AND user_id=r.user_id
GROUP BY m1) as days,
(SELECT
SUM(
DATEDIFF(LEAST(r.actual_check_out, LAST_DAY(m1)), GREATEST(r.actual_check_in, m1))
) *p.rate
FROM reservations as r
left join property as p
on r.property_id=p.id
where actual_check_in <= LAST_DAY(m1)
AND m1 <= actual_check_out
AND user_id=r.user_id
GROUP BY m1) as price
from (
select ('2015-01-01' - INTERVAL DAYOFMONTH('2015-01-01')-1 DAY) +INTERVAL m MONTH as m1 from (
select #rownum:=#rownum+1 as m from
(select 1 union select 2 union select 3 union select 4) t1,
(select 1 union select 2 union select 3 union select 4) t2,
(select 1 union select 2 union select 3 union select 4) t3,
(select 1 union select 2 union select 3 union select 4) t4,
(select #rownum:=-1) t0
) d1
) d2
cross join reservations as r
where m1<=CURDATE() group by user_id, m1 order by m1
http://sqlfiddle.com/#!9/36035/21
Still not sure of your request, but the query below may point you to right direction:
SELECT DATE_FORMAT(r.actual_check_in, '%m/%Y') AS mnth, r.user_id,
DATEDIFF(MAX(r.actual_check_out),MIN(r.actual_check_in)) AS days,
DATEDIFF(MAX(r.actual_check_out),MIN(r.actual_check_in))*p.rate AS totalRate
FROM reservations r
JOIN property p ON r.property_id=p.id
GROUP BY DATE_FORMAT(r.actual_check_in, '%m/%Y'), r.user_id;
This returns data like below:
mnth user_id days totalRate
------- ------- ------ -----------
01/2014 1 9 1350
02/2014 1 56 8400
03/2014 1 5 750
http://sqlfiddle.com/#!9/36035/36
select
r.user_id as userId,
DATE_FORMAT(m1, '%b %Y') as date,
(SELECT
SUM(
DATEDIFF(LEAST(actual_check_out, LAST_DAY(m1)), GREATEST(actual_check_in, m1))
) AS numdays
FROM reservations
where actual_check_in <= LAST_DAY(m1)
AND m1 <= actual_check_out
AND user_id=userId
GROUP BY m1) as days,
(SELECT
sum(DATEDIFF(LEAST(r.actual_check_out, LAST_DAY(m1)), GREATEST(r.actual_check_in, m1))*p.rate)
FROM reservations as r
left join property as p
on r.property_id=p.id
where r.actual_check_in <= LAST_DAY(m1)
AND m1 <= r.actual_check_out
AND r.user_id=userId
GROUP BY m1) as price
from (
select ('2015-01-01' - INTERVAL DAYOFMONTH('2015-01-01')-1 DAY) +INTERVAL m MONTH as m1 from (
select #rownum:=#rownum+1 as m from
(select 1 union select 2 union select 3 union select 4) t1,
(select 1 union select 2 union select 3 union select 4) t2,
(select 1 union select 2 union select 3 union select 4) t3,
(select 1 union select 2 union select 3 union select 4) t4,
(select #rownum:=-1) t0
) d1
) d2
cross join reservations as r
where m1<=CURDATE() group by user_id, m1 order by m1
Using SQL Server 2008: I am trying to write a report of customer transactions which have occurred in the past week and which are above $1000. I have the following SQL query which will give me the correct records, but obviously aggregates the results:
SELECT
customerID,
CAST(createdAt AS DATE) AS transactionDate,
SUM(transactionAmount) as dailyTotal,
FROM transactions
WHERE createdAt > DATEADD( DAY, -7, GETDATE() )
GROUP BY clientID, CAST(createdAt AS DATE)
HAVING SUM(transactionAmount) > 1000
Resulting in something like:
| customerID | transactionDate | dailyTotal |
| 1 | 2013-11-01 | 1212 |
| 2 | 2013-11-01 | 10002 |
...
| 1 | 2013-11-02 | 5212 |
However, I need to get the individual records which comprise these aggregated results, but obviously cannot omit the GROUP BY statement. Perhaps what I'm trying to achieve is not possible in a single query?
Do you want something like this?
SELECT t.customerID,
t.createdAt,
t.transactionAmount
FROM transactions t
INNER JOIN (
SELECT
customerID,
FROM transactions
WHERE createdAt > DATEADD( DAY, -7, GETDATE() )
GROUP BY clientID, CAST(createdAt AS DATE)
HAVING SUM(transactionAmount) > 1000
) a
ON a.customerID = t.customerID
WHERE t.createdAt > DATEADD( DAY, -7, GETDATE() )
Have you tried using a CTE with a SUM and an OVER() clause?
;WITH SalesAgg AS
(
SELECT
customerID,
TransactionDate = CAST(createdAt AS DATE),
TransactionAmount,
TotalSum = SUM(transactionAmount) OVER(PARTITION BY ClientID, CAST(CreatedAt AS DATE) ORDER BY ClientID)
FROM transactions
WHERE createdAt > DATEADD( DAY, -7, GETDATE())
)
SELECT
CustomerID, TransactionDate, TransactionAmount, TotalSum
FROM
SalesAgg
WHERE
TotalSum > 1000.0