I have a table with columns day, month, year, total_payments. And I have to calculate total_payments according to days, months, years.
How can I calculate values?
I am thinking code like :
select
month, year, sum(total_payments)
from
webassignment.subscription_stats
group by
day;
select
month, year, sum(total_payments)
from
webassignment.subscription_stats
group by
month;
select
month, year, sum(total_payments)
from
webassignment.subscription_stats
group by
year;
but it will not return the correct answers. And I want to calculate total_payments daywise, monthwise, yearwise. Please help me to find values.
Sample input :
Day Month Year Total_payments
10 01 2008 10
10 01 2008 20
11 02 2008 10
10 03 2010 10
Output:
Daywise :
day month year total_payments
-----------------------------
10 01 2008 30
11 02 2008 10
10 03 2010 10
Same for month and yearwise
You can get a summary with Totals by Year, Month and Day using GROUP BY WITH ROLLUP:
SELECT
`Year`,
`Month`,
`Day`,
SUM(`Total_payments`) as `Totals`
FROM `webassignment`.`subscription_stats`
GROUP BY `Year`,`Month`,`Day` WITH ROLLUP;
If you want individual queries for Year, Month and Day:
By Year:
SELECT
`Year`,
SUM(`Total_payments`) as `Totals`
FROM `webassignment`.`subscription_stats`
GROUP BY `Year`
ORDER BY `Year;
By Month:
SELECT
`Year`,
`Month`,
SUM(`Total_payments`) as `Totals`
FROM `webassignment`.`subscription_stats`
WHERE `Year` = 2017
GROUP BY `Year`,`Month`
ORDER BY `Year`,`Month`;
By Day:
SELECT
`Year`,
`Month`,
`Day`,
SUM(`Total_payments`) as `Totals`
FROM `webassignment`.`subscription_stats`
GROUP BY `Year`,`Month`,`Day`
ORDER BY `Year`,`Month`,`Day`;
A way could be the use on an union for show the different result based on different group by level
select day, month,year,sum(total_payments)
from webassignment.subscription_stats group by day, month,year
union
select null, month,year,sum(total_payments)
from webassignment.subscription_stats group by month,year
union
select null, null ,year,sum(total_payments)
from webassignment.subscription_stats group by year
order by year, month, day
for the sample you provided should be enough
select day, month,year,sum(total_payments)
from webassignment.subscription_stats group by day, month,year
order by day, month,year
Related
I have copied this SQL query from another website and I'm not actually sure why it returns null values for the previous period and the month comparing it with. What should I adjust in the query to get the desired result?
SQL Query:
WITH monthly_metrics AS (
SELECT EXTRACT(year from day) as year,
EXTRACT(month from day) as month,
SUM(revenue) as revenue
FROM daily_metrics
GROUP BY 1,2
)
SELECT year AS current_year,
month AS current_month,
revenue AS revenue_current_month,
LAG(year,12) OVER ( ORDER BY year, month) AS previous_year,
LAG(month,12) OVER ( ORDER BY year, month) AS month_comparing_with,
LAG(revenue,12) OVER ( ORDER BY year, month) AS revenue_12_months_ago,
revenue - LAG(revenue,12) OVER (ORDER BY year, month) AS month_to_month_difference
FROM monthly_metrics
ORDER BY 1,2;
Query Result:
dbfiddle
I want the count of records in every month and total count of records from start upto that month.
For ex.,
I have a table that looks like this:
#id,created#
1,'2016-01-01'
2,'2011-02-02'
3,'2011-02-09'
4,'2011-02-05'
5,'2011-03-07'
6,'2011-03-08'
How do I select and group these so the output is:
#Month, new, total#
Jan 2016, 1, 1
Feb 2016, 3, 4
Mar 2016, 2, 6
Thanks very much.
Here you go:
SELECT DATE_FORMAT(`created`,'%M %Y') AS month, COUNT(*) AS count,
(SELECT count(*) FROM test WHERE MONTH(created) <= MONTH(t.created)) AS total
FROM test t
GROUP BY MONTH(created);
Here's the SQL Fiddle.
Using single table read:
SELECT
CONCAT(LEFT(MONTHNAME(dt), 3), ' ', YEAR(dt)) month,
new,
#total:=#total + new total
FROM
(SELECT
created - INTERVAL DAY(created) - 1 DAY dt, COUNT(*) new
FROM
t
GROUP BY created - INTERVAL DAY(created) - 1 DAY
ORDER BY dt) t
CROSS JOIN
(SELECT #total:=0) t2
Demo
SR_ AREA INS_PRODUCT DATEADD TOTAL
Clinical Question PS 2016-01-06 280
I'm trying to show the month by name say January and I want all the totals for the month of January not just for one day in the month.
I got it to show just Month and total how do I get it to show all the months this is the code I have so far.
SELECT DATENAME(MM,GETDATE()) AS MONTH, COUNT(*) AS TOTAL
FROM S_SRV_REQ WITH (NOLOCK)
WHERE (dbo.fn_dstoffset(CREATED) >= '11-15-2015')
AND (dbo.fn_dstoffset(CREATED) <= DATEADD(D, 1, '3-31-2016'))
AND (INS_PRODUCT IN ('PS'))
AND [SR_AREA] IS NOT NULL
AND (SR_AREA IN ('Clinical Question'))
select date_format(dateadd, '%M'), sum(total)
from your_table
group by date_format(dateadd, '%M')
You can use MONTH
select month(dateadd), sum(total)
from your_table
group by month(dateadd)
Currently, my codes here produces such results:
SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS MONTH, COUNT(*) AS TOTAL
FROM news
GROUP BY MONTH
UNION ALL
SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS MONTH , COUNT(*) AS TOTAL
FROM equipment
GROUP BY MONTH
RESULTS:
YEAR MONTH TOTAL
2013 FEB 1 (news table)
2013 JAN 12 (news table)
2013 FEB 1 (equipment table)
2013 JAN 11 (equipment table)
How do I edit the SQL query such that I will be able to only show:
RESULTS:
YEAR MONTH TOTAL
2013 FEB 2 (both news and equipment table)
2013 JAN 23 (both news and equipment table)
Thanks in advance for any help!
Try :
SELECT YEAR, MONTH, SUM(TOTAL) AS TOTAL
FROM (SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS MONTH, COUNT(*) AS TOTAL
FROM news
GROUP BY MONTH
UNION ALL
SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS MONTH , COUNT(*) AS TOTAL
FROM equipment
GROUP BY MONTH) x
GROUP BY YEAR, MONTH
try
Select year, month, sum(total) from
(
SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS MONTH, COUNT(*) AS TOTAL
FROM news
GROUP BY MONTH
UNION ALL
SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS MONTH , COUNT(*) AS TOTAL
FROM equipment
GROUP BY MONTH
)
group by year, month
select x.year, x.month, sum(x.total) from x(
SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS MONTH, COUNT(*) AS TOTAL
FROM news
GROUP BY MONTH
UNION ALL
SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS MONTH , COUNT(*) AS TOTAL
FROM equipment
GROUP BY MONTH ) x
group by x.month, x.year
Try this:
SELECT YEAR, MONTH, SUM(total) total
FROM (SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS MONTH, COUNT(*) AS TOTAL
FROM news
GROUP BY MONTH
UNION ALL
SELECT YEAR(date_added) AS YEAR, MONTHNAME(date_added) AS MONTH , COUNT(*) AS TOTAL
FROM equipment
GROUP BY MONTH
) AS A
GROUP BY YEAR, MONTH
SELECT
Day,
month,
year,
GROUP_CONCAT(total),
GROUP_CONCAT(SP_ID)
FROM
(
SELECT
DATE_FORMAT(l.act_date, '%d') AS DAY,
DATE_FORMAT(l.act_date, '%M') AS MONTH,
EXTRACT(YEAR FROM l.act_date) AS YEAR,
COUNT(*) as total,l.sp_id
FROM lead_activity2 as l
right outer join salesperson as s on l.sp_id=s.sp_id
WHERE l.act_name='scb'
AND ((l.act_date>='2012-09-07 13:03:27' )
AND (l.act_date<= '2012-11-07 13:03:27'))
GROUP BY MONTH, YEAR, DAY, l.sp_id
ORDER BY YEAR DESC, MONTH DESC, DAY DESC, l.sp_id DESC
) t GROUP BY day, month, year
http://sqlfiddle.com/#!2/1514d/3 - you can view the scheme and the query,
what i would like to get is
18 | october | 2012 | 0,0,1,1 | 6,5,4,3
spid 6 and spid 5 have no data for 18 october but still should be shown tried doing right join and right outer join both dont seem to work...
Use GROUP_CONCAT like so:
SELECT
Day,
month,
year,
GROUP_CONCAT(total),
GROUP_CONCAT(SP_ID)
FROM
(
SELECT
DATE_FORMAT(l.act_date, '%d') AS DAY,
DATE_FORMAT(l.act_date, '%M') AS MONTH,
EXTRACT(YEAR FROM l.act_date) AS YEAR,
COUNT(*) as total,l.sp_id
FROM lead_activity2 as l
WHERE l.act_name='scb'
AND ((l.act_date>='2012-09-07 13:03:27' )
AND (l.act_date<= '2012-11-07 13:03:27'))
GROUP BY MONTH, YEAR, DAY, l.sp_id
ORDER BY YEAR DESC, MONTH DESC, DAY DESC, l.sp_id DESC
) t GROUP BY day, month, year
Updated SQL Fiddle
Update: Yes you can do this, but use LEFT JOIN to include non matching sp_id. These non matching ids will have a value of NULL use IFNULL to display it with zeros like so:
SELECT
Day,
month,
year,
GROUP_CONCAT(total) Total,
GROUP_CONCAT(SP_ID) 'List of sp_ids'
FROM
(
SELECT
DATE_FORMAT(l.act_date, '%d') AS DAY,
DATE_FORMAT(l.act_date, '%M') AS MONTH,
EXTRACT(YEAR FROM l.act_date) AS YEAR,
COUNT(*) as total,
IFNULL(s.sp_id , 0) sp_id
FROM lead_activity2 as l
LEFT JOIN salesperson s ON l.sp_id = s.sp_id
WHERE l.act_name='scb'
AND ((l.act_date>='2012-09-07 13:03:27' )
AND (l.act_date<= '2012-11-07 13:03:27'))
GROUP BY MONTH, YEAR,DAY,s.sp_id
) t
ORDER BY YEAR DESC,
MONTH DESC,
DAY DESC,
sp_id DESC
Updates SQL Fiddle Demo