Results 1 to 4 of 4

Thread: [RESOLVED] Simple question (Group By)

  1. #1

    Thread Starter
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Resolved [RESOLVED] Simple question (Group By)

    Hi everybody !

    I want to Select all fields and group by one field, like the code below, but I got an error ?

    I dont want to write all fields because there are too many...Is there a way to do that ?
    VB Code:
    1. strSQL = "SELECT * FROM Items WHERE BComm = ?" & " GROUP BY BCommNo"
    Thanks in advance !
    DubweiserTM

    If your question has been answered, you can mark a thread as resolved...

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Simple question (Group By)

    Moved to correct forum

    For a Group By every field in the Select list needs to either be in the Group By list, or be a calculated value in the Select list instead (eg: Sum(Field2)).

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

    Re: [RESOLVED] Simple question (Group By)

    Although I usually steer away from DISTINCT and usually tell users not to SELECT * (but instead put the column names)...

    You could say:

    SELECT DISTINCT * FROM SOMETABLE WHERE SOMECOL=123

    Although why you would want to do that is beyond me

    *** 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
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: [RESOLVED] Simple question (Group By)

    Quote Originally Posted by szlamany
    . . . and usually tell users not to SELECT * (but instead put the column names). . .
    I agree, unless you are going to use each and every column in the table (probably a very remote possibility) then only bring back the columns you need. This will reduce any overhead you may incur especially if you are bring back a lot of data.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


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