Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

Help with Query

$
0
0
Hello,

I build a query back in the day, but I am trying to remember how I did it.

I have three tables. #TESTS contains all the tests required. #EXEC_ORDER order contains the prerequisites definition. #SUBGROUP is the transaction table.

Code:

CREATE TABLE #SUBGROUP (
        SYSID INT IDENTITY(1,1) NOT NULL,
        TESTID INT NOT NULL,
        COMPLETED BIT NOT NULL DEFAULT 'FALSE'
)

CREATE TABLE #TESTS (
        SYSID INT IDENTITY(1,1) NOT NULL,
        TESTID INT NOT NULL
)

CREATE TABLE #EXEC_ORDER (
        SYSID INT IDENTITY(1,1) NOT NULL,
        TESTID1 INT NOT NULL,
        TESTID2 INT NOT NULL
)


INSERT INTO #TESTS (TESTID) VALUES (1),(2),(3),(4),(5)
INSERT INTO #EXEC_ORDER (TESTID1,TESTID2) VALUES (2,3),(2,4),(5,4)

I am looking for Tests #1 and #2 would be returned. When Test #2 is completed, then Tests #1, #3 and #4 would be returned. When Test #4 is completed, then Tests #1, #3, and #5 would be returned.

Code:

SELECT t.TESTID,s.COMPLETED
FROM #TESTS t
LEFT JOIN #SUBGROUP s
  ON t.TESTID = s.TESTID
 AND s.COMPLETED = 'FALSE'
LEFT JOIN #EXEC_ORDER eo
  ON t.TESTID = eo.TESTID1

Thanks
jlimited

Viewing all articles
Browse latest Browse all 13329

Trending Articles