正确的SQL查询方法

时间:2017-08-26 19:03:34

标签: mysql sql

我需要有关基于MySQL的SQL问题的帮助,以下是描述和示例输入:

enter image description here

以下是示例输出:

enter image description here

我尝试过类似的事情:

select ROUND( sum(i1.TIV_2012), 2 ) from INSURANE i1, INSURANCE i2, INSURANCE i3 where ( i1.PID!=i2.PID and i1.TIV_2011=i2.TIV_2011 ) and (i1.PID!=i3.PID and (i1.LAT!=i3.LAT or i1.LON!=i3.LON) )

但系统无法接受这一点。 谁能帮助我?

1 个答案:

答案 0 :(得分:1)

使用以下SQL

SELECT SUM(i1.tiv_2012) total       
  FROM insurance i1
 WHERE NOT EXISTS (SELECT 1 
                     FROM insurance i2 
                    WHERE i1.lat = i2.lat 
                      AND i1.lon = i2.lon 
                      AND i1.pid <> i2.pid)
   AND EXISTS (SELECT i3.tiv_2011 
                 FROM insurance i3 
                WHERE i3.tiv_2011 = i1.tiv_2011 
                  AND i3.pid <> i1.pid);   

结果

total
1711758.65