I need to find average sales by month of the customer. For example one customer bought in Jan2020 some products... then in Feb... so jan becomes 1st month and feb 2nd for the customer.
Similarly other customer buys 1st time in Apr'20 and next time in June'20..... so avg sales for 1st Mon(Apr) and 2nd month (Avg of Apr and Jun)
Expected outcome:
CustID Month Avg_sales
You can use window functions to get the first date. And then arithmetic. Here is one method:
select custid, year(date) * 12 + month(date) - (year(first_date) * 12 + month(first_date)) as diff,
avg(sales) as avg_sales
from (select t.*,
min(date) over (partition by custid) as first_date
from t
) t
group by custid, diff
Related
I have a table transactions like this
date
amount
2020-02-26
1000
2020-02-26
1500
2021-01-11
200
I want to select the sum of all transactions per month. The result should look something like...
month
sum
2020-02
2500
2021-01
200
This is what I've got so far
select sum(amount) sum, MONTH(date) month from transactions group by month;
However this groups by month ignoring year (the values returned for month are single integers). I need to group by each individual month (return a different row for Jan 2020 vs Jan 2021)
here is one way:
select sum(amount) sum, extract(YEAR_MONTH FROM `date`) month
from transactions
group by month;
I have a table that consists of the following data, I would like to know whether it is possible to get a Month (i.e Jan, Feb) wise count of Reservations that happened and also Month wise count for each location.
PNR
Location
Reservation Date
Passenger Name
Travel Date
PNR81239087
Mumbai
2019-10-01 12:19:00
Ram
2019-11-06 15:59:00
PNR81239090
Kerala
2019-10-01 15:18:00
Kannan
2019-12-03 19:18:00
PNR812390199
Mumbai
2019-10-01 17:19:00
Ram
2019-11-01 18:39:00
For example,
Month Wise Count (including all locations) should look something like this,
Month
Count
October-2019
3
Monthwise count for each location:
Month
Count
Location
October-2019
2
Mumbai
October-2019
1
Kerala
I think this will work for you
Month Wise Count (including all locations) :
select MONTHNAME(Reservation_Date) as Month, count(*)
from yourTable
group by MONTHNAME(Reservation_Date)
Monthwise count for each location :
select MONTHNAME(Reservation_Date) as Month, count(*), Location
from yourTable
group by MONTHNAME(Reservation_Date), Location
EDIT IN QUESTION :
Changed to Group by Year and Month in one column:
Month Wise Count (including all locations) :
select concat(MONTHNAME(Reservation_Date),'-',Year(Reservation_Date)) as Month-Year,
count(*) as Count
from yourTable
group by concat(MONTHNAME(Reservation_Date),'-',Year(Reservation_Date))
Monthwise count for each location :
select concat(MONTHNAME(Reservation_Date),'-',Year(Reservation_Date)) as Month-Year,
count(*) as Count, Location
from yourTable
group by concat(MONTHNAME(Reservation_Date),'-',Year(Reservation_Date)), Location
You can use this :
/*Location-wise*/
SELECT DATENAME(MONTH, ReservationDate) as [Month],
COUNT(*) AS Count,
Location
FROM Temp
GROUP BY DATENAME(MONTH, ReservationDate), Location
ORDER BY Location DESC
/*All locations*/
SELECT DATENAME(MONTH, ReservationDate) as [Month],
COUNT(*) AS Count
FROM Temp
GROUP BY DATENAME(MONTH, ReservationDate)
You can see this working here : All locations and Locations-wise
I am calculating weighted formula for a field as sum(revenue)/ Sum(qty) and this is as per the below query. Now I will be creating a view that would store these results as I shown in code below.
My question is, if I select this w_revenue out of the view and want to see per year, how will I aggregate to show it per year? See desired outputs.
select month,item, sum(revenue)/ Sum(qty) as w_revenue,
year
from my_revenue_table
group by month, year,item;
create view xyz as
select month,item, sum(revenue)/ Sum(qty) as w_revenue,
year
from my_revenue_table
group by month, year,item;
select w_revenue
from xyz.
How do I do this so as to aggregate this per year?
Year Month Item Revenue Qty Sum(revenue)/Sum(Qty)
2019 Mar A 10 2 5
2019 Mar B 30 3 10
2019 Feb C 50 1 50
2019 Feb D 20 2 10
Expected value if I see per year:
Year Sum(revenue)/Sum(Qty)
2019 13.75 (10+30+50+20)/(2+3+1+2)
group on by year then create another view as
create view abc as
select sum(revenue)/ Sum(qty) as w_revenue, year
from my_revenue_table
group by year;
and call
select * from xyz union all
select null, null, a.* from abc a
to combine them, if I understood you correctly.
or revenue per year repeating at every row as
select x.*,
(select w_revenue
from abc
where year = x.year) as w_revenue_year
from xyz x
You cannot do the aggregation you want from the view. You don't have enough information.
If you change the view to something like this:
create view xyz as
select year, month, item,
sum(revenue)/ Sum(qty) as w_revenue,
sum(qty) as total_qty
from my_revenue_table
group by month, year, item;
Then you can aggregate the results as:
select year, sum(w_revenue * total_qty) / sum(total_qty)
from xyz
group by year;
EDIT:
Or just modify the view to have the information you want:
create view xyz as
select year, month, item,
sum(revenue)/ Sum(qty) as w_revenue,
sum(qty) as total_qty,
(sum(sum(revenue)) over (partition by year, item) /
sum(sum(qty)) over (partition by year, item)
) as w_revenue_year
from my_revenue_table
group by month, year, item;
I want to create two queries for my table which has fields name,surname and amount paid,the first query should select the day,month and the amount paid,the second query should select a month,year in that year and the total amount paid in that month,lets say john paid on 2013-05-01, on 2013-05-03,while peter paid on 2013-04-08, i want the first query to output
month and day amount
05-01 200
05-03 400
04-08 50
and the second query should output:
month and year total
2013-05 600
2013-04 50
I know I can use the sum aggregate function to select the total but the tricky part is how to select the day and the month in the format above,
first query will be
SELECT DATE_FORMAT(date, "%m-%d") AS 'month and day',price as amount FROM `tablename`
and second query will be
SELECT DATE_FORMAT(date, "%Y-%m") AS 'month and year' , SUM(price) AS total FROM `tablename` GROUP BY YEAR(date), MONTH(date)
Have a matrix report now that has
Position, Hours and Wages for a location for a month.
There may be any number of locations...that's why I'm starting with a Matrix report...the user may chose up to 50 locations to view.
Location 1 Location 2
Total Hrs Amount Total Hrs Amount
Position1 441.68 $4,201.46 556.73 $6,103.67
We would like to get an average by position for that month, like so
Location 1 Location 2 Avg
Total Hrs Amount Total Hrs Amount Avg Hrs
Position1 441.68 $4,201.46 556.73 $6,103.67 499.20
Can't quite figure out how to get this to work in SSRS 2005...
You could have a calculated field with formula (rs!localtion1Hrs + rs!location2hrs) / 2.
OR
The query used to show the report can have this field as calculated column.
Pretty sure I've solved this by using the following as the DataSet for the report and using a matrix in the report layout.
Basically I did what shahkalpesh said above..."The query used to show the report can have this field as calculated column."
select
'Avg' as LocationID,
'Avg' as Description,
AccountDesc,
#PayrollYear as Year,
#PayrollMonth as Month,
avg(s.TotalHrs) as TotalHrs,
avg(s.Amount) as Amount from
(
select LocationID, 'Avg' as Description, AccountDesc,
#PayrollYear as Year, #PayrollMonth as Month,
sum(TotalHrs) as TotalHrs,
sum(Amount) as Amount from vwPayroll
where LocationID in (select value from dbo.ParmsToList(#PayrollLocIds))
and Year = #PayrollYear and Month = #PayrollMonth
group by LocationID, AccountDesc, Year, Month
) as s
group by AccountDesc
union all
select
LocationID,
Description,
AccountDesc,
Year,
Month,
Sum(TotalHrs) as TotalHrs,
Sum(Amount) as Amount
from vwPayroll
where LocationID in (select value from dbo.ParmsToList(#PayrollLocIds))
and Year = #PayrollYear and Month = #PayrollMonth
group by LocationID, Description, AccountDesc, Year, Month