Results 1 to 17 of 17

Thread: SQL top

  1. #1

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

    SQL top

    Hi:

    I have this code in SQL,and I just want extract the 3 big's values from my database(ACcess) working in DAO....but the code extract all the values >='90' and not the only 3 big values >='90'

    something wrong with code?


    VB Code:
    1. sql1 = _
    2.      "select top 3 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 >= '90' & _
    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 >= '90' & _
    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 >= '90' & _
    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 >= '90' & _
    20.      "and (pontoseq1 = '' or pontoseq1 is null) " & _
    21.      " ORDER BY classe desc,Totais desc "

    thanks

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: SQL top

    The Select Top 3 is applied to the first query only. If you want to get the Top 3 from all queries in the union you will need to use a subquery. I am not sure if MSAccess supports subqueries.

    Code:
    sql1 = "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 >= '90' & _
         "and (pontoseq1 = '' or pontoseq1 is null) " & _
         " UNION ALL" & _
         " select pontosind2, classifind2, seccao2, classe2, designacao2, nomesocio, anilha2, gaiolaind2, sexo2" & _
         " from exposicao " & _
         " WHERE pontosind2 >= '90' & _
         "and (pontoseq1 = '' or pontoseq1 is null) " & _
         " UNION ALL" & _
         " select pontosind3, classifind3, seccao3, classe3, designacao3, nomesocio, anilha3, gaiolaind3, sexo3" & _
         " from exposicao " & _
         " WHERE pontosind3 >= '90' & _
         "and (pontoseq1 = '' or pontoseq1 is null) " & _
         " UNION ALL" & _
         " select pontosind4, classifind4, seccao4, classe4, designacao4, nomesocio, anilha4, gaiolaind4, sexo4" & _
         " from exposicao " & _
         " WHERE pontosind4 >= '90' & _
         "and (pontoseq1 = '' or pontoseq1 is null)) " & _
         " ORDER BY classe desc,Totais desc "

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

    Re: SQL top

    I also do not use ACCESS, but if you have a problem with the SUBQUERY, maybe make a VIEW of the QUERY with the UNION's and then refer to that VIEW with the SELECT TOP 3...

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

    can you verify the code...I Got a error in the syntax

    Thanks

  5. #5
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: SQL top

    What's the description of the error raised?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: SQL top

    You could paste it into Access and execute it. It should give a descriptive error message.

  7. #7

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

    Re: SQL top

    you can try with the attached project
    Attached Files Attached Files

  8. #8
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: SQL top

    You just missed some & and "...

    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 >= '90'" & _
    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 >= '90'" & _
    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 >= '90'" & _
    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 >= '90'" & _
    20.      " and (pontoseq1 = '' or pontoseq1 is null)) " & _
    21.      " ORDER BY classe desc,Totais desc "
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  9. #9

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

    Re: SQL top

    yes you are right...now the code work,but the "top 3" don't extract the desired values because the top 3 extract only 3 recordsets and not all the 3 big recordsets....strange!!!!!!!


    Can you see the project attached,and run it,and change the top 3 for top 10?


    Thanks
    Attached Files Attached Files

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

    Re: SQL top

    You are missing an ORDER BY outside the subquery...

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

    Without that you are not going to get the proper top 3 - numerically highest.

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

  11. #11

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

    Re: SQL top

    sory but I don't understand...I have a order outside of the subquery...I Think!!!!

    do you have see the project?

    Thnaks

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

    Re: SQL top

    Yes - I do see that you have an ORDER BY...

    But you are ordering by the CLASS DESCRIPTION - right? So you are getting the alphabetically first 3 classes - is that what is happening?

    Don't you want the 3-highest grades?

    ORDER BY TOTAIS should do that for you...

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

  13. #13

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

    Re: SQL top

    But you are ordering by the CLASS DESCRIPTION - right? So you are getting the alphabetically first 3 classes - is that what is happening?
    yes that correct...I get the first 3 classes alphabetically

    Don't you want the 3-highest grades?
    yes I want the highest 3 grades but ordering by classe and totais...I want the 3 highest values ordering by classe and listening by totais....but the 3 highest values should be By totais not classe

    That's possible do this????

  14. #14
    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

  15. #15

    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

  16. #16
    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

  17. #17

    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