SQL 加入不同的 select 计数结果表

SQL join on different select count result-tables

我需要一些关于 sql-joins 的帮助。 我有 3 个 select 语句,它们给我这样的输出:

select-statement A:
location amount_A
7234     17
7456     2

select-statement B:
location number_x
7234     4455
7456     555

select-statement C:
location errors
7234     1
7456     44537

我想要这样的 table 结果:

location Amount_A  number_x  errors
7234     17        4455      1
7456     2         555       44537

什么是最好的 and/or 最简单的实现方法?每个 select 语句使用其他 tables.?!

声明如下:

A: select substring(column_a for 4) location, count(*) Amount_A from table_a where column_a like '7%' group by location  ;
B: select substring(e.column_xy for 4), count(*) number_x from table_b b, table_e e , table_c c where b.stationextension_id = e.id and b.id = c.id and ( c.column_h in ( 'value_a', 'value_b' ) ) group by substring(e.column_xy for 4)  ;
C: select substring(name from 1 for 4), count(*) from errors group by substring(name from 1 for 4) ;

使用Inner Join连接所有三个查询。

还使用内部联接语法联接两个表,这比旧式逗号分隔联接更具可读性。

Substring 函数有一些不同的参数希望这些是原始查询中的示例你需要将正确的值传递给子字符串函数

select a.location,Amount_A ,number_x,errors from
(
SELECT substring(column_a for 4) location, count(*) Amount_A FROM table_a WHERE column_a LIKE '7%' GROUP BY location  
) A 
inner join
(
SELECT substring(e.column_xy for 4) location , count(*) number_x FROM table_b b inner join table_e e on  b.stationextension_id = e.id and b.stationextension_id = e.id 
inner join  table_c c on  b.id = c.id 
where c.column_h IN ( 'value_a', 'value_b' ) ) GROUP BY substring(e.column_xy FOR 4)  ;
) B on a.location = b.location 
inner join 
(
SELECT substring(name from 1 FOR 4) location, count(*) errors FROM errors GROUP BY substring(name FROM 1 FOR 4) ;
)
C on c.location = b.location

您可以将所有三个查询合并为一个

SELECT s1.location,
       s1.Amount_A,
       s2.number_x,
       s3.errors
  FROM (SELECT SUBSTRING (column_a FOR 4) AS location,
    COUNT(*) AS Amount_A
  FROM table_a
    WHERE column_a LIKE '7%'
  GROUP BY location) s1
  JOIN ( SELECT SUBSTRING(e.column_xy FOR 4) AS location,
    COUNT(*) AS number_x
  FROM table_b b
    JOIN table_e e ON b.stationextension_id = e.id
    JOIN table_c c ON b.id = c.id
  WHERE c.column_h IN ( 'value_a', 'value_b' )
    GROUP BY SUBSTRING(e.column_xy FOR 4)) s2
    ON s1.location = s2.location
  JOIN (SELECT SUBSTRING(name FROM 1 FOR 4) AS location,
  COUNT(*) AS errors
  FROM errors
    GROUP BY SUBSTRING(name FROM 1 FOR 4)) s3 ON s1.location = s3.location

试试这个:

select 
  a.location, a.amount_A,
  b.number_x,
  c.errors
from (
  select_satement_A
) as a
join (
  select_satement_B
) as b on a.location = b.location
join (
  select_satement_C
) as c on a.location = c.location

如果 select_satement_A 中的所有数据都需要 retrieved 使用 left join。 来自 select_satement_A 的所有数据将与来自 select_satement_Bselect_satement_C 的相应数据一起检索。如果找不到 join 条件的匹配项,将替换 null

   select 
      a.location, a.amount_A,
      b.number_x,
      c.errors
    from (
      select_satement_A
    ) as a
    left join (
      select_satement_B
    ) as b on a.location = b.location
    left join (
      select_satement_C
    ) as c on a.location = c.location

要检索所有数据,请使用 full join

您总是可以将一些左外连接合并到一个语句中。我想位置可能没有任何错误。

如果对基础数据没有更好的理解,以正确的顺序进行连接有点棘手,但我希望这能为您指明正确的方向。

select  substring(e.column_xy for 4) location, 
        count(a.column_xy)           Amount_A, 
        count(e.ie)                  number_x, 
        count(e2.name)               errors
  from  table_b b
  inner join  table_e  e on b.stationextension_id        = e.id
  inner join  table_c  c on b.id                         = c.id
  left  join  table_a  a on substring(e.column_xy for 4) = substring(a.column_a from 1 for 4)
                        and a.column_a                like '7%'
  left  join  errors  e2 on substring(e.column_xy for 4) = substring(e2.name    from 1 for 4)
  where c.column_h in ( 'value_a', 'value_b' ) 
  group by substring(e.column_xy for 4);