问题题面没有看懂,"按照d排序在用 s和m依次比较"表达个什么意思不明白.
解决方案 »
- to 【ws_hgo】 谢谢。
- 一个表关联2次,急!
- 复制中一个比较严重的问题(请各位大哥帮帮忙,小妹没有多少分了,邹大哥,我可是你的忠实的FASN哦)
- 我想把where 语句, 改为用exists的语句, 为什么下面的不可以. 应该怎样改?
- SQL2008老是报【不支持用该后端版本设计数据库关系图或表】
- 一个初学者的问题???
- 请教这个语句有何错误?
- 在写存储过程中有没有分隔函数?
- 哎呀,装上oracle 不知道怎么进去 帮帮小妹吧:)
- 用sql模拟:有12支乒乓球,其中一只是假的,其余的是真的,用天平秤来称重,你能用此天平秤秤3次
- MSSQL如何定义跨数据库的外键引用。
- 如何让一个数据库只能被sa访问
上面有两个表@a,@b,大家都能看出来共同的字段是 a,b
但是下面的@b表中字段还有c,d
操作过程为
以a,b为连接条件将两表连接,明显看出在@b表中的数据会有多条
然后以@b标为基准把两表中a,b字段相同的数据依次按照d字段降序从大到小,逐条进行比较
最后小于0的去掉。
举例 @a表 中
a b s
----------- ----------- -----------
1 2 20
表@b中 与其相关的数据为a b c d m
----------- ----------- ----------- ----------- -----------
1 2 3 9 5
1 2 4 10 12
1 2 5 11 10
1 2 6 12 5按照d降序@b中数据为
a b c d m
----------- ----------- ----------- ----------- -----------
1 2 3 12 5
1 2 4 11 10
1 2 5 10 12
1 2 6 9 5
组条比较用@a中的s和@b中的m比较
则第一条 20>5第一条保留
比较第二条 15>10 第二条保留
比较第三条 5<12 第三条仍然把保留 但是m数量标为5
第四条 就直接不用要了结果就是下面a b c d m
----------- ----------- ----------- ----------- -----------
1 2 3 12 5
1 2 4 11 10
1 2 5 10 5
上面有两个表@a,@b,大家都能看出来共同的字段是 a,b
但是下面的@b表中字段还有c,d
操作过程为
以a,b为连接条件将两表连接,明显看出在@b表中的数据会有多条 然后以@b标为基准把两表中a,b字段相同的数据依次按照d字段降序从大到小,逐条进行比较
最后小于0的去掉。
举例 @a表 中
a b s
----------- ----------- -----------
1 2 20
表@b中 与其相关的数据为 a b c d m
----------- ----------- ----------- ----------- -----------
1 2 3 9 5
1 2 4 10 12
1 2 5 11 10
1 2 6 12 5 按照d降序@b中数据为
a b c d m
----------- ----------- ----------- ----------- -----------
1 2 3 12 5
1 2 4 11 10
1 2 5 10 12
1 2 6 9 5
组条比较用@a中的s和@b中的m比较
则第一条 20>5第一条保留
比较第二条 15>10 第二条保留
比较第三条 5 <12 第三条仍然把保留 但是m数量标为5
第四条 就直接不用要了 结果就是下面 a b c d m
----------- ----------- ----------- ----------- -----------
1 2 3 12 5
1 2 4 11 10
1 2 5 10 5
declare @b table (a int,b int,c int,d int,m int)
insert @a
select 1,2,20
union all
select 2,3,15insert @b
select 1,2,3,9,5
union all
select 1,2,4,10,12
union all
select 1,2,5,11,10
union all
select 1,2,6,12,5
union all
select 2,3,4,6,10
union all
select 2,3,5,7,5
union all
select 2,3,6,8,10select * from @a
select * from @b-- drop table #temp
-- select * from #tempselect * from (
select n1.a,n1.b,n1.c,n1.d
,(case when
((select
sum(s)
from @a
where a=n1.a and b=n1.b
and a=n2.a and b=n2.b
)-
isnull((select
sum(m) from @b
where a=n1.a and b=n1.b
and a=n2.a and b=n2.b
and c<=n1.c
and d<=n1.d
),0))>=0
then n1.m
else
case when n1.m +((select
sum(s) from @a
where a=n1.a and b=n1.b
and a=n2.a and b=n2.b
) -
isnull((select
sum(m) from @b
where a=n1.a and b=n1.b
and a=n2.a and b=n2.b
and c<=n1.c
and d<=n1.d
),0))>=0
then n1.m +((select
sum(s) from @a
where a=n1.a and b=n1.b
and a=n2.a and b=n2.b
) -
isnull((select
sum(m) from @b
where a=n1.a and b=n1.b
and a=n2.a and b=n2.b
and c<=n1.c
and d<=n1.d
),0))
else 0
end
end )mm
from @b n1 ,@a n2
where
n1.a=n2.a
and n1.b=n2.b
)bb
where bb.mm>0
declare @b table (a int,b int,c int,d int,m int)
insert @a
select 1,2,20
union all
select 2,3,15insert @b
select 1,2,3,9,5
union all
select 1,2,4,10,12
union all
select 1,2,5,11,10
union all
select 1,2,6,12,5
union all
select 2,3,4,6,10
union all
select 2,3,5,7,5
union all
select 2,3,6,8,10select * from @a
select * from @b-- drop table #temp
-- select * from #tempselect * from (
select n1.a,n1.b,n1.c,n1.d
,(case when
((select
sum(s)
from @a
where a=n1.a and b=n1.b
and a=n2.a and b=n2.b
)-
isnull((select
sum(m) from @b
where a=n1.a and b=n1.b
and a=n2.a and b=n2.b
and c<=n1.c
and d<=n1.d
),0))>=0
then n1.m
else
case when n1.m +((select
sum(s) from @a
where a=n1.a and b=n1.b
and a=n2.a and b=n2.b
) -
isnull((select
sum(m) from @b
where a=n1.a and b=n1.b
and a=n2.a and b=n2.b
and c<=n1.c
and d<=n1.d
),0))>=0
then n1.m +((select
sum(s) from @a
where a=n1.a and b=n1.b
and a=n2.a and b=n2.b
) -
isnull((select
sum(m) from @b
where a=n1.a and b=n1.b
and a=n2.a and b=n2.b
and c<=n1.c
and d<=n1.d
),0))
else 0
end
end )mm
from @b n1 ,@a n2
where
n1.a=n2.a
and n1.b=n2.b
)bb
where bb.mm>0
结果不是按最大的开始比较的大家指点下::。