select H,sum(case cast(H as int) % 2 when 0 then M+(select M from t where H='偶数') else M+(select M from t where H='奇数') end) as M from t where H<>'偶数' and H<>'奇数' group by H
上面是一条的情况不止一条就这样 select H,sum(case cast(H as int) % 2 when 0 then M+(select sum(M) from t where H='偶数') else M+(select sum(M) from t where H='奇数') end) as M from t where H<>'偶数' and H<>'奇数' and isnumeric(H)>0 group by H
那就这样了select H,sum(case cast(H as int) % 2 when 0 then M+(select sum(M) from t where H='偶数') else M end) as M from t where H<>'偶数' and isnumeric(H)>0 group by H
select a.H,M=sum(case when (a.H % 2)=0 then a.M+b.M when (a.H % 2)<>0 then a.M+b.M end) from test a,(select M=sum(M) from test where H='偶数')b,(select M=sum(M) from test where H='奇数')c where isnumeric(H)>0 group by a.H go
select h.h,bb=isnull(i.aa,0) from (select aa=sum(m),h=case when isnumeric(i.h)>0 then h when i.h='偶数' then 10 (这里有问题) end from i group by h) i right join h on h.h=i.h order by bb desc 我这样写了一条语句 但是有一个问题就是 如果i.h=偶数的时候 那么 =应该同时等于 2 4 6 8 10 一直到100 但是我现在只会 when i.h='偶数' then 10
这个只是处理偶数,其他的规则的要不要处理? select a.h,sum(case cast(a.h as int) % 2 when 0 then isnull(M,0)+(select sum(M) from I where H='偶数') else isnull(M,0) end) as M from H a left join I b on a.h=b.h where group by H 看看对不对?
加的数 是从 M字段提取出来的
应该是 如果H字段内容是“偶数” 则从1-100中选择 这个偶数是中文的 直接输入偶数 则分别给1-100中的偶数 + 对应的M 数值
when 0 then M+(select M from t where H='偶数')
else M+(select M from t where H='奇数')
end) as M
from t
where H<>'偶数' and H<>'奇数'
group by H
select H,sum(case cast(H as int) % 2
when 0 then M+(select sum(M) from t where H='偶数')
else M+(select sum(M) from t where H='奇数')
end) as M
from t
where H<>'偶数' and H<>'奇数' and isnumeric(H)>0
group by H
h_id h
自动 1-100 个数字I表
id h m
自动 1-100或者中文 数字
1 1 100
2 1 200
3 2 300
4 3 500
5 偶数 55
6 尾0 5000
7 9 1000
8 合偶 5000
-----------------------------------
想要的效果应该是这样的 提取H 表中 1-100个数字做显示
先累计出 I 表中 H字段相同数字的 累计数 保证每个号码只显示出一个总数(计算来源在M字段)
然后根据I表中的总数最大做排序规则假如不算 I表 H字段 中的中文字符的话 结果应该是这样的
9 1000
3 500
2 300
1 300
但现在要求的是假如 H字段中 出现 偶数 那么就要把1-100中的所有偶数都加一次M
比如就刚才的结果 有一条H=偶数 M=50 的记录 那么应该是
9 1000
3 500
2 350
1 300
4 50
6 50
8 50
类推到100
有很多种算法 但理论上都是一样 就是如果H是中文 那么就对应同等条件下的所有1-100数字.
when 0 then M+(select sum(M) from t where H='偶数')
else M
end) as M
from t
where H<>'偶数' and isnumeric(H)>0
group by H
when (a.H % 2)<>0 then a.M+b.M end)
from test a,(select M=sum(M) from test where H='偶数')b,(select M=sum(M) from test where H='奇数')c
where isnumeric(H)>0
group by a.H
go
你说那个4 对应的是H 表中的 1-100中的一条记录 比如我这样输入一条新记录
操作I表
偶数 50 我这样输入。那么显示的时候就显示
2 50
4 50
6 50
类推到H表末尾
这个 2、4、6 是来自H表中的 显示结果
(select aa=sum(m),h=case
when isnumeric(i.h)>0 then h
when i.h='偶数' then 10 (这里有问题)
end
from i group by h) i
right join h on
h.h=i.h
order by bb desc 我这样写了一条语句
但是有一个问题就是
如果i.h=偶数的时候 那么 =应该同时等于 2 4 6 8 10 一直到100
但是我现在只会
when i.h='偶数' then 10
我可以在QQ上给你看我的数据库结构 和运行结果 预期运行结果。
select a.h,sum(case cast(a.h as int) % 2
when 0 then isnull(M,0)+(select sum(M) from I where H='偶数')
else isnull(M,0)
end) as M
from H a left join I b on a.h=b.h
where
group by H
看看对不对?