How to aggregate list of dates and generate date range in SQL Server? - sql-server-2012

I have a list of date which I want to aggregate and generate a date range.
I have attached the result set I have and the result I want to.
Help me with this.
Thanks in advance.
Screenshot you can refer to

The logic for this can be complicated. This article has it right. Once you have a table that defines your buckets, consider if the bucket spans the front of a date range is in the middel or overlaps the end of the date range.
https://www.mssqltips.com/sqlservertip/5854/using-tsql-to-find-events-that-overlap-or-dont-in-sql-server/

Related

Apple Numbers sum if is date is in Month

In Apple numbers I tried to sum cells if the date is in a specific month.
So I have a column A with a date and column B has a price.
Date
Price
29-07-2021
12,50
16-06-2021
15,00
I use the function sum.if and the function month that returns the number of the month.
My formule look like this
som.if(month(Date));7;Price);
But this generates an error numbers can only check one date and not for each row for counting.
Is there anybody who can help me with this?
Thanks a lot!
Use SUMPRODUCT() instead. I am using US-EN version of excel. So, decimal separator and formula definition may differ.
=SUMPRODUCT((B2:B3)*(MONTH(A2:A3)=7))
If you want to use SUNIFS() then can try-
=SUMIFS(B2:B3,A2:A3,">="&DATE(2021,7,1),A2:A3,"<="&DATE(2021,7,31))
If you have Excel-365 then can use FILTER() function like-
=SUM(FILTER(B2:B3,MONTH(A2:A3)=7))

how to group rows based on a dates in a single column

I have a range of date (A1:CY7026) with a column for start dates. this column has a large amount of repeated dates within it. i need these dates group together based on the working week they are located in (eg. all values reading 16/7/18 - 22/07/18 would be one group and the following week would make up another).
Use below in B2 cell (Suppose you had a header column) and drag to the rest. Sort to obtain the desired result.
=CONCATENATE(YEAR(A2),"-",WEEKNUM(A2))

Formatting Day Time X Axis in SSRS Charts

I am working on a SSRS report which compares data of 2 given months. I'm categorising the chart based on Day and time in the following format 1, 6:00 AM. I get this column from the T-SQL itself. But the axis does not come properly. It looks like below now which doesn't make sense. I want it to be in order from 1st to 30th with the time component.
I guess I need some kind of sorting on X-axis with respect to date time. Please help!
After deleting from sorts from chart I'm getting some extra repitive dates after comparing all 30 days of both months. Data from the query looks alright to me!
Thank you!
Ok. Large query
You X-axis are show value in format date,hh mm tt Right ?
Then you want to sort them with day number 1 - 30.
From your query I suggest you add 1 field is like this CAST(SampleCollected AS DATE) [orders] and use this field in Order in Query or Sort on SSRS (not recommend ) and if you use Order in Query must delete sort condition on chart sort.
But if result still not you want try to add MONTH(SampleCollected) As MonthG to order again like this
ORDER BY MONTH(SampleCollected),CAST(SampleCollected AS DATE)
Hope it's Help.

Conditional mean calculation in excel

I have a dataset organized as following :
The column A is the name
The column B is the date
The column C is the value registered for that person in that day
How can i calculate for the whole dataset a mean of the value of that person in the 30 past days without manually ordering for name and making the mean checking the date?
Try the AVERAGEIFS function with the EDATE function giving you a one month window.
=AVERAGEIFS(C:C, A:A, "Jack", B:B, ">"&EDATE(TODAY(), -1), B:B, "<="&TODAY())
    
You can use a nested array formula (Ctrl+Shift+Enter instead of Enter):
=AVERAGE(IF($A$2:$A$15=A2,IF($B$2:$B$15>=TODAY()-30,$C$2:$C$15,""),""))
Add columns for these two fomulas:
=COUNTIF(A1:A4,"Jack")
=SUMIF(A1:A4,"Jack",C10:C13)
That will give you the count, and it will give you the sum. With those two you can calculate the mean.
That's the basic idea, anyway.
Of course, you can add another count for the date ranges. It's all the same sort of thing.

Converting date-time format and querying based on a date condition in SQL

I have around 20,000 entries in a SQL table for which a date column is of the form
YYYY-MM-DD HH-SS. I would like to convert this format to a YYYY-MM-DD format so I can run a query on all of the entries that will count the number of entries based on
a) the month under which they fall
b) the day
I'm new to SQL and not sure if there is a way to loop through all of the entries and check based on the required criteria; and as such, would greatly appreciate any help.
I unfortunately, cannot send a screenshot of the table since the data is classified.
You don't need to change the data in the table. Most databases have year() and month() functions, so you could do:
select year(datecol), month(datecol), count(*)
from sqltable
group by year(datecol), month(datecol)
order by year(datecol), month(datecol);
If these specific functions are not available, then I'm sure your database supports something similar.