在计算唯一值的 SQL 语法中包含 table 名称

Include table name in SQL syntax that counts unique values

以下 SQL 在我的数据中查找重复的唯一 ID,以便我可以清理它。我希望它包含该号码所在的表格列表。

所以

SELECT 
    GID_New, TableName, COUNT(*) no_matches
FROM 
    (SELECT GID_New 
     FROM "Drainage_Lines" AS TableName  
     UNION ALL
     SELECT GID_New  
     FROM "Drainage_Points" AS TableName    
     UNION ALL 
     SELECT GID_New 
     FROM "Drainage_Gross_Pollutant_Traps" AS TableName) t  
GROUP BY
    GID_New   
HAVING
    COUNT(*) > 1

此查询产生:

但我希望它得到这样的结果:

GID_New | Table Name                      | no_matches 
--------+---------------------------------+-----------
Null    | Drainage_Points, Drainage_Lines |    19
1906    | Drainage_Points                 |     2

等...

然后我可以看到 2 个表中有 Null(s),但 1906 在 Points 数据集中只重复了两次。

我该怎么做?这是使用 SQL 的 QGIS 风格,只允许 these commands?

您可以使用标志来做到这一点:

SELECT GID_New, TableName, count(*) as no_matches,
       sum(is_dl), sum(is_dgp), sum(is_dgpt)
FROM (select GID_New, 1 as is_dl, 0 as is_dgp, 0 as is_dgpt
      from "Drainage_Lines" as TableName
      union all
      select GID_New, 0, 1, 0
      from "Drainage_Points" as TableName
      union all
      select GID_New, 0, 0, 1
      from "Drainage_Gross_Pollutant_Traps" as TableName
     ) t  
Group by GID_New
having count(*) > 1;

这不是您想要的格式,但具有相同的信息。