SSRS creating subtotal based on condition - sql

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.

Related

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

Add a column of Averages in Access

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..

Starting result set at certain point in results

I've got an issue I'm a little stuck with. Basically, I have a table with 4 columns, one column is a date and the results returned are dependent on time of day. If the time is before 6pm return the data as is but if after 6, find the next available day.
This is some sample data assuming today is 2014-07-10:
+----------+---------------------+-----------------+----------------+
| Workzone | ScheduleDate | NextDayDelivery | StdDayDelivery |
+----------+---------------------+-----------------+----------------+
| SWANS | 2014-07-11 00:00:00 | 1 | 1 |
| SWANS | 2014-07-12 00:00:00 | 0 | 1 |
| SWANS | 2014-07-13 00:00:00 | 0 | 0 |
| SWANS | 2014-07-14 00:00:00 | 0 | 1 |
| SWANS | 2014-07-15 00:00:00 | 0 | 1 |
| SWANS | 2014-07-16 00:00:00 | 0 | 1 |
| SWANS | 2014-07-17 00:00:00 | 0 | 1 |
| SWANS | 2014-07-18 00:00:00 | 0 | 1 |
| SWANS | 2014-07-19 00:00:00 | 0 | 1 |
| SWANS | 2014-07-20 00:00:00 | 0 | 0 |
+----------+---------------------+-----------------+----------------+
This is the query so far:
DECLARE #hour AS Int
SET #hour = DatePart(hour, getdate())
SET #hour = 19
-- Work out if the time is before 18:00. If it is then this is the easy bit, use the data as is.
IF (#hour <= 18)
BEGIN
SELECT
[Workzone]
,[ScheduleDate]
,[NextDayDelivery]
,[StdDayDelivery]
FROM [tbl_Sys_QuickstartDeliveryQuota]
END
ELSE
BEGIN
-- OTHER QUERY HERE --
END
So the above simply checks the hour and performs a query based on that. If before 6pm then simply view the data in the table as it is otherwise execute the other query which is what i'm stuck on.
The sample data above is generated for the next day onwards so if today is the 2014-07-10 then the data will start from the 2014-07-11. The NextDayDelivery column will always start with 1 if the following day is a weekday or saturday with all following days set to 0 and the StdDayDelivery column will have 1 if on a weekday or saturday or 0 if a sunday or bank holiday.
What I need to do is adjust the results if the time is after 6 as this will change the next day delivery.
My thoughts are to use the StdDayDelivery column to work out the next available delivery day so then in the sample data provided the 2014-07-12 will become the first row (if today is the 11th) of the results with NextDayDelivery being set to 1 based on the StdDayDelivery being set to 1. If todays date was the 2014-07-12 then the results will start from the 14th as the 13th is a Sunday so the next available delivery day is the 14th so again, NextDayDelivery is a 1 on the 14th.
I hope this is making sense.
The two expected outputs would be:
If today is the Friday the 11th, the results would be:
2014-07-12 1 1
2014-07-13 0 0
2014-07-14 0 1
2014-07-15 0 1
2014-07-16 0 1
2014-07-17 0 1
2014-07-18 0 1
If today is the Sat the 12th, the results would be:
2014-07-13 0 0
2014-07-14 1 1
2014-07-15 0 1
2014-07-16 0 1
2014-07-17 0 1
2014-07-18 0 1
(13th values set to 0 as StdDayDelivery is showing 0 because it's a sunday)
Looks like that CROSS APPLY may help you with this task.
Create table function for [NextDayDelivery] calculation
IF OBJECT_ID(N'dbo.GetData', N'TF') IS NOT NULL
DROP FUNCTION dbo.GetData;
GO
CREATE FUNCTION GetData(#ScheduleDate AS DATETIME, #testDay AS DATETIME)
RETURNS #VV TABLE
(
[NextDayDelivery] bit
)
AS
BEGIN
INSERT INTO #VV
SELECT
CASE
WHEN (DATEDIFF(dd, #testDay, #ScheduleDate)=1 AND DATEPART(dw, #ScheduleDate)>1) OR
(DATEDIFF(dd, #testDay, #ScheduleDate)=2 AND DATEPART(dw, #testDay)=7) THEN 1
ELSE 0
END
AS [NextDayDelivery]
RETURN
END
GO
Use this function in SELECT
DECLARE #testDay DATETIME
SET #testDay = DATEADD(d,1,GETUTCDATE())
-- SET #testDay = GETUTCDATE()
SELECT t.[ScheduleDate], VV.[NextDayDelivery], t.StdDayDelivery
FROM [tbl_Sys_QuickstartDeliveryQuota] AS t
CROSS APPLY GetData(t.[ScheduleDate], #testDay) AS VV
where t.[ScheduleDate] > #testDay
Note: My code supposes that first day of week is Sunday. I.e. SELECT DATEPART(dw, '2014-07-12') returns me 7. Correct my code if you use other culture.