2012年10月19日 星期五

MS-SQL 好的統計方法

引用來源
---
 create table #temp(
    person_id int,
    work_year int
)

insert into #temp(person_id,work_year)
select 2110020 ,4  union
select 2110067 ,5  union
select 2112274 ,6  union
select 2113775 ,7  union
select 2113841 ,8  union
select 2114508 ,9  union
select 2114870 ,10 union
select 2115337 ,11 union
select 2115408 ,7  union
select 2115431 ,5 union
select 2115432 ,6;

select distinct work_year as 工作年限,
    count(work_year) over(partition by work_year) as 工作年限人数,
    count(person_id) over () 总人数,
    cast(
        cast(
            (count(work_year) over(partition by work_year))
            /
            cast((count(person_id) over ()) as decimal(18,2))*100  as int)
        as varchar(50)
    ) + '%' as 占比
from #temp
order by 工作年限

truncate table #temp
drop table #temp


(11 row(s) affected)
工作年限        工作年限人数      总人数         占比
----------- ----------- ----------- ---------------------------------------------------
4           1           11          9%
5           2           11          18%
6           2           11          18%
7           2           11          18%
8           1           11          9%
9           1           11          9%
10          1           11          9%
11          1           11          9%

(8 row(s) affected)

沒有留言:

張貼留言