有关sql语句的问题?。。我有点弧度了。。。

qaqaqa 2003-10-16 09:15:09
id p_id acount ac w pic
1 38 1 1 28 550
3 36 2 1 28 520
6 36 4 10 28 521
7 36 7 10 28 522
8 38 1 1 28 551
9 38 3 1 28 552
10 38 1 1 28 553
怎么找出 满足 pic > 522 并且 这些记录的acount 合小于 13 的所有纪录

还有如何得到 满足以上这些条件的sum(acount)


按 pic 的降序排列。。从第一条往后掐。。 找出 pic 满足一定条件 并且要记录的acount合控制在一定范围内 的所有纪录

像这样

10 38 1 1 28 553
9 38 3 1 28 552
8 38 1 1 28 551

找出 。。pic大于550 并且 sum(acount)小于等于 6



按 pic 的降序排列。。从第一条往后掐。。 找出 pic 满足一定条件 并且要记录的acount合控制在一定范围内 的所有纪录

像这样

10 38 1 1 28 553
9 38 3 1 28 552
8 38 1 1 28 551

找出 。。pic大于550 并且 sum(acount)小于等于 6
...全文
27 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengdali 2003-10-16
  • 打赏
  • 举报
回复

select * from 表 where pic>522 and ap_id in (select ap_id from 表 where pic>522 group by ap_id having sum(acount) <=4) order by id
qaqaqa 2003-10-16
  • 打赏
  • 举报
回复
我刚刚测试了。。没有达到要求阿。。。无论怎么改
pic>522 550 552 都是那几条数据 没有变化 好像条件不对
pengdali 2003-10-16
  • 打赏
  • 举报
回复
是呀,没有人说不是,你把

select * from 表 where ap_id in (select ap_id from 表 where pic>522 group by ap_id having sum(acount) <=4) order by id


这里面的“表” 改为你的表名试试就知道了。
qaqaqa 2003-10-16
  • 打赏
  • 举报
回复
我操作的是同一个表
pengdali 2003-10-16
  • 打赏
  • 举报
回复
找出 。。pic大于550 并且 sum(acount)小于等于 6
select * from 表 where ap_id in (select ap_id from 表 where pic>522 group by ap_id having sum(acount) <=6) order by id



找出 。。pic大于550 并且 sum(acount)小于等于 4
select * from 表 where ap_id in (select ap_id from 表 where pic>522 group by ap_id having sum(acount) <=4) order by id
pengdali 2003-10-16
  • 打赏
  • 举报
回复
select * from 表 where ap_id in (select ap_id from 表 where pic>522 group by ap_id having sum(acount) <=6)
CrazyFor 2003-10-16
  • 打赏
  • 举报
回复
TRY:


select * from 表 aa where aa.pic>XX and (select sum(acount) from 表 bb where bb.pic<=aa.pic and bb.pic >XX)<=6
order by aa.pic

XX为你要的数值.
qaqaqa 2003-10-16
  • 打赏
  • 举报
回复
pic大于550 是主要条件
sum(acount)小于等于 4 是次要条件
qaqaqa 2003-10-16
  • 打赏
  • 举报
回复
10 38 1 1 28 553
9 38 3 1 28 552

找出 。。pic大于550 并且 sum(acount)小于等于 4

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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