Group by - 2

Posted on 2010-01-04
Last Modified: 2012-05-08
I have this table
Id    AId   BId  
1     1       1
2     1       1
3     1       2
4     1       2
5     2       1
6     2       2
From this table I would like to group by 1) AId   2) BId
So that I would get returned:
AId    BId
1        1
1         2
2         1
2         2
How can I do this?    
Question by:johnkainn
    LVL 41

    Accepted Solution

      FROM YourTable
      GROUP BY AId, BId
    LVL 75

    Assisted Solution

    by:Aneesh Retnakaran
    select distinct AId ,  BId from tableName

    SELECT AID, bID from tableName GROUP BY AId ,  BId
    LVL 9

    Assisted Solution

    take the decision on which column you should group.

    SELECT AID, bID FROM T_group
    GROUP BY AId ,  BId
    LVL 24

    Assisted Solution

    by:DBAduck - Ben Miller
    All the above will work.  Just so it is stated in this question, if you do not specify an aggregate in the SELECT (like SUM, AVG, COUNT, etc) then the GROUP BY will do a DISTINCT.  Which is the same as just saying SELECT DISTINCT with the columns you want to have in the output.

    This is what aneeshattingal stated.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
    In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now