simple question

  • Hi

    i have this sp,

    i have a problem with its result, i want to have 3 records in result order by "Type_ID"but it gives me order by "News_ID"would you please help me?

    select *

    from News inner join NewsType on NEWS.Type_ID=NewsType.Type_ID

    where News_ID in (select MAX(News_ID) from News where Type_ID=1 and GeneralSport=0)

    UNION

    select *

    from News inner join NewsType on NEWS.Type_ID=NewsType.Type_ID

    where

    News_ID in (select MAX(News_ID) from News where Type_ID=2 and GeneralSport=0 )

    union

    select *

    from News inner join NewsType on NEWS.Type_ID=NewsType.Type_ID

    where

    News_ID in (select MAX(News_ID) from News where Type_ID=3 and GeneralSport=0 )

  • Without seeing the tables, data, or requirements this is difficult.

    Try this: -

    SELECT *

    FROM (SELECT *

    FROM news

    INNER JOIN newstype ON news.type_id = newstype.type_id

    WHERE news_id IN (SELECT MAX(news_id)

    FROM news

    WHERE type_id = 1 AND generalsport = 0)

    UNION

    SELECT *

    FROM news

    INNER JOIN newstype ON news.type_id = newstype.type_id

    WHERE news_id IN (SELECT MAX(news_id)

    FROM news

    WHERE type_id = 2 AND generalsport = 0)

    UNION

    SELECT *

    FROM news

    INNER JOIN newstype ON news.type_id = newstype.type_id

    WHERE news_id IN (SELECT MAX(news_id)

    FROM news

    WHERE type_id = 3 AND generalsport = 0))

    ORDER BY type_id


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • I started write a refutation of just about everything Joe wrote, because he's wrong at just about every level, even his use of English, but then I decided not to bother. Too much work to point out all the errors, too little payoff.

    So, I'll just give my usual, "Please ignore the rabid chimpanzee in the corner. It means well, but it is, after all, a rabid chimp."

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • GSquared (8/1/2011)


    I started write a refutation of just about everything Joe wrote, because he's wrong at just about every level, even his use of English, but then I decided not to bother. Too much work to point out all the errors, too little payoff.

    So, I'll just give my usual, "Please ignore the rabid chimpanzee in the corner. It means well, but it is, after all, a rabid chimp."

    LMAO!!! Gus, I too started writing a nasty gram but decided it wasn't worth it. 😛

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Heh... Joe who? 😉

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 5 posts - 1 through 4 (of 4 total)

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