日期:2014-05-17  浏览次数:20686 次

发个SQL 看看大家的SQL语句谁更简单
SELECT person_id, work_year FROM info_person ip

---结果 person_id int,work_year int
2110020 4
2110067 5
2112274 6
2113775 7
2113841 8
2114508 9
2114870 10
2115337 11
2115408 7
2115431 5
2115610 6

需要对不同工作年限的个人进行统计 如下:

工作年限 数量 占比  
1 15000 15.00%
2 15222 16.00% 

请给出你认为够简单的SQL语句!


------解决方案--------------------
SQL code


select count(*) from info_person where work_year =“+work_year +”

------解决方案--------------------
写到存储过程中吧,先获得sum值,然后select xxx / sum值
------解决方案--------------------
还是写个存储过程比较好,把所有的工作年龄遍历一遍
------解决方案--------------------
SQL code

     Select work_year AS 工作年限,Count(work_year) AS 数量,(Count(work_year)/Count(*)) AS 占比 from info_person GROUP BY work_year

------解决方案--------------------
select work_year,count(*),count(*)*1./num from 
 (select *,count(*)over() from info_person) t group by work_year,num
------解决方案--------------------

SQL code

 Select work_year AS 工作年限,Count(work_year) AS 数量,(Count(work_year)/Count(*)) AS 占比 from info_person GROUP BY work_year

------解决方案--------------------
你这num是什么,根本不识别。

select work_year,count(work_year) as nmber, (COUNT(work_year)/(select COUNT(*) from info_person )) as rate from info_person ip 
group by work_year

比率怎么算啊
------解决方案--------------------
SQL code
Select work_year,Count(work_year) AS Quantity,STR(Convert(Decimal(10,2),(Count(work_year)*100.0/(Select Count(person_id) from info_person))),10,2)+'%' AS Proprotion from info_person Group by work_year

------解决方案--------------------
探讨

引用:

SQL code
Select work_year,Count(work_year) AS Quantity,STR(Convert(Decimal(10,2),(Count(work_year)*100.0/(Select Count(person_id) from info_person))),10,2)+'%' AS Proprotion from in……

------解决方案--------------------
over函数
SQL code

 CREATE TABLE  WBT
(
    person_id int,
    work_year  int
)
INSERT INTO WBT
SELECT 2110020,5 UNION ALL 
SELECT 2112274,6 UNION ALL 
SELECT 2113775,7 UNION ALL 
SELECT 2113841,8 UNION ALL 
SELECT 2114508,9 UNION ALL 
SELECT 2114870,10 UNION ALL 
SELECT 2115337,11 UNION ALL 
SELECT 2115408,7 UNION ALL 
SELECT 2115431,5 UNION ALL 
SELECT 2115610,6 

select work_year as 工作年限 ,count(*) as 数量,convert(nvarchar(10),convert(decimal(18,2),count(*)*100.00/rowno))+'%' as 所占比例 from  
 (select *,count(*)over() rowno from WBT) t group by work_year,rowno
 /*
 工作年限        数量          所占比例
----------- ----------- -----------
5           2           20.00%
6           2           20.00%
7           2           20.00%
8           1           10.00%
9           1           10.00%
10          1           10.00%
11          1           10.00%

(7 行受影响
 */

------解决方案--------------------
改了一下:
SQL code

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_yea