Help in Correlated Subquery.

  • SET NOCOUNT ON

    DECLARE @ceb TABLE

    (

    IRNumber INT

    )

    INSERT @ceb VALUES (1)

    INSERT @ceb VALUES (2)

    INSERT @ceb VALUES (3)

    INSERT @ceb VALUES (4)

    INSERT @ceb VALUES (5)

    INSERT @ceb VALUES (6)

     

    DECLARE @CCC TABLE 

    (

    CaseRow  INT,

    CaseName VARCHAR(20),

    CaseNumber INT

    )

    INSERT @CCC VALUES (1, 'AAA', 1)

    INSERT @CCC VALUES (2, 'AAA', 1)

    INSERT @CCC VALUES (3, 'AAA', 1)

    INSERT @CCC VALUES (4, 'AAA', 2)

    INSERT @CCC VALUES (5, 'AAA', 3)

    INSERT @CCC VALUES (6, 'AAA', 3)

    INSERT @CCC VALUES (7, 'AAA', 3)

    INSERT @CCC VALUES (8, 'AAA', 4)

    INSERT @CCC VALUES (9, 'AAA', 4)

    INSERT @CCC VALUES (10, 'AAA', 5)

    INSERT @CCC VALUES (11, 'AAA', 5)

    INSERT @CCC VALUES (12, 'AAA', 5)

    INSERT @CCC VALUES (13, 'AAA', 6)

     

    SELECT * FROM @ceb A

    JOIN

     @CCC B

    ON

     A.IRNumber = B.CaseNumber

    /* Since there is more than one row match I need only the first CaseRow */

    /* Expected result */

    IRNumber    CaseRow     CaseName             CaseNumber 

    ----------- ----------- -------------------- -----------

    1           1           AAA                  1

    2           4           AAA                  2

    3           5           AAA                  3

    4           8           AAA                  4

    5           10          AAA                  5

    6           13          AAA                  6

    Regards,
    gova

  • SELECT min(b.CaseRow), IRNumber, CaseName, CaseNumber

    FROM @ceb A

    JOIN @CCC B

        ON A.IRNumber = B.CaseNumber

    Group by IRNumber, CaseName, CaseNumber

  • Thanks Ray.

    I thought I presented my problem with two tables. Which I did not. It involves 5 tables and a count(IRNumber) along with Min(CaeRow).

    I managed to make my query. Thanks for your help.

    Regards,
    gova

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply