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
Related
i tried this query
select id, year(created_at) as year,month(created_at) as month,sum(grandtotal) as monthtotalsale
from bookings
WHERE YEAR(created_at)=YEAR(CURRENT_DATE) group by month(created_at)
order by id, year(created_at),month(created_at)
but this query return the total sale of each month
This would give you the three month with the highest sale monthly for this year
SELECT
id,
YEAR(created_at) AS year,
MONTH(created_at) AS month,
SUM(grandtotal) AS monthtotalsale
FROM
bookings
WHERE
YEAR(created_at) = YEAR(CURRENT_DATE)
GROUP BY MONTH(created_at)
ORDER BY monthtotalsale DESC
LIMIT 3
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
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)
I have a subscription table that tracks membership renewals by date. Renewals are for calendar year but members are allowed to renew as early as Oct 1, in which case they would be current until Dec. 31 of the following year . Therefore it is possible to renew in January and then renew again in October of the same year. I'm reporting total memberships by month and I want to avoid counting that as 2 memberships.
Each record has a unique prodID but can have more than 1 record of a memberID due to the renewal option above. payDate is the transaction date
My statement is:
$sql = "SELECT
EXTRACT(MONTH FROM payDate) as month,
EXTRACT(YEAR FROM payDate) as year,
count(*)
FROM
memberDues
WHERE payDate >= $lastYear-10-01
GROUP BY
month,
year
ORDER BY
year ASC,
month ASC";
I get an output like this (not formatted):
Member dues paid by month
October 46
November 30
December 99
January 42
February 8
March 9
April 4
May 1
June 3
Member Total: 242
How do I modify the select statement to avoid duplicate renewals in a report period?
you need to group by memberID to get one member renewal not being repeated.
SELECT year, month, count(memberID) FROM (
SELECT memberID,
EXTRACT(MONTH FROM payDate) as month,
EXTRACT(YEAR FROM payDate) as year,
count(*)
FROM
memberDues
WHERE payDate >= $lastYear-10-01
GROUP BY
memberID,
month,
year
ORDER BY
year ASC,
month ASC";
)TMP GROUP BY year, month
Counting the distinct memberID's for each month should do the job:
SELECT
EXTRACT(MONTH FROM payDate) as month,
EXTRACT(YEAR FROM payDate) as year,
count(DISTINCT memberID)
FROM
memberDues
WHERE payDate >= $lastYear-10-01
GROUP BY
month,
year
ORDER BY
year ASC,
month ASC
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