Aggregate totals for DATEDIFF() calculations - reporting-services

I am trying to get aggregate sums for days between certain dates. I have managed to use DATEDIFF to find the number of days, but cannot find a way to add them.
Here is what I would like the table to look like. Please note that dates are in UK format and days counted include both Start and End date. I also have removed weekends so that this only includes weekdays:
User | txtStartDate | txtEndDate | Days | Days Total
-------------------------------------------------------------------------
1 | 04/01/18 | 05/01/18 | 2 | 2
1 | 09/01/18 | 12/01/18 | 4 | 6
1 | 22/01/18 | 31/01/18 | 8 | 14
-------------------------------------------------------------------------
2 | 11/01/18 | 12/01/18 | 2 | 2
2 | 18/01/18 | 18/01/18 | 1 | 3
-------------------------------------------------------------------------
TOTAL | 17
I am using this code to calculate the days:
=DateDiff(DateInterval.day, Fields!txtStartDate.Value, Fields!txtEndDate.Value)+1)
-(DateDiff(DateInterval.WeekOfYear, Fields!txtStartDate.Value, Fields!txtEndDate.Value)*2)
and have tried putting this in =SUM() to calculate the total but this does not work.
Any help would be greatly appreciated.
Thanks
Rob

You need to use RunningValue
You might get a syntax error here
=RunningValue(DateDiff(DateInterval.day, Fields!txtStartDate.Value, Fields!txtEndDate.Value)+1)
-(DateDiff(DateInterval.WeekOfYear, Fields!txtStartDate.Value, Fields!txtEndDate.Value)*2)
,sum
,"DataSet1") --enter the scope here or `nothing`

Related

Mysql - Get season from current month

I have the following table of seasons:
| id | name | start_month | end_month |
------------------------------------------
| 101 | Summer | 12 | 2 |
| 102 | Winter | 6 | 8 |
| 103 | Spring | 9 | 11 |
| 104 | Fall | 3 | 5 |
I need to get the season by month. Say current month is 2 (February), I want Summer to be the output.
I can get other seasons to work by simply having the where condition start_month >= 4 and end_month <= 4. But this won't work with Summer since the season crosses into next year.
What do I have to do to handle the case of Summer?
One solution I thought was to use dates instead of month number like 1980-12-01 and use between function but it gets a bit complicated for the user end.
It'd be great if it could work with just month numbers.
You could do:
(month(d) between start_month and end_month) or
(start_month>end_month and (month(d)>=start_month or month(d)<=end_month))
See db-fiddle

SSRS aggregate data for days and for month within the same table

Supposing the following dataset:
- product
- date
- producedUnit
How can I realize the following tale:
Product | sum of Produced Unit this day | sum of produced Unit this Month
I don't want group record so if in the dataset there are 10 product I want to display them all.
As example:
Input dataset:
+--------+-------------+---------------+
|Product | date | produced unit |
+--------+-------------+---------------+
| M&Ms | 2018-02-08 | 5 |
+--------+-------------+---------------+
| M&Ms | 2018-02-08 | 2 |
+--------+-------------+---------------+
| M&Ms | 2018-02-28 | 3 |
+--------+-------------+---------------+
| Kit Kat| 2018-02-08 | 10 |
+--------+-------------+---------------+
| Kit Kat| 2018-02-28 | 10 |
+--------+-------------+---------------+
Today is 2018-02-08
Output:
+--------+-------------------------------+---------------------------------+
|Product | sum of Produced Unit this day | sum of produced Unit this Month |
+--------+-------------------------------+---------------------------------+
| M&Ms | 7 | 10 |
+--------+-------------------------------+---------------------------------+
| M&Ms | 7 | 10 |
+--------+-------------------------------+---------------------------------+
| M&Ms | 7 | 10 |
+--------+-------------------------------+---------------------------------+
|Kit Kat | 10 | 20 |
+--------+-------------------------------+---------------------------------+
|Kit Kat | 10 | 20 |
+--------+-------------------------------+---------------------------------+
Add a calculated field to your Dataset that gets the Month of the production date. For this example, I used =Month(Fields!Date.Value), but if you are crossing years, you may need to use the first day of each month as your common value.
Add a calculated field to get the number produced today. For example:
=IIf(Fields!Date.Value = "2/8/2018", Fields!Produced.Value, Nothing)
Create a new Table. Change the "Details" group to be grouped by Month. Add a parent row group that is grouped by Product.
The expression for "sum of Produced Unit this day" would be like this:
=Sum(Fields!Produced_Today.Value)
The expression for "sum of produced Unit this Month" would be like this:
=Sum(Fields!Produced.Value)
Here's a sample of the result:
Explanation: The table first groups by Product and then by month. You could hide the month and date columns if you don't want to show those. The amount produced today is being filtered at the Dataset level. The amount produced this month is being summed within the scope of the Month group.
Obviously, this is a trivial example, but you can take the principles shown here to account for more variables such as multiple months in your dataset.

Calculate the fullness of an apartment using SQL expression

I have a database which looks like this:
Reservations Table:
-------------------------------------------------
id | room_id | start | end |
1 | 1 | 2015-05-13 | 2015-05-16 |
2 | 1 | 2015-05-18 | 2015-05-20 |
3 | 1 | 2015-05-21 | 2015-05-24 |
-------------------------------------------------
Apartment Table:
---------------------------------------
id | room_id | name |
1 | 1 | test apartment |
---------------------------------------
Meaning that in the month 05 (May) there is 31 days in the database we have 3 events giving us 8 days of usage 31 - 8 = 23 / 31 = 0.741 * 100 = %74.1 is the percentage of the emptiness and %25.9 is the percentage of usage. how can i do all of that in SQL? (mySQL).
This is my proposal:
SELECT SUM(DAY(`end`)-DAY(`start`))/EXTRACT(DAY FROM LAST_DAY(`start`)) FROM `apt`;
LAST_DAY function gives as output the date of last day of the month.
Check this
http://sqlfiddle.com/#!9/7c53b/2/0
Not the most efficient query but will get the job done.
select
sum(a.days)*100/(SELECT DAY(LAST_DAY(min(start))) from test1)
as usePercent,
100-(sum(a.days)*100/(SELECT DAY(LAST_DAY(min(start))) from test1))
as emptyPercent
FROM
(select DATEDIFF(end,start) as days from test1) a
What I did is first get the date difference and count them. Then in a nested query use the day(last_day()) function to get the last day of month. Then calculated by using your logic.

Mysql query for counting days in some season by date range

So, Im trying to make a query that will count how many days belong in different seasons. Its for site that's about booking apartments and it can have multiple seasons one after another. For example:
| Season_id | From | Till |
| 1 | 2015-09-01 | 2015-09-30 |
| 2 | 2015-09-30 | 2015-10-20 |
| 3 | 2015-10-30 | 2015-12-30 |
So when someone is searching to book for date ex.:
2015-09-25 - 2015-10-10.
I want to get result like:
Seasons: 1, 2
Count days: 5, 10
Is it possible to do that with one query or it must be multiple??
Thank you in advance.

Returning multiple results from one row

I have a set of MySQL data similar to the following:
| id | type | start | end |
===============================================================
| 1 | event | 2011-11-01T00:00:00 | 2012-01-02T00:00:00 |
| 2 | showing | 2012-11-04T00:00:00 | 2012-11-04T00:00:00 |
| 3 | conference | 2012-12-01T00:00:00 | 2012-12-04T00:00:00 |
| 4 | event2 | 2012-01-01T00:00:00 | 2012-01-01T00:00:00 |
I want to retrieve events within a certain date range, but I also want to return individual results for each row that has a time span of more than one day. What's the best way to achieve this?
EDIT: In other words, I want to return two results from the event row, four results from the conference row and a single result for all the others.
Any ideas would be greatly appreciated.
Try this statement:
SELECT * FROM table
WHERE START BETWEEN '2012-01-01' AND '2012-01-03'
OR END BETWEEN '2012-01-01' AND '2012-01-03'
OR TO_DAYS(end) - TO_DAYS(start) > 1
I have created it for testing on SQL Fiddle