Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««123

Recursive CTE? Not Sure How To Do This... Expand / Collapse
Author
Message
Posted Saturday, May 24, 2014 7:11 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, August 20, 2014 5:06 PM
Points: 154, Visits: 584
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.




"If I had been drinking out of that toilet, I might have been killed." -Ace Ventura
Post #1574347
Posted Sunday, May 25, 2014 1:18 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 9:43 PM
Points: 1,901, Visits: 19,002
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.



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 !
__________________________________________________________________
Post #1574363
Posted Sunday, May 25, 2014 8:30 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, August 20, 2014 5:06 PM
Points: 154, Visits: 584
Normally I would be sharing all my code. But this particular time it would not be appropriate to share. 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. :)



"If I had been drinking out of that toilet, I might have been killed." -Ace Ventura
Post #1574388
Posted Sunday, May 25, 2014 9:13 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:34 PM
Points: 36,978, Visits: 31,498
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.



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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1574389
Posted Sunday, May 25, 2014 9:26 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, August 20, 2014 5:06 PM
Points: 154, Visits: 584
Yessir, all is well! :) Thank you for checking back in!


"If I had been drinking out of that toilet, I might have been killed." -Ace Ventura
Post #1574390
Posted Sunday, May 25, 2014 9:59 AM This worked for the OP Answer marked as solution


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 9:43 PM
Points: 1,901, Visits: 19,002
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
)

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 !
__________________________________________________________________
Post #1574395
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse