sql query

  • I have a database table CustomerGroup which has a column GroupId which is a primary key columnn

    In another database table Customerdtl I have columns GroupId(foreign key in this table) and

    CustRpt(primary key in this table)

    (that is each different CustRpt can be associated with a same GroupId)

    How can I make a select query which will return me 2 columns where in the first column I want all the unique values

    of GroupId and in the second column I want the count of the CustRpt for that GroupId

    How do I go about doing this?

    Thanks

     

  • I think you are looking for this gropby query.

    select GroupId, count(*) as [Count] from Customerdtl

    group by GroupId

  • In case there are Groups with no members (no corresponding entry in Customerdtl) and you want to display these as well :

    SELECT g.GroupId, count(c.CustRpt)

    FROM CustomerGroup g

    LEFT JOIN Customerdtl c ON c.GroupId = g.GroupId

    GROUP BY g.GroupId

    HTH, Vladan

Viewing 3 posts - 1 through 2 (of 2 total)

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