Results 1 to 3 of 3

Thread: IN query

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    69

    IN query

    Hi All,

    How can I better replace the IN(....) to a better code?

    SELECT TOP 1 MIN(table.Item), MIN(table.Post), MIN(table.add), MIN(table.creator), MIN(table.datecreated) FROM table WITH (NOLOCK) WHERE table.code IN('0012','0013','0014').

    The above has causes some performance and system run time error issue especially within the IN bracket the table code gets very large. Please advise.

    Thanks.
    Last edited by skyseh; May 11th, 2006 at 04:16 AM.

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: IN query

    If this is actual range of codes then:

    WHERE table.code Between '0012' and '0014'

    But I also cannot understand how that query will run at all - MIN() aggregate functions mixed with TOP 1 makes no sense.

    The query should be:

    Code:
    SELECT MIN(table.Item), MIN(table.Post)
    , MIN(table.add), MIN(table.creator), MIN(table.datecreated)
    FROM table WITH (NOLOCK)
    WHERE table.code Between '0012' and '0014'
    btw - is table.code a INDEX column on your table? How many rows in the table?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: IN query

    Also dont forget that you can nest your Select statements. You can use a select statement to select the numbers you want in your IN statement....
    Code:
    SELECT ... FROM table WHERE Something IN (SELECT IDS From ... Where ... etc).
    As long as the nested select contains the data that you are wanting in your IN statement, then it should work. In the above, this means as long as the nested select selects just the numbers...

Posting Permissions

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



Click Here to Expand Forum to Full Width