当前位置: 代码迷 >> SQL >> sql话语改写,运行成功就给分
  详细解决方案

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

热度:54   发布时间:2016-05-05 12:37:00.0
sql语句改写,运行成功就给分。
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

这个取得是交集,我现在想取合集,该怎么写,望指教。合集不重复。
------解决方案--------------------
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 .. 语句
  相关解决方案