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
Ali ShahSQL DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
hmm, looks like you need a hierarchic query.

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
     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)
     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

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ali ShahSQL DeveloperAuthor Commented:
Wow this is magnificent and awesome.
Thank you so much for your help
0
ste5anSenior DeveloperCommented:
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

0
Ali ShahSQL DeveloperAuthor Commented:
Thank you very much this is great great help.
Regards,
0
Mark WillsTopic AdvisorCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.