sql 查询求助各位大神

x2500969 2013-11-15 06:16:44
现有一个sql 查询求助各位大神

在一个表中有a,b,c,d,e栏

查询条件为:a中第二个字符相同的把d中的数据优先选择小的加大的接近100(不超过100),并输出格式为

a,b,c,d,e a,b,c,e, f(d数据相加的和)


sql怎么写?谢谢各位了。
...全文
198 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mfkpie 2013-11-18
  • 打赏
  • 举报
回复
思路很hi哦不错不错
唐诗三百首 2013-11-17
  • 打赏
  • 举报
回复

create table table1
(a int,b int,c int)

insert into table1
 select 111,222,15 union all
 select 113,222,65 union all
 select 121,222,35 union all
 select 125,222,45 union all
 select 126,222,15


select a,b,c,a1,b1,c1,d
from
(select x.a,x.b,x.c,
        y.a 'a1',y.b 'b1',y.c 'c1',
        x.c+y.c 'd',
        row_number() over(partition by substring(rtrim(x.a),2,1)
                          order by abs(100-(x.c+y.c))) 'rn'
 from table1 x
 cross join table1 y
 where substring(rtrim(x.a),2,1)=substring(rtrim(y.a),2,1)
 and not(x.a=y.a and x.b=y.b and x.c=y.c)) t
where t.rn=1

/*
a           b           c           a1          b1          c1          d
----------- ----------- ----------- ----------- ----------- ----------- -----------
111         222         15          113         222         65          80
121         222         35          125         222         45          80

(2 row(s) affected)
*/
x2500969 2013-11-15
  • 打赏
  • 举报
回复
不好意思,重新描述一下,现只要c中相加结近100的优先排序 ------------------------------------------ 如表table1中 a b c 111 222 15 113 222 65 121 222 35 125 222 45 126 222 15 ................................. 现要求选择a值中第二个数字相同然后c中值相加接近为100的优先的进行排序。输出格式为: a b c a1 b1 c1 d 111 222 15 113 222 65 80 121 222 35 125 222 45 80
x2500969 2013-11-15
  • 打赏
  • 举报
回复
不好意思,重新描述一下,现只要c中相加结近100的优先排序 ------------------------------------------ 如表table1中 a b c 111 222 15 113 222 65 121 222 35 125 222 45 126 222 15 ................................. 现要求选择a值中第二个数字相同然后c中值相加接近为100的优先的进行排序。输出格式为: a b c a b c d 111 222 15 113 222 65 80 121 222 35 125 222 45 80
shoppo0505 2013-11-15
  • 打赏
  • 举报
回复
看不懂,具体举例。

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧