I am getting a max recursion error with this query.
WITH [Days] AS
(
SELECT DATENAME(weekday,getdate()) AS NameOfDay
,DAY(GETDATE()) AS NumberOfDay
,GETDATE() AS curDate
UNION ALL
SELECT DATENAME(WEEKDAY,DATEADD(day, -1, curDate)) As NameOfDay
,DAY(DATEADD(DAY, -1, curDate)) AS NumberOfDay
,DATEADD(DAY, -1, curDate) AS curDate
FROM [Days]
WHERE DAY(GETDATE()) - DAY(DATEADD(DAY, -1, curDate)) >= 0
)
SELECT NameOfDay
FROM [Days]
ORDER BY NumberOfDay
and the error is :
The statement terminated. The maximum recursion 100 has been exhausted before statement completion.
When I add option (maxrecursion 0) to the query, I get this error:
Adding a value to a 'datetime' column caused an overflow.
I do not know how to resolve these errors. Any help will be appreciated.
Thanks.
You can try this:
SET DATEFIRST 7;
GO
WITH [Days] AS
(
SELECT DATENAME(WEEKDAY, GETDATE()) AS NameOfDay
,DAY(GETDATE()) AS NumberOfDay
,GETDATE() AS curDate
UNION ALL
SELECT DATENAME(WEEKDAY,DATEADD(day, -1, curDate)) As NameOfDay
,DAY(DATEADD(DAY, -1, curDate)) AS NumberOfDay
,DATEADD(DAY, -1, curDate) AS curDate
FROM [Days]
WHERE DATEPART(WEEKDAY, DATEADD(DAY, -1, curDate)) >= 2
)
SELECT NameOfDay
FROM [Days]
Related
I have tried asking this before but do not think i did a good job wording my request, so apologies.
I have a table containing data as per:
;WITH MONTHSCTE AS (
SELECT
ReportMonth = DATEADD(MONTH, -1, DATEADD(DAY, 1, EOMONTH(GETDATE(), -OFFSET.X))),
Start12Months = DATEADD(MONTH, -12, DATEADD(DAY, 1, EOMONTH(GETDATE(), -OFFSET.X))),
End12Months = EOMONTH(DATEADD(MONTH, -1, DATEADD(DAY, 1, EOMONTH(GETDATE(), -OFFSET.X)))),
[Month] = DATEPART(MONTH, DATEADD(MONTH, -1, DATEADD(DAY, 1, EOMONTH(GETDATE(), -OFFSET.X)))),
[MonthName] = DATENAME(MONTH, DATEADD(MONTH, -1, DATEADD(DAY, 1, EOMONTH(GETDATE(), -OFFSET.X)))),
[Year] = DATEPART(YEAR, DATEADD(MONTH, -1, DATEADD(DAY, 1, EOMONTH(GETDATE(), -OFFSET.X))))
FROM (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12)) OFFSET(X)
)
,CTE2 AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY ReportMonth) AS [Order]
,*
FROM
MONTHSCTE
)
SELECT * FROM CTE2
I would like to use the date ranges in this dataset to query another.
Lets say i have a table called Attendances showing PatientID and date of attendance. I would like to query this 'Attendances' table for each of the date ranges in the above dataset.
So:
A count of how many patients between 2018-05-01 and 30-04-2019 attended 1,2,3,4,5,6 etc.. times in each respective date range.
The same for patients between 01-06-2018 and 2019-05-31 and so on for each range in the above dataset.
The final product should look something like this:
I have a TSQL query that works when executed through SSMS but when using the same query in an SSRS dataset it does not produce any results.
I removed the where clause in the TSQL statement which then displayed the results in the ssrs report but i need the where cluase in the TSQL statement to produce the output.
I have changed the where clause data type using cast/convert/int/varchar/date without any success.
Does anybody have a solution?
Although i tried filtering in the SSRS report itself which also did not work, i don't want to use this technique and the results will be output in csv.
Any ideas, thoughts, solutions appreciated - Thanks in advance.
Update: The report has no parameters and is designed to drop a csv based on contents of the sql below when subscribed to run;
select TenancyRef, FullName, Telephone1, Telephone2,convert(varchar (2),weekucpayday) wkpayday, substring(convert(varchar,getdate(),103),1,2) daypayday --,Currentdate, paydate,
from (
SELECT CAST(RTRIM(ZZ.propref) AS nvarchar) + CAST(RTRIM(ZZ.tensuffix) AS nvarchar) + CAST(RTRIM(ZZ.checkdigit) AS nvarchar) AS TenancyRef, RTRIM(ZZ.perstitle) + ' ' + RTRIM(ZZ.perfnames)
+ ' ' + RTRIM(ZZ.persname) AS FullName,
ZZ.Mobile_Number AS Telephone1,
ZZ.Home_Number AS Telephone2,
ZZ.tenind AS Status,
ZZ.ArrearsBalance,
a.ucdaycode,
ZZ.tenaltkey,
cast(cast(a.ucdaycode as varchar) +'/' + cast(month(GETDATE()) as varchar) +'/' + cast(YEAR(GETDATE()) as varchar ) as date) as paydate,
datename(dw,convert(date,(cast(a.ucdaycode as varchar) +'/' + cast(month(GETDATE()) as varchar) +'/' + cast(YEAR(GETDATE()) as varchar )),103)) payday,
datepart(dd,(case when datepart(dw,cast(DATEADD(dd, a.ucdaycode, DATEADD(month, DATEDIFF(month, 0, GETDATE()), -1)) as date)) = 6 then
cast(DATEADD(dd, a.ucdaycode, DATEADD(month, DATEDIFF(month, 0, GETDATE()), -2)) as date)
when datepart(dw,cast(DATEADD(dd, a.ucdaycode, DATEADD(month, DATEDIFF(month, 0, GETDATE()), -1)) as date)) = 7 then
cast(DATEADD(dd, a.ucdaycode, DATEADD(month, DATEDIFF(month, 0, GETDATE()), -3)) as date)
else cast(DATEADD(dd, a.ucdaycode, DATEADD(month, DATEDIFF(month, 0, GETDATE()), -1)) as date)
end)) as weekucpayday,
cast(DATEADD(dd, a.ucdaycode, DATEADD(month, DATEDIFF(month, 0, GETDATE()), -1)) as date) AS ucpayday
, convert(nvarchar,GETDATE(),1) Currentdate
FROM [ucdetail ] AS a INNER JOIN
(SELECT Z.propref, Z.tensuffix, Z.checkdigit, Z.tenind, Z.tenaltkey, Z.ArrearsBalance, Z.paymthdcode, T3.perstitle, T3.perfnames, T3.persname, T1.contdetails AS Mobile_Number,
T2.contdetails AS Home_Number
FROM (SELECT T.propref, T.tensuffix, T.tenaltkey, T.tenind, T.checkdigit, occupancy.relncode, occupancy.occend, occupancy.perref AS OccPerref, T.tenarrsbal AS ArrearsBalance, T.paymthdcode
FROM tenancy AS T LEFT OUTER JOIN
occupancy ON T.tenaltkey = occupancy.tenaltkey
WHERE (occupancy.relncode = 'T1') AND (GETDATE() < occupancy.occend) OR
(occupancy.relncode = 'T1') AND (occupancy.occend IS NULL)) AS Z LEFT OUTER JOIN
(SELECT perref, contdetails
FROM [contdetails ] AS C1
WHERE (UPPER(commtype) = 'T') AND (UPPER(commdesccode) = 'MOB') AND (UPPER(activeind) = 'Y') AND (UPPER(preferred) = 'Y')) AS T1 ON Z.OccPerref = T1.perref LEFT OUTER JOIN
(SELECT perref, contdetails
FROM [contdetails ] AS C2
WHERE (UPPER(commtype) = 'T') AND (UPPER(commdesccode) = 'HOME') AND (UPPER(activeind) = 'Y') AND (UPPER(preferred) = 'Y')) AS T2 ON Z.OccPerref = T2.perref LEFT OUTER JOIN
(SELECT perstitle, perfnames, persname, perref
FROM person) AS T3 ON Z.OccPerref = T3.perref) AS ZZ ON a.tenaltkey = ZZ.tenaltkey
WHERE ZZ.tenaltkey not in (select postref from tranmisc where tranmisc.acctcode = 'ATBEN') and ZZ.paymthdcode != 'DD' and (ZZ.Mobile_Number IS NOT NULL OR
ZZ.Home_Number IS NOT NULL) AND (a.status = 'Active') AND (ZZ.tenind = 'C') AND (a.ucdaycode <> 0) --AND (a.ucdaycode = DATEPART(dd, GETDATE()))
) AS UC where convert(int,UC.weekucpayday) = convert(int,(DATEPART(dd,getdate())))
I found in our database a cursor statement and I would like to replace it.
Declare #max_date datetime
Select #max_date = max(finished) From Payments
Declare #begin_date datetime = '2015-02-01'
Declare #end_of_last_month datetime
While #begin_date <= #max_date
Begin
SELECT #end_of_last_month = CAST(DATEADD(DAY, -1 , DATEFROMPARTS(YEAR(#begin_date),MONTH(#begin_date),1)) AS DATE) --AS end_of_last_month
Insert Into #table(Customer, ArticleTypeID, ArticleType, end_of_month, month, year)
Select Count(distinct (customerId)), prod.ArticleTypeID, at.ArticleType, #end_of_last_month, datepart(month, #end_of_last_month), datepart(year, #end_of_last_month)
From Customer cust
Inner join Payments pay ON pay.member_id = m.member_id
Inner Join Products prod ON prod.product_id = pay.product_id
Inner Join ArticleType at ON at.ArticleTypeID = prod.ArticleTypeID
Where #end_of_last_month between begin_date and expire_date
and completed = 1
Group by prod.ArticleTypeID, at.ArticleType
order by prod.ArticleTypeID, at.ArticleType
Set #begin_date = DATEADD(month, 1, #begin_date)
End
It groups all User per Month where the begin- and expire date in the actual Cursormonth.
Notes:
The user has different payment types, for e.g. 1 Month, 6 Month and so on.
Is it possible to rewrite the code - my problem is only the identification at the where clause (#end_of_last_month between begin_date and expire_date)
How can I handle this with joins or cte's?
What you need first, if not already is a numbers table
Using said Numbers table you can create a dynamic list of dates for "end_of_Last_Month" like so
;WITH ctexAllDates
AS (
SELECT end_of_last_month = DATEADD(DAY, -1, DATEADD(MONTH, N.N -1, #begin_date))
FROM
dbo.Numbers N
WHERE
N.N <= DATEDIFF(MONTH, #begin_date, #max_date) + 1
)
select * FROM ctexAllDates
Then combine with your query like so
;WITH ctexAllDates
AS (
SELECT end_of_last_month = DATEADD(DAY, -1, DATEADD(MONTH, N.N -1, #begin_date))
FROM
dbo.Numbers N
WHERE
N.N <= DATEDIFF(MONTH, #begin_date, #max_date) + 1
)
INSERT INTO #table
(
Customer
, ArticleTypeID
, ArticleType
, end_of_month
, month
, year
)
SELECT
COUNT(DISTINCT (customerId))
, prod.ArticleTypeID
, at.ArticleType
, A.end_of_last_month
, DATEPART(MONTH, A.end_of_last_month)
, DATEPART(YEAR, A.end_of_last_month)
FROM
Customer cust
INNER JOIN Payments pay ON pay.member_id = m.member_id
INNER JOIN Products prod ON prod.product_id = pay.product_id
INNER JOIN ArticleType at ON at.ArticleTypeID = prod.ArticleTypeID
LEFT JOIN ctexAllDates A ON A.end_of_last_month BETWEEN begin_date AND expire_date
WHERE completed = 1
GROUP BY
prod.ArticleTypeID
, at.ArticleType
, A.end_of_last_month
ORDER BY
prod.ArticleTypeID
, at.ArticleType;
How do you divide a count by another count. I have seen a few different methods but I am unable to get them to work for my purposes. The code that I am working on currently is:
Select(select COUNT(lUsers)
FROM Tlocation
WHERE dLastUpdated IS NOT NULL
AND dRemovalDate BETWEEN DATEADD(day,-7,GETDATE()) and GETDATE())
/
(SELECT COUNT(lUsers)
FROM Tlocation
WHERE dLastUpdated < GETDATE()
AND dRemovalDate IS NULL OR dRemovalDate > GETDATE())
But this just returns a 0 every time.
It's because the value is less than an INT (which COUNT() returns). Here's how you could do it:
SELECT
(
SELECT 1562
)
/
CAST(
(
SELECT 92825
)
AS DECIMAL(20, 10)
)
;
Or for your query:
Select(select COUNT(lUsers)
FROM Tlocation
WHERE dLastUpdated IS NOT NULL
AND dRemovalDate BETWEEN DATEADD(day,-7,GETDATE()) and GETDATE())
/
CAST(
(SELECT COUNT(lUsers)
FROM Tlocation
WHERE dLastUpdated < GETDATE()
AND dRemovalDate IS NULL OR dRemovalDate > GETDATE())
AS DECIMAL(20, 10)
)
;
I have a table with date range an i need the sum of overlapping periods (in hours) between its rows.
This is a schema example:
create table period (
id int,
starttime datetime,
endtime datetime,
type varchar(64)
);
insert into period values (1,'2013-04-07 8:00','2013-04-07 13:00','Work');
insert into period values (2,'2013-04-07 14:00','2013-04-07 17:00','Work');
insert into period values (3,'2013-04-08 8:00','2013-04-08 13:00','Work');
insert into period values (4,'2013-04-08 14:00','2013-04-08 17:00','Work');
insert into period values (5,'2013-04-07 10:00','2013-04-07 11:00','Holyday'); /* 1h overlapping with 1*/
insert into period values (6,'2013-04-08 10:00','2013-04-08 20:00','Transfer'); /* 6h overlapping with 3 and 4*/
insert into period values (7,'2013-04-08 11:00','2013-04-08 12:00','Test'); /* 1h overlapping with 3 and 6*/
And its fiddle: http://sqlfiddle.com/#!6/9ca31/10
I expect a sum of 8h overlapping hours:
1h (id 5 over id 1)
6h (id 6 over id 3 and 4)
1h (id 7 over id 3 and 6)
I check this: select overlapping datetime events with SQL but seems to not do what I need.
Thank you.
select sum(datediff(hh, case when t2.starttime > t1.starttime then t2.starttime else t1.starttime end,
case when t2.endtime > t1.endtime then t1.endtime else t2.endtime end))
from period t1
join period t2 on t1.id < t2.id
where t2.endtime > t1.starttime and t2.starttime < t1.endtime;
Updated to handle several overlaps:
select sum(datediff(hh, start, fin))
from (select distinct
case when t2.starttime > t1.starttime then t2.starttime else t1.starttime end as start,
case when t2.endtime > t1.endtime then t1.endtime else t2.endtime end as fin
from period t1
join period t2 on t1.id < t2.id
where t2.endtime > t1.starttime and t2.starttime < t1.endtime
) as overlaps;
I have some "dirty" solution. Hope this helps :)
with src as (
select
convert(varchar, starttime, 112) [start_date]
, cast(left(convert(varchar, starttime, 108), 2) as int) [start_time]
, convert(varchar, endtime, 112) [end_date]
, cast(left(convert(varchar, endtime, 108), 2) as int) [end_time]
, id
from [period]),
[gr] as (
select
row_number() over(order by s1.[start_date], s1.[start_time], s1.[end_time], s2.[start_time], s2.[end_time]) [no]
, s1.[start_date] [date]
, s1.[start_time] [t1]
, s1.[end_time] [t2]
, s2.[start_time] [t3]
, s2.[end_time] [t4]
from src s1
join src s2 on s1.[start_date] = s2.[start_date]
and s1.[end_date] = s2.[end_date]
and (s1.[start_time] between s2.[start_time] and s2.[end_time] or s1.[end_time] between s2.[start_time] and s2.[end_time])
and s1.id != s2.id),
[raw] as (
select [no], [date], [t1] [h] from [gr] union all
select [no], [date], [t2] from [gr] union all
select [no], [date], [t3] from [gr] union all
select [no], [date], [t4] from [gr]),
[max_min] as (
select [no], [date], max(h) [max_h], min(h) [min_h]
from [raw]
group by [no], [date]
),
[result] as (
select [raw].*
from [raw]
left join [max_min] on [raw].[no] = [max_min].[no]
and ([raw].h = [max_min].[max_h] or [raw].h = [max_min].[min_h])
where [max_min].[no] is null),
[final] as (
select distinct r1.[date], r1.h [start_h], r2.h [end_h], abs(r1.h - r2.h) [dif]
from [result] r1
join [result] r2 on r1.[no] = r2.[no]
where abs(r1.h - r2.h) > 0
and r1.h > r2.h)
select sum(dif) [overlapping hours] from [final]
SQLFiddle