Percentage with group by

  • I have a table which contains the fields:

    Plancode

    Startdate

    MemberNumber

    I need to have a query which will display the counts of each plancode, the

    percentage of each plan relative to the whole table ( all the plancodes) , and the plancode

    in a single result set.  Any help out there?

  • Select count(*),

           ((Select count(*) From YourTable Where PlanCode = YT.plancode) /

            (Select count(*) From YourTable) * 100 ) as [Percent],

     PlanCode

    From YourTable YT

    Group by PlanCode

    There's gotta be a better way

     

     

  • declare @allplancode int

    select @allplancode = count(*) from plancode

    select plancode, count(*) as plancodecnt , 100.0 * count(*) / @allplancode as percent

     from thetable

     group by plancode

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

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