简体   繁体   中英

SQL query with multiple conditions

I am stuck on this question. Can anyone help?

Write a query to print the sum of all total investment values in 2015 (TIV_2012) toa scale of 2 decimal places, for all policyholders who meet the following criteria:

1) Have the same TIV_2011 value as one or more other policyholders.

2) Are not located in the same city as another policy holder (ie (latitude, longitude) attribute pair must be unique,

the input format islike this , The table is

Insurance table is described as follows:

Column Name Type PID INTEGER TIV_2011 NUMERIC TIV_2012 NUMERIC LAT NUMERIC LON NUMERIC

where PID is the policyholder's policy ID, TIV_2011 is the total investment in 2011,TIV_2012 is the total investment in 2012, LAT is the latitude of the policy holder's city and LON is the longitude of the policy holder's city.

For example if thhe data is 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

The answer would be 1601.30. Sum of (300.1, 400, 500.7, 400.5)

So, far I have come up with this

SELECT SUM(TIV_2012) FROM Insurance WHERE NOT UNIQUE(SELECT TIV_2011 from Insurance);

This does not work, I am getting an error. Someone pls help.

SELECT CAST(SUM(t1.TIV_2012) as DECIMAL(10,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
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
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

I believe the query you need to write has two inner joins and goes like this:

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 >>

All the answers here are in one approach. I have tried an other approach so submitting it.

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));
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  

select sum(d.tiv_2012) as Total_2012_TIV from

(select a.TIV_2011,a.TIV_2012,a.lat,a.lon from insurance a join ( SELECT TIV_2011, count( )count FROM Insurance GROUP BY TIV_2011 HAVING COUNT( ) > 1 )b on a.TIV_2011 = b.TIV_2011 join (select lat,count(lat) lat_count from insurance GROUP by lat having count(lat) =1 )c on a.lat = c.lat)d

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM