Link to home
Start Free TrialLog in
Avatar of Ali Shah
Ali ShahFlag for United Kingdom of Great Britain and Northern Ireland

asked on

T-SQL help required

Hi EE,

I have got a scenario that is we have contracted customers and contract can be renewed many times. Each renewed contract has got the originating contractID and FirstContractId.
The contract can have a service on anytime time during its whole life that is from the firstcon tract to the renewed contract. There is a expire flag as well. I need to find for all Active contracts if they had been serviced in their entire life and even if the renewed contract which is active has not been serviced but one of its originating or first contract was serviced i need to put a service flag on. Now i have achieved this by following code. Just wondering is there any better way of doing it.
For simplicity i am using sample data
IF OBJECT_ID('tempdb..#OriginatingContract') IS NOT NULL
BEGIN
  DROP TABLE #OriginatingContract
END

CREATE TABLE #OriginatingContract (
  ContractId int,
  OriginatingContract int,
  FirstContract int,
  OrgServiced bit,
  Expired bit,
  Serviced bit
)
DECLARE @t TABLE (
  ContractId int,
  OriginatingContract int,
  FirstContract int,
  Serviced bit,
  Expired bit
)

INSERT INTO @t
  VALUES (1, NULL, 1, 1, 1),
  (2, 1, 1, 0, 1),
  (3, 2, 1, 0, 1),
  (4, 3, 1, 0, 0),
  (5, NULL, 5, 0, 1),
  (6, 5, 5, 1, 1),
  (7, 6, 5, 0, 1),
  (8, 7, 5, 0, 0),
  (9, NULL, 9, 0, 1),
  (10, 9, 9, 0, 1),
  (11, 10, 9, 1, 1),
  (12, 11, 9, 0, 0),
  (13, NULL, 13, 0, 1),
  (14, 13, 13, 0, 1),
  (15, 14, 13, 0, 1),
  (16, 15, 13, 0, 1),
  (17, 16, 13, 1, 0)

INSERT INTO #OriginatingContract (ContractId, OriginatingContract, FirstContract, OrgServiced, Expired)
  SELECT
    ContractId,
    OriginatingContract,
    FirstContract,
    Serviced,
    Expired
  FROM @t
  WHERE Expired = 0;

DECLARE @t1 TABLE (
  Firstcontract int,
  serviced bit,
  servicerow int
)

INSERT INTO @t1

  SELECT
    FirstContract,
    Serviced,
    ROW_NUMBER() OVER (PARTITION BY firstcontract ORDER BY serviced DESC) servicerow
  FROM @t

UPDATE #OriginatingContract
SET Serviced = tv1.Serviced
FROM #OriginatingContract OrigiC
INNER JOIN @t1 tv1
  ON OrigiC.FirstContract = tv1.FirstContract
  AND tv1.servicerow = 1

SELECT
  *
FROM #OriginatingContract

Open in new window


Kindest regards
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ali Shah

ASKER

Wow this is magnificent and awesome.
Thank you so much for your help
Just for fun, when you need the first contract who serviced:

DECLARE @Contracts TABLE
    (
        ContractId INT PRIMARY KEY ,
        OriginatingContract INT ,
        FirstContract INT ,
        Serviced BIT ,
        Expired BIT
    );

INSERT INTO @Contracts
VALUES ( 1, NULL, 1, 1, 1 ) ,
       ( 2, 1, 1, 0, 1 ) ,
       ( 3, 2, 1, 0, 1 ) ,
       ( 4, 3, 1, 0, 0 ) ,
       ( 5, NULL, 5, 0, 1 ) ,
       ( 6, 5, 5, 1, 1 ) ,
       ( 7, 6, 5, 0, 1 ) ,
       ( 8, 7, 5, 0, 0 ) ,
       ( 9, NULL, 9, 0, 1 ) ,
       ( 10, 9, 9, 0, 1 ) ,
       ( 11, 10, 9, 1, 1 ) ,
       ( 12, 11, 9, 0, 0 ) ,
       ( 13, NULL, 13, 0, 1 ) ,
       ( 14, 13, 13, 0, 1 ) ,
       ( 15, 14, 13, 0, 1 ) ,
       ( 16, 15, 13, 0, 1 ) ,
       ( 17, 16, 13, 1, 0 );

WITH Hierarchy
AS ( SELECT A.ContractId ,
            A.OriginatingContract ,
            A.Serviced ,
            A.Expired ,
            A.ContractId AS RootContractID ,
            '\\' + CAST(A.ContractId AS NVARCHAR(MAX)) AS ContractPath ,
            0 AS ContractLevel ,
            A.Serviced AS InPathServiced ,
            IIF(A.Serviced = 1, A.ContractId, NULL) AS ServicedBy
     FROM   @Contracts A
     WHERE  A.OriginatingContract IS NULL
     UNION ALL
     SELECT C.ContractId ,
            C.OriginatingContract ,
            C.Serviced ,
            C.Expired ,
            P.RootContractID ,
            P.ContractPath + '\' + CAST(C.ContractId AS NVARCHAR(MAX)) ,
            P.ContractLevel + 1 ,
            IIF(C.Serviced = 1, C.Serviced, P.InPathServiced) ,
            IIF(C.Serviced = 1, C.ContractId, P.ServicedBy)
     FROM   Hierarchy P
            INNER JOIN @Contracts C ON P.ContractId = C.OriginatingContract )
SELECT   H.*
FROM     Hierarchy H
ORDER BY H.ContractPath;

Open in new window

Thank you very much this is great great help.
Regards,
Please explain your tables in your query that works. I can see #originatingcontract

But what does @t represent ? and @t1 ?

Seems to me that everything is in #originatingcontracts