-3

This is what my table looks like

Measure    PT Name
Expired    Mickey Mouse
Expired    Donald Duck
Alive      Big Bird
Alive      Baby Bird
Rehab      Daffy Duck
Xray       Goofy

I want it to look like this:

Measure   Count  %
Expired   15     40%
Alive     5      13%
Rehab     7      18%
Xray      10     27%

I made a mistake, I want to get a count grouped by Measure including a % all in the same SQL statement

Select DischargeDisp, Count(*) as TotalCnt
INTO #Measure
from #LOS
group by DischargeDisp
sqluser
  • 5,502
  • 7
  • 36
  • 50

5 Answers5

0

is this what you wanted ?

Select Measure, Count(*), Count(*) * 100 / SUM ( COUNT (*) ) OVER()
from   #LOS
group by Measure
Squirrel
  • 23,507
  • 4
  • 34
  • 32
0
select Measure
      ,SUM(m_count) m_count
      ,(sum(m_count)*100/(select sum(m_count) from your_table)) as Percentage
      from your_table group by Measure;

Changed column name count to m_count. As it creates confusion.

Ravi
  • 30,829
  • 42
  • 119
  • 173
0
SELECT Measure,
       SUM(Cnt) AS [Count],
       CAST(SUM(Cnt) * 100 / (SELECT SUM(Cnt) FROM #LOS) AS VARCHAR(10)) + '%' AS [%]
FROM #LOS
GROUP BY Measure
sqluser
  • 5,502
  • 7
  • 36
  • 50
  • While this code may answer the question, providing additional context regarding *why* and/or *how* this code answers the question improves its long-term value. – Benjamin W. Mar 27 '16 at 04:57
0

Your questions keeps changing but maybe this will meet your need:

Select Measure, Sum(1) as 'Count', LTrim(Str(Sum(1) * 100 / (Select Count(*) From Measure))) + '%' as [Percent]
  from Measure
  Group by Measure
order by Measure
0

you can use this:

select distinct Measure, 
       count(*) over (partition by measure) as [Count],
       format(convert(float,count(*) over (partition by measure)) /count(*) over(),'p') as [%]
from los

test is here

Vasily
  • 5,707
  • 3
  • 19
  • 34