Sql que.@ShaneBond-当其他Lat/Lon值在所有数字中明显不同时,很容易犯错误。没有其他表格:重点是与同一表格中的其他投资者进行比较。@Dan R:你在说什么其他表格?我没有提到任何问题,我的问题中也没有。或者你是在回答一些在你写下自己的评论后被

Sql que.@ShaneBond-当其他Lat/Lon值在所有数字中明显不同时,很容易犯错误。没有其他表格:重点是与同一表格中的其他投资者进行比较。@Dan R:你在说什么其他表格?我没有提到任何问题,我的问题中也没有。或者你是在回答一些在你写下自己的评论后被,sql,sql-server,join,Sql,Sql Server,Join,que.@ShaneBond-当其他Lat/Lon值在所有数字中明显不同时,很容易犯错误。没有其他表格:重点是与同一表格中的其他投资者进行比较。@Dan R:你在说什么其他表格?我没有提到任何问题,我的问题中也没有。或者你是在回答一些在你写下自己的评论后被删除的评论吗?没有其他表格:重点是要与同一表格中的其他投资者进行比较。@Dan R:你在说什么其他表格?我没有提到任何问题,我的问题中也没有。或者你是在回答一些在你写下你的评论后被删除的评论吗?我认为这句话的目的是避免把投资者和他自己联系起来。


que.@ShaneBond-当其他
Lat
/
Lon
值在所有数字中明显不同时,很容易犯错误。没有其他表格:重点是与同一表格中的其他投资者进行比较。@Dan R:你在说什么其他表格?我没有提到任何问题,我的问题中也没有。或者你是在回答一些在你写下自己的评论后被删除的评论吗?没有其他表格:重点是要与同一表格中的其他投资者进行比较。@Dan R:你在说什么其他表格?我没有提到任何问题,我的问题中也没有。或者你是在回答一些在你写下你的评论后被删除的评论吗?我认为这句话的目的是避免把投资者和他自己联系起来。然而,你是对的,这是不需要的,因为我们已经避免在同一个城市匹配投资者,而且每个投资者都和他自己在同一个城市。(嗨,Omkar,欢迎来到StackOverflow!)我认为这条线的目的是避免将投资者与自己匹配。然而,你是对的,这是不需要的,因为我们已经避免在同一个城市匹配投资者,而且每个投资者都和他自己在同一个城市。(嗨,Omkar,欢迎来到StackOverflow!)
PID   TIV_2011  TIV_2012     LAT         LON
123     1000      1200     20.123489   80.341245
456     1500      3000     21.341287   80.341245
789     2000      1500     21.341287   80.341245
321     1000       750     21.123641   80.238716
567     1500      2300     22.123641   80.238716
select cast(sum(i1.tiv_2012) as decimal(12,2))
from insurance i1 join insurance i2 on i1.tiv_2011 = i2.tiv_2011
where i1.pid != i2.pid 
and i1.lat != i2.lat 
and i1.lon != i2.lon
select cast(sum(i1.tiv_2012) as decimal(12,2)) from insurance i1 
join (select TIV_2011 from insurance group by TIV_2011 having count(*) > 1) i2 on i1.tiv_2011 = i2.TIV_2011
join (select lat,LON from insurance group by LAT,LON having COUNT(*) = 1) i3 on i1.LAT = i3.LAT and i1.LON = i3.LON
i1.pid != i2.pid 
DECLARE @Test TABLE (
    PID         int,
    TIV_2011    int,
    TIV_2012    int,
    LAT         DECIMAL(8,6),        
    LON         DECIMAL(8,6)
) 
INSERT @Test(PID,   TIV_2011,  TIV_2012,     LAT,         LON)
VALUES
(123, 1000, 1200, 20.123489, 80.341245),
(456, 1500, 3000, 21.341287, 80.341245),
(789, 2000, 1500, 21.341287, 80.341245),
(321, 1000,  750, 21.123641, 80.238716),
(567, 1500, 2300, 22.123641, 80.238716)

select cast(sum(i1.tiv_2012) as decimal(12,2))
from @Test i1 
    join @Test i2 on i1.tiv_2011 = i2.tiv_2011
        AND i1.pid != i2.pid 
where 
    NOT EXISTS (
        SELECT 1
        FROM @Test
        WHERE LAT = i1.lat 
            and LON = i1.lon
            AND PID != i1.pid
    )
select sum(tiv_2012)
from insurance i
where exists
(
  select * 
  from mytable other 
  where other.pid <> i.pid
  and other.tiv_2011 = i.tiv_2011
)
and not exists
(
  select * 
  from mytable other 
  where other.pid <> i.pid
  and other.lat = i.lat
  and other.lon = i.lon
);
declare @t table (PID int, TIV_2011 int, TIV_2012 int, Lat decimal(8,6),Lon decimal(9,6))
insert into @t(PID,TIV_2011,TIV_2012,LAT,LON) values
(123,1000,1200,20.123489,80.341245),
(456,1500,3000,21.341287,80.341245),
(789,2000,1500,21.341287,80.341245),
(321,1000, 750,21.123641,80.238716),
(567,1500,2300,22.123641,80.238716)

select
    CONVERT(decimal(12,2),SUM(TIV_2012))
from
    @t t_keep
where
    exists (select * from @t t_other2011
       where t_keep.PID != t_other2011.PID
       and t_keep.TIV_2011 = t_other2011.TIV_2011
) and
    not exists (select * from @t t_city
       where t_keep.PID != t_city.PID and
       t_keep.Lat = t_city.Lat and
       t_keep.Lon = t_city.Lon)