T-SQL Generate sequence of days after certain date - sql

I have a sequence of days as an array like the following:
DECLARE #days VARCHAR(MAX) = N'Monday, Wednesday, Friday'
And a n amount of occurrences like:
DECLARE #occurrences INT = 10
In this way I can generate a table using the following query and a custom table called Numbers which contains 1,000,000 numbers:
SELECT TOP #occurrences
Item
FROM
dbo.SplitStrings_Numbers(#days, ',')
CROSS JOIN Numbers
ORDER BY n
The result is the following:
|----------------------|
| 1 | Monday |
|----------------------|
| 2 | Wednesday |
|----------------------|
| 3 | Friday |
|----------------------|
| 4 | Monday |
|----------------------|
| 5 | Wednesday |
|----------------------|
| 6 | Friday |
|----------------------|
Now what I need to complete this function is to start the sequence with the first day that occur based on a specified date.
So, when I declare start from 1st of September
DECLARE #start DATETIME = '2015-09-01'
The result should look like the following:
|------------------------------------|
| 1 | Wednesday | 2015-09-02 |
|------------------------------------|
| 2 | Friday | 2015-09-04 |
|------------------------------------|
| 3 | Monday | 2015-09-07 |
|------------------------------------|
| 4 | Wednesday | 2015-09-09 |
What I cannot calculate is this pseudo-code:
"Given an array of days and a starting date, get the first occurrence after that date".

This is not a complete answer but it will get you started:
;With DatesCte AS
(
SELECT TOP(#occurrences) DATEADD(DAY, number-1, #Start) As TheDate
FROM Numbers
), DatesWithWeekDay As
(
SELECT TheDate, DATENAME(WEEKDAY, TheDate) As WeekDayName
FROM DatesCTE
)
SELECT *
FROM DatesWithWeekDay

Related

Display dates as columns using pivot

I have a table like this.
------------------------------------
Id | Name | Date
-----------------------------------
1 | Syam | 2017-05-23 13:53:20.000
2 | Syam | 2017-05-22 13:53:20.000
3 | Syam | NULL
4 | Ram | 2017-05-23 13:53:20.000
5 | Ram | 2017-05-21 13:53:20.000
I need to find out the users who have submitted their log in 7 days prior to the specified date input by user.
The result will be like this
Input: 2017-05-28 13:53:20.000
Id | Name | 05/23/2017 tuesday | 05/22/2017 Monday | 05/21/2017
--------------------------------------------------------------
1 |Syam | True | true | False
2 |Ram | True | False | True
Can some one help me to do this. I think pivot can be used for this. But I am not familiar with that.
you can use pivot
select pvt.* from
(
select id,name,DATENAME(WEEKDAY, date1) as DayName from t
) as t1
PIVOT
(max(name) for DayName in ([Tuesday],[Monday],[Sunday] )) as pvt -- you can put 7days name here

Get count for each time row appears in the range between 2 dates

I'am trying to calculate how many times a row "appears" a in the range between 2 dates and grouping them by the month.
So, let's say i have rows that look like this:
Name | StartDate | EndDate
-----------|-----------------|------------
Mathias | 2017-01-01 | 2017-04-01
Lucas | 2017-01-01 | 2017-04-01
i would like to get the output that shows how many records exists between the 2 dates in a query, so something like the following output:
Count | Year | Month
-----------|-----------------|------------
2 | 2017 | 1
2 | 2017 | 2
2 | 2017 | 3
2 | 2017 | 4
0 | 2017 | 5
0 | 2017 | 6
what i've tried is:
SELECT COUNT(*) as COUNT, YEAR(StartDate) YEAR, MONTH(StartDate) MONTH
FROM NamesTable
WHERE Start >= '2017-01-01 00:00:00'
AND Slut <= '2017-06-01 00:00:00'
group by YEAR(StartDate), MONTH(StartDate)
where this is giving me the expected output of:
Count | Year | Month
-----------|-----------------|------------
2 | 2017 | 1
0 | 2017 | 2
0 | 2017 | 3
0 | 2017 | 4
0 | 2017 | 5
0 | 2017 | 6
Because of grouping by the "start date", how can i count rows in the month for every one it expands across?
You need a table with the months range
Table allMonths
+---------+------------+------------+
| monthId | StartDate | EndDate |
+---------+------------+------------+
| 1 | 2017-01-01 | 2017-01-02 |
| 2 | 2017-01-02 | 2017-01-03 |
| 3 | 2017-01-03 | 2017-01-04 |
| 4 | 2017-01-04 | 2017-01-05 |
| 5 | 2017-01-05 | 2017-01-06 |
| 6 | 2017-01-06 | 2017-01-07 |
| 7 | 2017-01-07 | 2017-01-08 |
| 8 | 2017-01-08 | 2017-01-09 |
| 9 | 2017-01-09 | 2017-01-10 |
| 10 | 2017-01-10 | 2017-01-11 |
| 11 | 2017-01-11 | 2017-01-12 |
| 12 | 2017-01-12 | 2018-01-01 |
+---------+------------+------------+
Then your query is:
SELECT am.startDate, COUNT(y.Name)
FROM allMonths am
LEFT JOIN yourTable y
ON am.StartDate <= y.EndDate
AND am.EndDate >= y.StartDate
GROUP BY am.startDate
NOTE: You need to check border cases. Maybe you need change >= to > or change EndDate to the last day of the month.
So, what i ended up doing was something like Juan Carlos proposed, but instead of creating a table i made it up with CTE instead for a cleaner approach:
Declare #todate datetime, #fromdate datetime, #firstOfMonth datetime, #lastOfMonth datetime
Select
#fromdate='2017-01-11',
#todate='2017-12-21',
#firstOfMonth = DATEADD(month, DATEDIFF(month, 0, #fromdate), 0), ----YEAR(#fromdate) + MONTH(#fromdate) + DAY(1),
#lastOfMonth = DATEADD(month, ((YEAR(#fromdate) - 1900) * 12) + MONTH(#fromdate), -1)
;with MonthTable (MonthId, StartOfMonth, EndOfMonth) as
(
SELECT MONTH(#firstOfMonth) as MonthId, #firstOfMonth as StartOfMonth, #lastOfMonth as EndOfMonth
UNION ALL
SELECT MONTH(DATEADD(MONTH, 1, StartOfMonth)), DATEADD(MONTH, 1, StartOfMonth), DATEADD(MONTH, 1, EndOfMonth)
FROM MonthTable
WHERE StartOfMonth <= #todate
)
SELECT am.StartOfMonth, COUNT(y.Start) as count
FROM MonthTable am
left JOIN clientList y
ON y.Start <= am.StartOfMonth
AND y.End >= am.EndOfMonth
GROUP BY am.StartOfMonth

get data for first 3 months that have claims

I have a table of members and their claims value, I'm interested in getting the claims values for the first 3 months for each member. Here's what I've tried so far:
WITH START as
(SELECT [HEALTH_ID]
,MIN([CLM_MONTH]) as DOS
FROM [TEST]
GROUP BY
[HEALTH_PLAN_ID])
SELECT HEALTH_ID
,DOS
,FORMAT(DATEADD(month, +1, DOS), 'MM/dd/yyyy')
,FORMAT(DATEADD(month, +2, DOS), 'MM/dd/yyyy')
FROM START
My plan is to get the dates of the first 3 months with claims then join the claim amounts to ID and dates. The problem here is not every member has claims in consecutive months and the dateadd function gives me consecutive months. For example if a member has claims in jan, feb, april, may etc...I'm interested in the claims for jan, feb and april since there were no claims in march. Using the dateadd function would give me dates jan, feb, march excluding april.
In summary, I need help getting the first 3 months that have claims values(months may or may not be consecutive).
Using dense_rank() to rank the months, partitioned by Health_Id, in order to filter for the first three months of each Health_Id.
;with cte as (
select *
, dr = dense_rank() over (
partition by Health_ID
order by dateadd(month, datediff(month, 0, CLM_Month) , 0) /* truncate to month */
)
from test
)
select *
from cte
where dr < 4 -- dense rank of 1-3
test data:
create table test (health_id int, clm_month date)
insert into test values
(1,'20170101'),(1,'20170201'),(1,'20170301'),(1,'20170401')
,(2,'20170101'),(2,'20170201'),(2,'20170401'),(2,'20170501') -- no March
,(3,'20170101'),(3,'20170115'),(3,'20170201'),(3,'20170215') -- Multiple per month
,(3,'20170401'),(3,'20170415'),(3,'20170501'),(3,'20170515')
rextester demo: http://rextester.com/MTZ16877
returns:
+-----------+------------+----+
| health_id | clm_month | dr |
+-----------+------------+----+
| 1 | 2017-01-01 | 1 |
| 1 | 2017-02-01 | 2 |
| 1 | 2017-03-01 | 3 |
| 2 | 2017-01-01 | 1 |
| 2 | 2017-02-01 | 2 |
| 2 | 2017-04-01 | 3 |
| 3 | 2017-01-01 | 1 |
| 3 | 2017-01-15 | 1 |
| 3 | 2017-02-01 | 2 |
| 3 | 2017-02-15 | 2 |
| 3 | 2017-04-01 | 3 |
| 3 | 2017-04-15 | 3 |
+-----------+------------+----+

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

Count concurrent dates in user-input date range using SQL

The user will input a date range, and I want to output in SQL every date between and including that range in the number of concurrent uses of said equipment.
In this example, the user date range is 03/08/2016 to 03/09/2016, so you can see below I include anything on or between those dates (grouped by category, but I've simplified here by only using 'powerchair')
The table schema is as follows;
trans_date | trans_end_date | eq_category
17/03/2016 | 16/10/2016 | POWERCHAIR
08/08/2016 | 08/08/2016 | POWERCHAIR
12/08/2016 | 12/08/2016 | POWERCHAIR
17/08/2016 | 18/08/2016 | POWERCHAIR
22/08/2016 | 22/08/2016 | POWERCHAIR
26/08/2016 | 26/08/2016 | POWERCHAIR
02/09/2016 | 02/09/2016 | POWERCHAIR
And I would like to output;
date | concurrent_use
03-08-2016 | 1
04-08-2016 | 1
05-08-2016 | 1
06-08-2016 | 1
07-08-2016 | 1
08-08-2016 | 2
09-08-2016 | 1
10-08-2016 | 1
11-08-2016 | 1
12-08-2016 | 2
13-08-2016 | 1
14-08-2016 | 1
15-08-2016 | 1
16-08-2016 | 1
17-08-2016 | 2
18-08-2016 | 2
19-08-2016 | 1
20-08-2016 | 1
21-08-2016 | 1
22-08-2016 | 2
23-08-2016 | 1
24-08-2016 | 1
25-08-2016 | 1
26-08-2016 | 2
27-08-2016 | 1
28-08-2016 | 1
29-08-2016 | 1
30-08-2016 | 1
31-08-2016 | 1
01-09-2016 | 1
02-09-2016 | 2
03-09-2016 | 1
Anything 1 or 0, I can then filter out as there mustn't have been any equipment out concurrently that day.
I don't think this is a gaps/islands problem, but I'm drawing a blank trying to get this in an SQL statement.
Try like below. You need to generate dates using recursive cte. Then we need to count the no of occurrences of each date falling in range.
;WITH CTE
AS (SELECT CONVERT(DATE, '2016-08-03', 103) DATE1
UNION ALL
SELECT Dateadd(DAY, 1, DATE1) AS DATE1
FROM CTE
WHERE Dateadd(DD, 1, DATE1) <= '2016-09-03')
SELECT C.DATE1,
Count(1) OCCURENCES
FROM CTE C
JOIN #TABLE1 T
ON C.DATE1 BETWEEN [TRANS_DATE] AN [TRANS_END_DATE]
GROUP BY C.DATE1
You need a set of numbers or dates. So, if you want everything in that range:
with d as (
select cast('2016-08-03' as date) as d
union all
select dateadd(day, 1, d.d)
from d
where d < '2016-09-03'
)
select d.d, count(s.trans_date)
from d left join
schema s
on d.d between s.trans_date and s.trans_date_end
group by d.d;
I'm not sure if both the start and end dates are included in the range.