SQL Server 2000 Correlated Query Question

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default SQL Server 2000 Correlated Query Question

    Hey all,

    Have another question. I've got a SQL Server 2000 DB that contains employee information....we've got a table called EMPLOYEE_MASTER and one called EMPLOYEE_DETAIL. The two tables are linked based on SSN.

    In the EMPLOYEE_DETAIL table, there is a column called START_DATE and a column called END_DATE. Employees can move from one job to another, so there could be multiple records w/ the same SSN in this table.

    I'm trying to write a query that will find the branch number a person currently works at. There are two criteria to that -
    1) Find the Branch number that corresponds to the person's most recent START_DATE, but
    2) Only is their END_DATE is null.

    I'm thinking that a correlated query can do this, but I've never done one before. Here's what I hashed out - can someone tell me if this looks correct? I don't want to run it against my DB until I have a fair amount of confidence that it works right.

    Thanks in advance:

    strSQL = "SELECT D.BRANCH_NBR, " &_
    "D.START_DATE," &_
    "E.FIRST_NAME, " &_
    "E.LAST_NAME, " &_
    "E.SSN, " &_
    "Right(E.SSN,6) AS LoginID, " &_
    "Right(E.SSN,4) AS Pwd " &_
    "FROM EMPLOYEE_MASTER E INNER JOIN EMPLOYEE_DETAIL D ON E.SSN=D.SSN " & _
    "WHERE (D.START_DATE=(SELECT MAX(D2.START_DATE) FROM EMPLOYEE_DETAIL D2 WHERE D2.SSN = D.SSN) AND D.END_DATE IS NULL)
    MDW Guest

  2. Similar Questions and Discussions

    1. Access 2000 or SQL Server 2000
      Hi I am planning to design a database (destined for the web) that will have between 20000 and 45000 records in it and will receive a lot of reads...
    2. Error in COM+ while migrating from 2000 Server to 2000 Advace Server
      Hi, I have one 3 tier architecture application running on Windows 2000 Server. I have created replica of same machine on another machine where OS...
    3. Error while porting applicaition from 2000 Server to 2000 advace Server
      Hi, I have one 3 tier architecture application running on Windows 2000 Server. I have created replica of same machine on another machine where OS...
    4. Correlated Subquery or Distinct
      There is an index on Column1 in both tables A and B but no index on B.Column2. Yes I know an index on Column2 would help but because of the size...
    5. Query crashes Access 2000
      I am working on a Access database for a client that uses office 2000 on winXp, I use OfficeXP.on win2000. Using access 2000 fileformat. Everything...
  3. #2

    Default Re: SQL Server 2000 Correlated Query Question

    MDW wrote:
    > Hey all,
    >
    > Have another question. I've got a SQL Server 2000 DB that contains
    > employee information....we've got a table called EMPLOYEE_MASTER and
    > one called EMPLOYEE_DETAIL. The two tables are linked based on SSN.

    Hmmmm. You may wish to rethink this: SSN might not be unique. Do a Google
    search for more on this.
    >
    > In the EMPLOYEE_DETAIL table, there is a column called START_DATE and
    > a column called END_DATE. Employees can move from one job to another,
    > so there could be multiple records w/ the same SSN in this table.
    >
    > I'm trying to write a query that will find the branch number a person
    > currently works at. There are two criteria to that - 1) Find the
    > Branch number that corresponds to the person's most recent
    > START_DATE, but 2) Only is their END_DATE is null.
    Will there only be one record for an employee with a null END_DATE?
    >
    > I'm thinking that a correlated query can do this, but I've never done
    > one before. Here's what I hashed out - can someone tell me if this
    > looks correct? I don't want to run it against my DB until I have a
    > fair amount of confidence that it works right.
    >
    > Thanks in advance:
    >
    > strSQL = "SELECT D.BRANCH_NBR, " &_
    > "D.START_DATE," &_
    > "E.FIRST_NAME, " &_
    > "E.LAST_NAME, " &_
    > "E.SSN, " &_
    > "Right(E.SSN,6) AS LoginID, " &_
    > "Right(E.SSN,4) AS Pwd " &_
    > "FROM EMPLOYEE_MASTER E INNER JOIN EMPLOYEE_DETAIL D ON E.SSN=D.SSN "
    > & _ "WHERE (D.START_DATE=(SELECT MAX(D2.START_DATE) FROM
    > EMPLOYEE_DETAIL D2 WHERE D2.SSN = D.SSN) AND D.END_DATE IS NULL)
    It looks to me as if you are trying to create and test a query using ASP.
    This is not recommended. Always create and test your queries in your
    database's native query tool (Query Analyzer in your case) before attempting
    to run them from a client application (ASP). Using Query Analyzer will allow
    you to analyze and optimize the performance of your queries, something you
    would never do if writing your queries in ASP.

    SQL is so much easier to read without all the &'s, etc. Please post the
    actual query in the future instead of the vbscript code that is supposed to
    result in a SQL statement.

    Your attempt does not look very efficient to me. It always helps to break
    down your task into subtasks. Try this in Query Analyzer:

    This query will give you the "current" record for each employee (assuming
    there will be only one "current" record for each employee):
    SELECT SSN, BRANCH_NBR, START_DATE
    FROM EMPLOYEE_DETAIL
    WHERE END_DATE IS NULL

    If there could be multiple "current" records (i.e., records where the
    END_DATE is Null, then you will need to do this:

    First, get the most recent "current" start dates for each employee:
    SELECT SSN,Max(START_DATE) CurStartDate
    FROM EMPLOYEE_DETAIL
    WHERE END_DATE IS NULL
    GROUP BY SSN

    Test it to verify that it works correctly.
    Then use this query as one of the data sources in the next query:

    SELECT SSN, BRANCH_NBR, START_DATE
    FROM EMPLOYEE_DETAIL d INNER JOIN
    (SELECT SSN,Max(START_DATE) CurStartDate
    FROM EMPLOYEE_DETAIL
    WHERE END_DATE IS NULL
    GROUP BY SSN) q ON d.SSN = q.SSN AND
    d.START_DATE = q.START_DATE

    Again, test it in QA to verify it works as advertised.

    Now, it's simply a matter of adding the master table into the join to get
    the information required from the master table:

    SELECT e.SSN, d.BRANCH_NBR, d.START_DATE,
    E.FIRST_NAME, E.LAST_NAME, Right(E.SSN,6) AS LoginID,
    Right(E.SSN,4) AS Pwd
    FROM EMPLOYEE_MASTER E INNER JOIN
    EMPLOYEE_DETAIL d ON E.SSN=D.SSN
    INNER JOIN
    (SELECT SSN,Max(START_DATE) CurStartDate
    FROM EMPLOYEE_DETAIL
    WHERE END_DATE IS NULL
    GROUP BY SSN) q ON d.SSN = q.SSN AND
    d.START_DATE = q.START_DATE

    Try both versions of this query in QA to see which performs better. I
    strongly suggest creating a stored procedure from this query, and executing
    the stored procedure in ASP instead of using dynamic SQL.

    Bob Barrows

    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"


    Bob Barrows [MVP] Guest

  4. #3

    Default Re: SQL Server 2000 Correlated Query Question

    Run the query in the SQL query analyzer and check if you get correct
    results.

    Also, why not just query on the end date being null, and not worry about Max
    start date? Considering the details table contains only full-time jobs, a
    person can not have more than one job.


    --
    Manohar Kamath
    Editor, .netWire
    [url]www.dotnetwire.com[/url]


    "MDW" <MDW@discussions.microsoft.com> wrote in message
    news:4F3056B6-2D66-425E-A128-B7332BFB0162@microsoft.com...
    > Hey all,
    >
    > Have another question. I've got a SQL Server 2000 DB that contains
    employee information....we've got a table called EMPLOYEE_MASTER and one
    called EMPLOYEE_DETAIL. The two tables are linked based on SSN.
    >
    > In the EMPLOYEE_DETAIL table, there is a column called START_DATE and a
    column called END_DATE. Employees can move from one job to another, so there
    could be multiple records w/ the same SSN in this table.
    >
    > I'm trying to write a query that will find the branch number a person
    currently works at. There are two criteria to that -
    > 1) Find the Branch number that corresponds to the person's most recent
    START_DATE, but
    > 2) Only is their END_DATE is null.
    >
    > I'm thinking that a correlated query can do this, but I've never done one
    before. Here's what I hashed out - can someone tell me if this looks
    correct? I don't want to run it against my DB until I have a fair amount of
    confidence that it works right.
    >
    > Thanks in advance:
    >
    > strSQL = "SELECT D.BRANCH_NBR, " &_
    > "D.START_DATE," &_
    > "E.FIRST_NAME, " &_
    > "E.LAST_NAME, " &_
    > "E.SSN, " &_
    > "Right(E.SSN,6) AS LoginID, " &_
    > "Right(E.SSN,4) AS Pwd " &_
    > "FROM EMPLOYEE_MASTER E INNER JOIN EMPLOYEE_DETAIL D ON E.SSN=D.SSN " & _
    > "WHERE (D.START_DATE=(SELECT MAX(D2.START_DATE) FROM EMPLOYEE_DETAIL D2
    WHERE D2.SSN = D.SSN) AND D.END_DATE IS NULL)


    Manohar Kamath [MVP] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139