select statement

  • Hi, I have a table named test. The table have this records:

    Name: Numbers:

    pedro 10

    pedro 11

    paulo 5

    ana 7

    ana 8

    ana 9

    What i want is to make a select that will display a count for each equal name:

    Pedro 10 2

    pedro 11 2

    paulo 5 1

    Ana 7 3

    ana 8 3

    ana 9 3

    It will count the number of times that a name is equal.

    how can i make the select?

    Thank you.

  • Inline correlated subquery is one way.

    Without knowing your table structure, I can't really provide sample code for how to do it.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • This somewhat looks like homework, so I'll give you some hints here.

    I'd build an aggregate query that uses COUNT() to determine how many times each name appears. That it a very simple aggregate. Then make that a subquery and join it back to the original table on names, and join in the count from that subquery to get that value.

  • table structure is:

    Name, numbers

    Only this.

  • slightly different than the correlated subquery is the option of using a CTE. Use the cte to join back to the original table to get the results you are seeking.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

Viewing 5 posts - 1 through 4 (of 4 total)

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