0

I have a CTE query like so:

declare @stop datetime
set @stop='2014-12-12 00:00:00.000'

declare @start datetime
set @start='2011-12-12 00:00:00.000'

declare @temp datetime
set @temp=@start


with X(A,B) AS(
     select @Temp as A,count(*) as B
          from Table1
               where left(convert(varchar,Table1Row1,120),4)=left(convert(varchar,@Temp,120),4)
      union all select dateadd(year,1,(select X.A from X)) as A,count(*) as B
          from Table1
               where left(convert(varchar,Table1Row1,120),4)=left(convert(varchar,(select X.A from X),120),4)
               and datediff(YYYY,(select X.A from X),@stop)>0)
      select A,B into #TempTbl1 from X option(maxrecursion 32767);

I'm trying to get the count of rows for each year from @start to @stop. The query accepts the select X.A from X at the select statement but not at the where clause.

I'm getting a compile time error stating: Recursive Member of a Common table expression 'X' has multiple recursive references.

On executing, I'm getting the error recursive references are not allowed in sub-queries. But, I've just referenced it in the select query where it shows no error. Is there a syntactical problem, or am I just not allowed to reference the table there?

2
  • Where is your datetime column in the Table1? What is the relation between @Start, @Stop and your table? Commented Mar 24, 2014 at 8:15
  • @alzaimar: In table1, Table1Row1 is the datetime column. Start and Stop are two datetimes which indicate the start and stop years. ie from 2012-12-01 00:00:00.000 to 2012-12-01 00:00:00.000 Commented Mar 24, 2014 at 8:44

2 Answers 2

1
select dateadd(year,1,(select X.A from X)) as A,count(*) as B
          from Table1
               where left(convert(varchar,Table1Row1,120),4)=left(convert(varchar,(select X.A from X),120),4)
               and datediff(YYYY,(select X.A from X),@stop)>0

You select X three times in the query. Try doing a join from on Table1 and X instead of 3 subquery's.

Also I think this query can be done allot easier. Something like:

SELECT DATEPART(year, datecolumn), COUNT(pk)
FROM Table1
WHERE datecolumn between @startdate AND @enddate
GROUP BY DATEPART(year, datecolumn)
Sign up to request clarification or add additional context in comments.

2 Comments

I get it, when I try using X in a query just once, it works. Why is it like that?
And I know it can be done easier, just wanted to try out some CTE methods.
0

As mentioned in my comment, I don't see a relation with the date range and your Table1. I would assume some kind of a date or datetime column.

If your table would have column to use, you could simply execute the following query.

select DatePart (year,Table1DateTimeColumn) as Year,
        count (*) as Cnt
  from Table1
 where Table1DateTimeColumn between @Start and @Stop
 group by DatePart (year,Table1DateTimeColumn)

1 Comment

Yes, I understand that there is an easier way, just I'm trying to read up on Common Table Expressions, and understand how they function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.