不同select count结果表上的SQL联接

不同select count结果表上的SQL联接,sql,sql-server,select,join,count,Sql,Sql Server,Select,Join,Count,我需要一些关于sql连接的帮助。 我有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 location Amount_A

我需要一些关于sql连接的帮助。 我有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
location Amount_A  number_x  errors
7234     17        4455      1
7456     2         555       44537
我希望将结果放在一个表中,如下所示:

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
location Amount_A  number_x  errors
7234     17        4455      1
7456     2         555       44537
实现这一目标的最佳和/或最简单的方法是什么?每个select语句都使用其他表

声明如下:

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

使用
internaljoin
连接所有三个查询

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

Substring
函数有一些不同的参数,希望这些是原始查询中的示例,您需要将正确的值传递给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_statement_A
的数据将与来自
select_statement_B
select_statement_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

对于要检索的所有数据,请使用
完全联接

,您始终可以将一些左外部联接合并到一个语句中。我想位置可能没有任何错误

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

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

感谢您的编辑,使其更具可读性。使用的
子字符串
函数OP看起来像来自MySQL.works,但我的结果表中只有一行-这一行的值是正确的。如果您想要
sele_state_A
中所有行的
数据
,请使用
左连接
。更新答案。