sql语句改写,运行成功就给分。

hanluner 2011-07-14 11:26:09
select a.acc_no, state, country, institution, b.maxvalue, b.total, c.maxvalue2, c.total2
from account a,(select acc_no, max(init_date) as maxvalue, sum(amount) as total from quotation group by acc_no) b,
(select acc_no, max(order_date) as maxvalue2, sum(total_price) as total2 from order_main group by acc_no) c
where a.acc_no=b.acc_no and a.acc_no=c.acc_no and a.acc_no>20000

这个取得是交集,我现在想取合集,该怎么写,望指教。合集不重复。
...全文
74 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwwb 2011-07-14
  • 打赏
  • 举报
回复
什么合集?
try:
select acc_no, state, country, institution from account a
union
select acc_no, max(init_date) as maxvalue, sum(amount) as total,'' from quotation group by acc_no
union
select acc_no, max(order_date) as maxvalue2, sum(total_price) as total2,'' from order_main group by acc_no
tobyzhu 2011-07-14
  • 打赏
  • 举报
回复
select a.acc_no, state, country, institution, SUM(b.maxvalue) MAXVALUE, SUM(b.total) TOTAL, SUM(c.maxvalue2) MAXVALUES, SUM(c.total2) TOTAL2
from account a,(select acc_no, max(init_date) as maxvalue, sum(amount) as total from quotation group by acc_no) b,
(select acc_no, max(order_date) as maxvalue2, sum(total_price) as total2 from order_main group by acc_no) c
where (a.acc_no=b.acc_no OR a.acc_no=c.acc_no) and a.acc_no>20000
GROUP BY a.acc_no, state, country, institution,
ACMAIN_CHM 2011-07-14
  • 打赏
  • 举报
回复
不懂你的交集是指什么?合集是指什么。

(不要高估你的汉语表达能力或者我的汉语理解能力)
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html

1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)

这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。

hanluner 2011-07-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 tobyzhu 的回复:]

数据量多少?
我上面的答复中间,最后多了路",", 你自己去掉。
如果还不成功,报什么错误?
[/Quote]
数据量大概6W
运行时超时,标点什么我自己改过来了。
tobyzhu 2011-07-14
  • 打赏
  • 举报
回复
select a.acc_no, a.state, a.country, a.institution, SUM(maxvalue) MAXVALUE, SUM(total) TOTAL, SUM(maxvalue2) MAXVALUES, SUM(total2) TOTAL2
from
(select acc_no, state, country, institution, 0 maxvalue, 0 total, 0 maxvalue2, 0 total2 from account ) a,
(select acc_no, state, country, institution, max(init_date) as maxvalue, sum(amount) as total , 0 maxvalue2, 0 total2 from quotation group by acc_no) b,
(select acc_no, state, country, institution, 0 maxvalue, 0 total, max(order_date) as maxvalue2, sum(total_price) as total2 from order_main group by acc_no) c
where (a.acc_no=b.acc_no OR a.acc_no=c.acc_no) and a.acc_no>20000
GROUP BY a.acc_no, state, country, institution
tobyzhu 2011-07-14
  • 打赏
  • 举报
回复
数据量多少?
我上面的答复中间,最后多了路",", 你自己去掉。
如果还不成功,报什么错误?
hanluner 2011-07-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tobyzhu 的回复:]

select a.acc_no, state, country, institution, SUM(b.maxvalue) MAXVALUE, SUM(b.total) TOTAL, SUM(c.maxvalue2) MAXVALUES, SUM(c.total2) TOTAL2
from account a,(select acc_no, max(init_date) as max……
[/Quote]
数据量很大,你的这个不成功。你看看能不能用left jion写一下

2,596

社区成员

发帖
与我相关
我的任务
社区描述
Sybase相关技术讨论区
社区管理员
  • Sybase社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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