Results 1 to 17 of 17

Thread: SQL top

Hybrid View

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

    Re: SQL top

    Well since you are already going down the path of sub-query...

    You need to do the TOP 3/ORDER BY TOTAIS - there is no way around that.

    If you want them to then be ordered by CLASS DESCRIPTION, then I guess you put that in another SUBQUERY - order by class description...

    Code:
    SELECT * FROM (SELECT TOP 3 * FROM (...) ORDER BY TOTAIS DESC) 
    ORDER BY CLASSE

    *** 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

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    Re: SQL top

    Maybe like this,but the sort are not right,unless the syntax are not correct

    VB Code:
    1. sql1 = "Select Top 3 * From ( " & _
    2.      "select pontosind1 as Totais, classifind1 as Classifind, seccao1 as Secção, classe1 as Classe, designacao1 as Designação, nomesocio as Expositor, anilha1 as Anilha, gaiolaind1 as Gaiola, Sexo1 as Sexo" & _
    3.      " from exposicao " & _
    4.      " WHERE pontosind1 >= '88'" & _
    5.      " and (pontoseq1 = '' or pontoseq1 is null) " & _
    6.      " UNION ALL" & _
    7.      " select pontosind2, classifind2, seccao2, classe2, designacao2, nomesocio, anilha2, gaiolaind2, sexo2" & _
    8.      " from exposicao " & _
    9.      " WHERE pontosind2 >= '88'" & _
    10.      " and (pontoseq1 = '' or pontoseq1 is null) " & _
    11.      " UNION ALL" & _
    12.      " select pontosind3, classifind3, seccao3, classe3, designacao3, nomesocio, anilha3, gaiolaind3, sexo3" & _
    13.      " from exposicao " & _
    14.      " WHERE pontosind3 >= '88'" & _
    15.      " and (pontoseq1 = '' or pontoseq1 is null) " & _
    16.      " UNION ALL" & _
    17.      " select pontosind4, classifind4, seccao4, classe4, designacao4, nomesocio, anilha4, gaiolaind4, sexo4" & _
    18.      " from exposicao " & _
    19.      " WHERE pontosind4 >= '88'" & _
    20.      " and (pontoseq1 = '' or pontoseq1 is null)" & _
    21.      [B]" ORDER BY totais desc) " & _
    22.      " ORDER BY classe desc [/B] "


    thanks

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

    Re: SQL top

    I said to add another sub-query - not sort the "primary sub-query".

    Code:
    sql1 = "Select * From (Select Top 3 * From ( " & _
         "select pontosind1 as Totais, classifind1 as Classifind, seccao1 as Secção, classe1 as Classe, designacao1 as Designação, nomesocio as Expositor, anilha1 as Anilha, gaiolaind1 as Gaiola, Sexo1 as Sexo" & _
         " from exposicao " & _
         " WHERE pontosind1 >= '88'" & _
         " and (pontoseq1 = '' or pontoseq1 is null) " & _
         " UNION ALL" & _
         " select pontosind2, classifind2, seccao2, classe2, designacao2, nomesocio, anilha2, gaiolaind2, sexo2" & _
         " from exposicao " & _
         " WHERE pontosind2 >= '88'" & _
         " and (pontoseq1 = '' or pontoseq1 is null) " & _
         " UNION ALL" & _
         " select pontosind3, classifind3, seccao3, classe3, designacao3, nomesocio, anilha3, gaiolaind3, sexo3" & _
         " from exposicao " & _
         " WHERE pontosind3 >= '88'" & _
         " and (pontoseq1 = '' or pontoseq1 is null) " & _
         " UNION ALL" & _
         " select pontosind4, classifind4, seccao4, classe4, designacao4, nomesocio, anilha4, gaiolaind4, sexo4" & _
         " from exposicao " & _
         " WHERE pontosind4 >= '88'" & _
         " and (pontoseq1 = '' or pontoseq1 is null))" & _
         " ORDER BY totais desc) " & _
         " ORDER BY classe desc  "

    *** 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

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2004
    Posts
    1,414

    Re: SQL top

    I think this is a big problem....the code don't extract the desired values

    If you can try the project please...the code work but don't extract all values

    Thanks
    Attached Files Attached Files

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