Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Recursive CTE? Not Sure How To Do This...


Recursive CTE? Not Sure How To Do This...

Author
Message
autoexcrement
autoexcrement
SSC Veteran
SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)

Group: General Forum Members
Points: 204 Visits: 777
Update:

J Livingston's version of Craig's version of Jeff's QU is basically the solution I am going with. Thanks again to J and also to everyone else involved in getting us to that point.

For anyone else following along, I just thought I'd mention however that I omitted SalesRepID from my index and from my case statements on eligibility. My index and row_number, etc. were done on CustID, SaleDate.

Also, for whatever reason, I wasn't able to get it all working with the 3-part assignment syntax. Maybe my fault. But it's all working great with the more verbose 2-part assignments.

:-D


"If I had been drinking out of that toilet, I might have been killed." -Ace Ventura
J Livingston SQL
J Livingston SQL
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3445 Visits: 33011
autoexcrement (5/24/2014)
Also, for whatever reason, I wasn't able to get it all working with the 3-part assignment syntax. Maybe my fault. But it's all working great with the more verbose 2-part assignments.

:-D


wondering why you couldn't get it to work with 3-part?
...care to share your code?

________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

autoexcrement
autoexcrement
SSC Veteran
SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)

Group: General Forum Members
Points: 204 Visits: 777
Normally I would be sharing all my code. But this particular time it would not be appropriate to share. Sad I had to abstract the problem and the code a lot to even post the original question.

I will give the 3-part assignment another go next week. I bet it is just something screwy that I did. But the main message is: QU works, and it works gloriously. Smile


"If I had been drinking out of that toilet, I might have been killed." -Ace Ventura
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44994 Visits: 39880
autoexcrement (5/24/2014)
For anyone else following along, I just thought I'd mention however that I omitted SalesRepID from my index and from my case statements on eligibility. My index and row_number, etc. were done on CustID, SaleDate.

:-D


I'm not sure which index is "my index" above and, apologies, I don't have the time to go back through this post but I want to make sure that you understand that if SalesRepID is one of the columns that you're relying on for the proper order of the QU, it needs to be included in the clustered index that the QU uses.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
autoexcrement
autoexcrement
SSC Veteran
SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)SSC Veteran (204 reputation)

Group: General Forum Members
Points: 204 Visits: 777
Yessir, all is well! Smile Thank you for checking back in!


"If I had been drinking out of that toilet, I might have been killed." -Ace Ventura
J Livingston SQL
J Livingston SQL
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3445 Visits: 33011
Hi auto....code without the SalesRepId.......based on previous code provided....still using the temp table.





IF OBJECT_ID('#tempsales') IS NOT NULL DROP TABLE #tempsales

SELECT CustID
   , SalesRepID
   , SaleDate
   , eligibility
INTO #tempsales
FROM Sales

CREATE CLUSTERED INDEX cix_jls ON #tempsales
(CustID,
/*SalesRepID, */
SaleDate) /*required for QU to run correctly*/

/*the quirky update code */


DECLARE @CustID INT
   , @SalesRepID INT
   , @SaleDate DATETIME
   , @eligibility INT
   , @eligibilityDate DATETIME
   , @SafetyCounter BIGINT
   , @LockDays TINYINT

SET @eligibilityDate = (select min(saledate) from #tempsales)
SET @LockDays = 1
SET @SafetyCounter = 1

;WITH cte_tempsales /*Adds a "safety counter" to the rows in the expected processing order which is used as a check in QU*/
AS (
   SELECT SafetyCounter = ROW_NUMBER() OVER
   (ORDER BY
   CustID,
   /*SalesRepID, */
   SaleDate)
      , CustID
/*      , SalesRepID*/
      , SaleDate
      , eligibility
   FROM #tempsales
   Wink

UPDATE cte_tempsales
SET @eligibility = eligibility =
CASE /*This CASE does the safety check*/
   WHEN SafetyCounter = @SafetyCounter /*Checks sequence of processing*/
      THEN CASE
          WHEN @CustID = CustID
             /*AND @SalesRepID = SalesRepID */
             AND SaleDate < DATEADD(dd, @LockDays, @eligibilityDate)
             THEN 0 /* ineligible*/
            ELSE 1 /* eligible*/
            END
   ELSE 1/0 /*- force error if QU out of sync*/
   END
   
   , @SafetyCounter = @SafetyCounter + 1
   , @CustID = CustID
/*   , @SalesRepID = SalesRepID*/
   , @eligibilityDate =
    CASE
      WHEN @eligibility = 1
         THEN SaleDate
      ELSE @eligibilityDate
      END
FROM cte_tempsales WITH (TABLOCKX) /* required for QU*/
OPTION (MAXDOP 1)   /* required for QU*/

/*results*/

SELECT top 10000 *
FROM #tempsales
order by custid,SaleDate






________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search