a question about simple sp

  • Hi

    where is my error?

    somthing wrong is happened near UNION !!!!

    select Top 1 *

    from News

    where GeneralSport=1 and Type_ID=1

    order by News_ID

    UNION

    select Top 1 *

    from News

    where GeneralSport=1 and Type_ID=1

    order by News_ID Desc

  • You cannot have two ORDER BY statements in a single UNION query.

    select Top 1

    from News

    where GeneralSport=1 and Type_ID=1

    order by News_ID

    UNION

    select Top 1 *

    from News

    where GeneralSport=1 and Type_ID=1

    order by News_ID Desc

    What are you trying to do? It seems like you're trying to find the MIN() and MAX() value. Why not using something like the following?

    ; WITH cte AS

    (

    SELECT *,

    ROW_NUMBER() OVER(ORDER BY News_ID) AS min_id,

    ROW_NUMBER() OVER(ORDER BY News_ID DESC) AS max_id

    FROM News

    WHERE GeneralSport=1 and Type_ID=1

    )

    SELECT *

    FROM cte

    WHERE min_id=1 OR max_id=1

    As a side note: I strongly recommend to replace SELECT * with the list of named columns.



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

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

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