具有多个条件的SQL查询

时间:2016-10-14 21:19:23

标签: mysql sql

我坚持这个问题。有人可以帮忙吗?

为符合以下条件的所有投保人写一个查询,将2015年所有投资总额的总和(TIV_2012)打印到2位小数的比例:

1)与一个或多个其他保单持有人具有相同的TIV_2011价值。

2)与其他保单持有人不在同一个城市(即(纬度,经度)属性对必须是唯一的,

输入格式是这样的,表是

保险表描述如下:

列名称类型 PID INTEGER TIV_2011 NUMERIC TIV_2012 NUMERIC LAT NUMERIC LON NUMERIC

其中PID是保单持有人的政策ID,TIV_2011是2011年的总投资,TIV_2012是2012年的总投资,LAT是保单持有人城市的纬度,而LON是经度保单持有人的城市。

例如,如果数据是 PID,TIV_2011,TIV_2012,lat,lon

  1. 1,300,400.5,60,70

  2. 2,300,500.7,70,80

  3. 3,400,400,60,90

  4. 4,500,600,80,80

  5. 5,400,300.1,6,6

  6. 答案是1601.30。总和(300.1,400,500.7,400.5)

    所以,到目前为止,我已经提出了这个

    SELECT SUM(TIV_2012)FROM Insurance WHERE NOT UNIQUE(从保险中选择TIV_2011);

    这不起作用,我收到错误。有人帮忙。

7 个答案:

答案 0 :(得分:13)

SELECT SUM(t1.TIV_2012)
FROM Insurance t1
INNER JOIN
(
    SELECT TIV_2011
    FROM Insurance
    GROUP BY TIV_2011
    HAVING COUNT(*) > 1
) t2
    ON t1.TIV_2011 = t2.TIV_2011
INNER JOIN
(
    SELECT lat, lon
    FROM Insurance
    GROUP BY lat, lon
    HAVING COUNT(*) = 1
) t3
    ON t1.lat = t3.lat AND
       t1.lon = t3.lon

答案 1 :(得分:3)

SELECT CAST(SUM(t1.TIV_2012) as DECIMAL(11,2)) 
FROM Insurance t1
INNER JOIN (
  SELECT TIV_2011
  FROM Insurance
  GROUP BY TIV_2011 HAVING COUNT(*) > 1 ) t2 ON t1.TIV_2011 = t2.TIV_2011
INNER JOIN ( 
  SELECT lat, lon 
  FROM Insurance 
  GROUP BY lat, lon HAVING COUNT(*) = 1 ) t3 ON t1.lat = t3.lat AND t1.lon = t3.lon

答案 2 :(得分:1)

Round(x,2) -> to scale to 2 decimal digits
inner join 1 -> for condition 1 (finds all the repeating TIV_2011)
inner join 2 -> for condition 2 (finds LAT, LON that are distinct as a pair)

Select ROUND(SUM(i1.TIV_2012),2) 
from Insurance i1
inner join 
    (Select TIV_2011 
    from Insurance 
    group by TIV_2011 
    having count(*) > 1) i2 
on
    i1.TIV_2011 = i2.TIV_2011
inner join 
    (Select LAT, LON 
    from Insurance 
    group by LAT, LON 
    having count(*) = 1) i3 
on 
    i1.LAT = i3.LAT 
and  
    i1.LON = i3.LON

答案 3 :(得分:0)

我相信你需要编写的查询有两个内连接,如下所示:

SELECT SUM(ins1.column_of_interest) as value_needed
FROM Insurance ins1
INNER JOIN Insurance ins2 ON (ins1.id = ins2.id AND <<conditions to get same TIV_2011 applied to ins2 >> )
INNER JOIN Insurance ins3 ON (ins1.id = ins3.id AND << conditions to get unique latitude, longitude on ins3 >> )
WHERE << other conditions you may apply to ins1 >>

答案 4 :(得分:0)

这里的所有答案都采用一种方法。我尝试了另一种方法,所以提交它。

SELECT CAST(SUM(i.TIV_2012) AS NUMERIC(18,2))
FROM Insurance i
WHERE i.PID IN (SELECT DISTINCT(i1.PID)
                FROM Insurance i1
                INNER JOIN Insurance i2 ON (i1.TIV_2011 = i2.TIV_2011 AND i1.PID <> i2.PID)
                WHERE NOT EXISTS (SELECT i3.PID 
                    FROM Insurance i3 
                    WHERE i1.PID <> i3.PID
                    AND i1.LAT = i3.LAT
                    AND i1.LON = i3.LON));

答案 5 :(得分:0)

SELECT SUM(i1.TIV_2012) FROM (
  SELECT * ,COUNT(1) OVER (PARTITION BY i.TIV_2011 ORDER BY i.TIV_2011) R2 ,COUNT(1) OVER (PARTITION BY i.Lat, i.Lon ORDER BY i.Lat, i.Lon) R1 FROM 
    #Insurance i) i1 
WHERE R2 > 1 AND R1 = 1  

答案 6 :(得分:0)

从以下位置选择总和(d.tiv_2012)作为Total_2012_TIV

(选择a.TIV_2011,a.TIV_2012,a.lat,a.lon 来自保险 加入 (SELECT TIV_2011,count()count 来自保险 按TIV_2011分组 拥有COUNT()> 1 )b 在a.TIV_2011 = b.TIV_2011 加入 (选择纬度,计数(纬度)纬度计数 来自保险 按拉特分组 具有count(lat)= 1 )C 在a.lat = c.lat)d