The Date_Bucket function is introduced in Azure SQL Edge which is mainly used by IoT devices. This useful function returns the date-time value corresponding to the start of each date-time bucket from the timestamp defined by the origin parameter, or the default origin value of 1900-01-01 00:00:00.000. In other words, it lets you arrange data into groups that represent fixed intervals of time. SQL Server 2022 includes this useful function in its database engine.
Results of Data_Bucket might be confusing, Let's look at its syntax first.
DATE_BUCKET (datepart, number, date, origin)
datepart
Datepart needs to be one of the following datepart list, this parameter controls the size of 1 bucket. Do you want to get the summary of invoices in a week, a month, or a minute? datepart will control the default size of a bucket.
datepart | Abbreviations |
day | dd,d |
week | wk,ww |
month | mm,m |
quarter | qq,q |
year | yy,yyyy |
hour | hh |
minute | mi,n |
second | ss,s |
millisecond | ms |
SELECT DATE_BUCKET(week,1, InvoiceDate) as InvoiceWeek,
COUNT(CustomerId) as CustomerCount
FROM Sales.Invoices
GROUP BY DATE_BUCKET(week,1, InvoiceDate)
ORDER BY InvoiceWeek
SELECT DATE_BUCKET(week,2, InvoiceDate) as InvoiceWeek,
COUNT(CustomerId) as CustomerCount
FROM Sales.Invoices
GROUP BY DATE_BUCKET(week,2, InvoiceDate)
ORDER BY InvoiceWeek
No comments:
Post a Comment