当前位置:编程学习 > C#/ASP.NET >>

特殊条件一求平均值,并根据所值的总和进行排列

表如下:
将 jb=1 时,得出的分数除人数的平均值 乘以 0.3,
将 jb不等于1 时,得出的 分数除人数的平均值 乘以 0.7,
最终将以上两值相加所得的值的表按倒序排列
我做的是
aa ="select cons,avg(num)*0.3 from kao_r_List where jb='1' group by cons order by avg(num)*0.3 desc"; //得出jb=1的值 *0.3

bb = "select cons,avg(num)*0.7 from kao_r_List where jb in ('2','3','4','5') group by cons order by avg(num)*0.7 desc";//得出jb不等于1的值 *0.7

如 jb=1 时人数为0, 则 aa=0
如 jb!=1 时人数为0, 则 bb=0

怎么把以上的两个语句 综合成一个语句
求教!!!!
没有分了,希望不要嫌弃。

平均值,总和排列,多条件,sum  --------------------编程问答-------------------- 表如下
jb       cons      num
2         史        10
4         张        18
3         宁        9
1         林        18
2         马        16
1         宁        29 --------------------编程问答-------------------- 用case when

select cons, 
    set 字段=
    case 
        when jb='1' then avg(num)*0.3 
        else avg(num)*0.7 
    end
from kao_r_List ... --------------------编程问答--------------------

create table #tb(jb int,cons varchar(10),num int)
insert into #tb
select 2,'史',10
union all select 4,'张',18
union all select 3,'宁',9
union all select 1,'林',18
union all select 2,'马',16
union all select 1,'宁',29
go

select *
from 
(
select cons,avg(num)*0.3 as cavg
from #tb where jb='1' group by cons 
union all
select cons,avg(num)*0.7 
from #tb where jb<>'1' group by cons 
)t
order by cavg

/*
cons cavg
--------------------------
林 5.4
宁 6.3
史 7.0
宁 8.7
马 11.2
张 12.6

*/
--------------------编程问答-------------------- 用union all 就可以 --------------------编程问答--------------------
引用 3 楼 hdhai9451 的回复:

create table #tb(jb int,cons varchar(10),num int)
insert into #tb
select 2,'史',10
union all select 4,'张',18
union all select 3,'宁',9
union all select 1,'林',18
union all select 2,'马',16
union all select 1,'宁',29
go

select *
from 
(
select cons,avg(num)*0.3 as cavg
from #tb where jb='1' group by cons 
union all
select cons,avg(num)*0.7 
from #tb where jb<>'1' group by cons 
)t
order by cavg

/*
cons cavg
--------------------------
林 5.4
宁 6.3
史 7.0
宁 8.7
马 11.2
张 12.6

*/

可以用,非常感谢! --------------------编程问答--------------------
引用 3 楼 hdhai9451 的回复:

create table #tb(jb int,cons varchar(10),num int)
insert into #tb
select 2,'史',10
union all select 4,'张',18
union all select 3,'宁',9
union all select 1,'林',18
union all select 2,'马',16
union all select 1,'宁',29
go

select *
from 
(
select cons,avg(num)*0.3 as cavg
from #tb where jb='1' group by cons 
union all
select cons,avg(num)*0.7 
from #tb where jb<>'1' group by cons 
)t
order by cavg

/*
cons cavg
--------------------------
林 5.4
宁 6.3
史 7.0
宁 8.7
马 11.2
张 12.6

*/

可是这个不能去重啊,宁这个人就只要出现一次 --------------------编程问答-------------------- 我在求出同一个人在既有1的的情况下的平均分,又有不在1的情况下的平均分的总和进行排列 --------------------编程问答--------------------
引用 2 楼 caozhy 的回复:
用case when

select cons, 
    set 字段=
    case 
        when jb='1' then avg(num)*0.3 
        else avg(num)*0.7 
    end
from kao_r_List ...


这个代码不知道 楼主为什么不用,奇了怪了 --------------------编程问答--------------------
引用 8 楼 u010361303 的回复:
Quote: 引用 2 楼 caozhy 的回复:

用case when

select cons, 
    set 字段=
    case 
        when jb='1' then avg(num)*0.3 
        else avg(num)*0.7 
    end
from kao_r_List ...


这个代码不知道 楼主为什么不用,奇了怪了

我不明白“字段=”这是什么意思 --------------------编程问答-------------------- 这个字段是 设置变量,是什么样的,是sum() as www
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,