简体   繁体   English

联表过滤MySQL中的信息

[英]Join tables to filter information in MySQL

I have the following question:我有以下问题:

Input: 
Insurance table:
+-----+----------+----------+-----+-----+
| pid | tiv_2015 | tiv_2016 | lat | lon |
+-----+----------+----------+-----+-----+
| 1   | 10       | 5        | 10  | 10  |
| 2   | 20       | 20       | 20  | 20  |
| 3   | 10       | 30       | 20  | 20  |
| 4   | 10       | 40       | 40  | 40  |
+-----+----------+----------+-----+-----+
Output: 
+----------+
| tiv_2016 |
+----------+
| 45.00    |
+----------+

Explanation: The first record in the table, like the last record, meets both of the two criteria.解释:表中的第一条记录与最后一条记录一样,同时满足这两个条件。 The tiv_2015 value 10 is the same as the third and fourth records, and its location is unique. tiv_2015的值10与第三条和第四条记录相同,其位置是唯一的。

The second record does not meet any of the two criteria.第二条记录不符合这两个条件中的任何一个。 Its tiv_2015 is not like any other policyholders and its location is the same as the third record, which makes the third record fail, too.它的 tiv_2015 与其他保单持有人不同,它的位置与第三条记录相同,这使得第三条记录也失败了。 So, the result is the sum of tiv_2016 of the first and last record, which is 45.因此,结果是第一条和最后一条记录的 tiv_2016 之和,即 45。

My SQL query:我的SQL查询:

WITH tb1 AS(
SELECT DISTINCT a.pid,a.tiv_2016
FROM Insurance a
JOIN Insurance b
ON a.pid != b.pid AND a.tiv_2015 = b.tiv_2015
JOIN Insurance c
ON a.pid != c.pid AND (a.lat, a.lon) != (c.lat, c.lon)
)
SELECT SUM(tiv_2016) AS tiv_2016
FROM tb1

However, for the following input, my code failed to filter on the lat and lon columns:但是,对于以下输入,我的代码无法过滤 lat 和 lon 列:

| pid | tiv_2015 | tiv_2016 | lat | lon |
| --- | -------- | -------- | --- | --- |
| 1   | 10       | 5        | 10  | 10  |
| 2   | 20       | 20       | 20  | 20  |
| 3   | 10       | 30       | 20  | 20  |
| 4   | 10       | 40       | 40  | 40  |

My output:我的 output:

| tiv_2016 |
| -------- |
| 75       |

The desired output:所需的 output:

| tiv_2016 |
| -------- |
| 45       |

When constructing tb1, my query also selected the row with pid = 3, which led to a total of 75.在构造tb1的时候,我的查询也选择了pid=3的行,导致一共75个。

I figured out other ways to return the expected output but just don't understand why my original SQL query won't work.我想出了其他方法来返回预期的 output 但只是不明白为什么我原来的 SQL 查询不起作用。 Why does the ON a.pid.= c.pid AND (a,lat. a.lon),= (c.lat? c.lon) seem to do nothing?为什么 ON a.pid.= c.pid AND (a,lat. a.lon),= (c.lat? c.lon) 似乎什么都不做?

I would first do an outer join of the Insurance table with itself to find all rows with unique lat/lon values (subquery sq below).我将首先对Insurance表与其自身进行外部连接,以查找具有唯一纬度/经度值的所有行(下面的子查询sq )。 Then I would do a sum of the tiv_2016 column grouped by the tiv_2015 column:然后我会对按tiv_2016列分组的tiv_2015列求和:

Schema (MySQL v5.7)架构 (MySQL v5.7)

CREATE TABLE Insurance
    (`pid` int, `tiv_2015` int, `tiv_2016` int, `lat` int, `lon` int)
;
    
INSERT INTO Insurance
    (`pid`, `tiv_2015`, `tiv_2016`, `lat`, `lon`)
VALUES
    (1, 10, 5, 10, 10),
    (2, 20, 20, 20, 20),
    (3, 10, 30, 20, 20),
    (4, 10, 40, 40, 40)
;

Query #1查询 #1

select sum(tiv_2016) as tiv_2016 from (
  select i1.* from Insurance i1
  left join Insurance i2 on i1.pid <> i2.pid and i1.lat = i2.lat and i1.lon = i2.lon
  where i2.pid is null
) sq
group by tiv_2015
;
tiv_2016 tiv_2016
45 45

View on DB Fiddle在 DB Fiddle 上查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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