• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 55
  • Last Modified:

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
0
Ali Shah
Asked:
Ali Shah
  • 2
  • 2
1 Solution
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now