Add a column of Averages in Access - sql

I have written the below query to show total calls (from phone system) coming in for individual days of the week. What I now need is to show the average of the total calls coming in for these days.
My query look like this:
SELECT WeekdayName(Weekday([Calls Volume].Weekday),False,1) AS [Day of the week], Sum([Calls Volume].Offered) AS [Calls Offered]
FROM [Calls Volume]
GROUP BY WeekdayName(Weekday([Calls Volume].Weekday),False,1), [Calls Volume].Weekday
ORDER BY [Calls Volume].Weekday;
Current Output:
Day of the week | Calls Offered
--------------------------------
Monday | 100
Tuesday | 200
Wednesday | 300
Desired output:
Day of the week | Calls Offered | Average Calls for the day of the week
-------------------------------------------------------------------------
Monday | 100 | 30
Tuesday | 200 | 20
Wednesday | 300 | 15
Thanks
Sample data:
DateTime | Calls Offered
------------------------
1/08/2014 9AM | 12
1/08/2014 10AM | 10
2/08/2014 9AM | 9
2/08/2014 11AM | 12
and so forth..

Related

Calculate average (AVG) including missing data in a date range in Redshift DB

Consider I have the following set of data
s_date | sales
------------+-------
2020-08-04 | 10
2020-08-05 | 20
2020-08-07 | 10
2020-08-08 | 20
2020-08-09 | 10
2020-08-10 | 30
2020-08-11 | 20
2020-08-12 | 10
I want to calculate the average for 'Sales' for each week. Please note that the data is missing for the date 2020-08-03 and 2020-08-06 for week starting on 2020-08-03.
When I am calculating weekly average using the AVG() function, it calculates the average based on the available records.
Following is the query that I am trying and the response that I am getting from it.
select trunc(date_trunc('WEEK', s_date)::timestamp) as week, avg(sales)
from test_temp.sales group by week;
Result:
week | avg
------------+-----
2020-08-03 | 14
2020-08-10 | 20
However I want to calculate the average considering 0 as the value for missing dates. So the average calculation should be done with the following values.
s_date | sales
------------+-------
2020-08-03 | 0
2020-08-04 | 10
2020-08-05 | 20
2020-08-06 | 0
2020-08-07 | 10
2020-08-08 | 20
2020-08-09 | 10
2020-08-10 | 30
2020-08-11 | 20
2020-08-12 | 10
Expected Result:
week | avg
------------+-----
2020-08-03 | 10 // Expected value
2020-08-10 | 20
Can anyone please let me know how to can we calculate the average as expected?
Regards,
Paul
Take the sum and divide by 7:
select trunc(date_trunc('WEEK', s_date)::timestamp) as week, sum(sales) / 7
from test_temp.sales
group by week;
EDIT:
To handle the last week, you can do something like this:
select trunc(date_trunc('WEEK', s_date)::timestamp) as week,
sum(sales) / least(7, current_date - trunc(date_trunc('WEEK', s_date)::timestamp))
from test_temp.sales
group by week;

Oracle SQL count days in week for every month

I have these two tables (times and sales):
times
TIME_ID | DAY_NAME | DAY_NUMBER_IN_WEEK | CALENDAR_MONTH_NAME | CALENDAR_MONTH_ID
1998-01-10| Monday | 1 | January | 1684
1998-01-10| Tuesday | 2 | January | 1684
1998-01-10| Wednesday | 3 | January | 1684
...
1998-01-11| Monday | 1 | February | 1685
1998-01-11| Tuesday | 2 | January | 1685
1998-01-11| Wednesday | 3 | January | 1685
sales
PROD_ID | TIME_ID | AMOUNT_SOLD
13 | 1998-01-10 | 1232
13 | 1998-01-11 | 1233
14 | 1998-01-11 | 1233
I need to make columns for every day in week (Monday, Tuesday, Wednesday...) and SUM of AMOUNT_SOLD for each PROD_ID for each day for each month.
SELECT SUM(times.day_number_in_week), times.calendar_month_name, times.day_name, times.calendar_year
FROM sales
INNER JOIN times ON times.time_id = sales.time_id
GROUP BY times.calendar_month_number, times.calendar_month_name, times.day_name, times.calendar_year
Output:
5988 March Wednesday 1998
9408 April Thursday 1998
7532 June Sunday 1998
9220 July Thursday 1998
7490 July Sunday 1998
12540 August Saturday 1998
but this sum all Wednesdays for all years, i need sum of amount for 1 month for all days (Wednesday, Monday...) for one month.
Can you help me?
You can do conditional aggregation:
SELECT
t.calendar_year
t.calendar_month_name,
SUM(case when t.day_number_in_week = 1 then s.amount_sold else 0 end) amount_sold_mon,
SUM(case when t.day_number_in_week = 2 then s.amount_sold else 0 end) amount_sold_tue,
SUM(case when t.day_number_in_week = 3 then s.amount_sold else 0 end) amount_sold_wed,
...
FROM sales s
INNER JOIN times t ON t.time_id = sales.time_id
GROUP BY t.calendar_year, t.calendar_month_number, t.calendar_month_name

how to get data of this week day wise in SQL SERVER

I have data entry table(ServiceData) with users data.Columns are Userid,BillDate (type DateTime)and etc.
I need to fetch count of users weekly (current week).
Query
select BillDate,DATENAME(dw,BillDate) as day ,count(BillDate) as total
from ServiceData
group by BillDate,DATENAME(dw,BillDate)
order by BillDate desc
this only fetches the day of billdate, but i want the count of sunday entries , count of monday entries - from sunday to saturday (current weeek). Is this possible?
expected Output
ID | TOTAL | DAY
--------------------------
1 | 23 | Sun
2 | 54 | Mon
3 | 17 | Tues
4 | 56 | Thus
5 | 45 | Fri
6 | 78 | Sat

how to display a weekday(set it to zero) of datepart function when there are no occurence of the day in the table

Sat Naam
I have a table in my databse that register of sales and stores. some stores are close on sundays so there are no sales on that day
i have the following query code to get the number of sale
SELECT DATENAME(WEEKDAY, [Data_Hora_Ficheiro_fim]),[Store_id],count([sales_no])
FROM [GPOS].[dbo].[V_Period_stats_with_CAE_GC]
group by [Store_id],[Store name],DATEPART(WEEKDAY,[Data_Hora_Ficheiro_fim])DATENAME(WEEKDAY, [Data_Hora_Ficheiro_fim])
order by DATEPART(WEEKDAY, [Data_Hora_Ficheiro_fim]),DATENAME(WEEKDAY, [Data_Hora_Ficheiro_fim]),[Store_id]
of the query would be something like(store 4 close on sunday)
day Storeid num_sales
----------|--------|----------
Monday 3 90
Tuesday 3 70
Wednesday 3 20
Thursday 3 60
Friday 3 96
Saturday 3 98
Sunday 3 200
Monday 4 90
Tuesday 4 70
Wednesday 4 20
Thursday 4 60
Friday 4 96
Saturday 4 98
so what changes do i need to do to my query in order to the output also include the line
Sunday 4 0
thanks in advance
One way would be to cross join the weekdays and distinct store_id to get all combinations of the two, and left join your view.
Using a common table expression for the first part and a Table Value Constructor (Transact-SQL)
;with Weekdays as (
select
day=datename(weekday, convert(datetime,t.n))
, s.Store_id
from (values(0),(1),(2),(3),(4),(5),(6)) t(n)
cross join (
select distinct Store_id
from [gpos].[dbo].[V_Period_stats_with_cae_gc]
) s
)
select
w.day
, w.[Store_id]
, count(v.[sales_no])
from Weekdays w
left join [gpos].[dbo].[V_Period_stats_with_cae_gc] v
on w.day = datename(weekday, v.[Data_Hora_Ficheiro_fim])
and w.Store_id = v.Store_id
group by
w.day
w.Store_id
order by
w.day
, w.Store_id
rextester demo (modified for provided example data): http://rextester.com/JZLF76266
returns:
+-----------+---------+-----------+
| day | storeid | num_sales |
+-----------+---------+-----------+
| Monday | 3 | 90 |
| Tuesday | 3 | 70 |
| Wednesday | 3 | 20 |
| Thursday | 3 | 60 |
| Friday | 3 | 96 |
| Saturday | 3 | 98 |
| Sunday | 3 | 200 |
| Monday | 4 | 90 |
| Tuesday | 4 | 70 |
| Wednesday | 4 | 20 |
| Thursday | 4 | 60 |
| Friday | 4 | 96 |
| Saturday | 4 | 98 |
| Sunday | 4 | 0 |
+-----------+---------+-----------+
If you have a table where store_id is unique (e.g. dbo.Store) then that would be an alternative source for select distinct Store_id ....
Another option would be to use a calendar table restricted to the date range you are interested in cross joined with the Store_id. This would allow you to join on an actual date data type instead of the datename() function, which would could improve performance.
Number and Calendar table reference:
Generate a set or sequence without loops - 2 - Aaron Bertrand
The "Numbers" or "Tally" Table: What it is and how it replaces a loop - Jeff Moden
Creating a Date Table/Dimension in sql Server 2008 - David Stein
Calendar Tables - Why You Need One - David Stein
Creating a date dimension or calendar table in sql Server - Aaron Bertrand

SSRS creating subtotal based on condition

I'm using SSRS 2008 and currently doing this matrix table. My table will populate all the days in a month, from Monday to Sunday. What I need is to add a subtotal row everytime it passes Sunday to sum up the total amount for the week.
Amount
Monday 100
Tuesday 200
Wednesday 200
Thursday 100
Friday 100
Saturday 300
Sunday 100
**Week 1 Total 1100 <-----Add in this subtotal row each time after Sunday**
Monday 100
Tuesday 200
Add week number into query and group the matrix using this field.
Your dataset should look like:
WeekNo | Day | Amount
1 | Monday 100
1 | Tuesday 200
1 | Wednesday 200
1 | Thursday 100
1 | Friday 100
1 | Saturday 300
1 | Sunday 100
2 | Monday 100
2 | Tuesday 200
...
.
Then use WeekNo as grouping expression and display subtotals.