sql语句改写,运行成功就给分。SQL codeselect a.acc_no, state, country, institution, b.maxvalue, b.tota
sql语句改写,运行成功就给分。
SQL codeselect 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
这个取得是交集,我现在想取合集,该怎么写,望指教。合集不重复。
[解决办法]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,
[解决办法]什么合集?
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
[解决办法]数据量多少?
我上面的答复中间,最后多了路",", 你自己去掉。
如果还不成功,报什么错误?
[解决办法]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
[解决办法]不懂你的交集是指什么?合集是指什么。
(不要高估你的汉语表达能力或者我的汉语理解能力)
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式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)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。