簡體   English   中英

SQL將一個表兩次連接到另一個表

[英]SQL join one table to other table twice

我有這個SQL通過加入3個表,sample_register,村庄和water_cssr來獲取數據。

SELECT
  sample_register.location,
  sample_register.description,
  villages.distance,
  case when water_cssr.istp='Cs' then COUNT(water_cssr.usin) end as total_cs,
  case when water_cssr.bdl='Y'and water_cssr.istp='Cs' then COUNT(water_cssr.usin) end as bdl_cs,
  case when water_cssr.bdl='N'and water_cssr.istp='Cs' then min(water_cssr.activity) end as csmin,
  case when water_cssr.bdl='N'and water_cssr.istp='Cs' then max(water_cssr.activity) end as csmax,
  case when water_cssr.istp='Sr' then COUNT(water_cssr.usin) end as total_sr,
  case when water_cssr.bdl='Y'and water_cssr.istp='Sr' then COUNT(water_cssr.usin) end as bdl_sr,
  case when water_cssr.bdl='N' and water_cssr.istp='Sr' then min(water_cssr.activity) end as srmin,
  case when water_cssr.bdl='N' and water_cssr.istp='Sr' then max(water_cssr.activity) end as srmax
FROM sample_register
LEFT JOIN villages on sample_register.location=villages.location
LEFT JOIN sample_allocation on sample_register.usin=sample_allocation.usin
INNER JOIN water_cssr ON water_cssr.usin = sample_register.usin
GROUP BY sample_register.location, sample_register.description, sample_allocation.cs 
order by villages.dist_group, villages.location

我得到這樣的結果

Location   Type    Distance   Total_cs   Bdl_cs   Csmin   csmax   Total_sr   Bdl_sr   Srmin   srmax
A          TYPE1   5                                               1         1
B          TYPE2   10         1          1        4       12
B          TYPE2   10                                              1         1        1       8
C          TYPE3   15                                              1         1        9       14
C          TYPE3   15         1          1        15      24
D          TYPE1   10                                              1         1
E          TYPE2   10         1          1
F          TYPE1   20         1          1

在上面的位置B和C有兩行,每行的值在第4至7列,另一行的值在第8至11列。 我希望這兩行的內容在同一行中,因為第一列到第三列的值對於兩行都是通用的。 例如,將第二行和第三行合並后應產生這樣的行

B   TYPE2   10  1   1   4   12  1   1   1   8

請幫助我重組SQL

這只是一個概念:

  • 查找哪些表或哪些表聯接返回B和C的2行。
  • 為該表或表聯接創建查詢,因此它將為b和c及其他記錄返回1行。
  • 然后修改您的查詢以在聯接中使用新查詢,如果正確的b和c不再返回2行。

祝好運

我做了兩個子查詢並像這樣加入

 SELECT * FROM  (SELECT
sample_register.location,
sample_register.description,
villages.distance, villages.direction,
case when water_cssr.istp='Cs' then COUNT(water_cssr.usin) end as total_cs,
case when water_cssr.bdl='Y'and water_cssr.istp='Cs' then COUNT(water_cssr.usin) end as bdl_cs,
case when water_cssr.bdl='N'and water_cssr.istp='Cs' then min(water_cssr.activity) end as csmin,
case when water_cssr.bdl='N'and water_cssr.istp='Cs' then max(water_cssr.activity) end as csmax
FROM
sample_register LEFT JOIN villages on sample_register.location=villages.location LEFT JOIN sample_allocation on sample_register.usin=sample_allocation.usin INNER JOIN water_cssr ON water_cssr.usin = sample_register.usin
 WHERE mid(sample_register.usin,3,1)='N' and  sample_register.doc between '$start_date' and '$end_date' AND water_cssr.istp='Cs'  GROUP BY sample_register.location, sample_register.description, sample_allocation.cs order by villages.dist_group, villages.location) AS a left JOIN (SELECT
sample_register.location,
sample_register.description,
villages.distance, villages.direction,
case when water_cssr.istp='Sr' then COUNT(water_cssr.usin) end as total_sr,
case when water_cssr.bdl='Y'and water_cssr.istp='Sr' then COUNT(water_cssr.usin) end as bdl_sr,
case when water_cssr.bdl='N'and water_cssr.istp='Sr' then min(water_cssr.activity) end as srmin,
case when water_cssr.bdl='N'and water_cssr.istp='Sr' then max(water_cssr.activity) end as srmax
FROM
sample_register LEFT JOIN villages on sample_register.location=villages.location LEFT JOIN sample_allocation on sample_register.usin=sample_allocation.usin INNER JOIN water_cssr ON water_cssr.usin = sample_register.usin
 WHERE mid(sample_register.usin,3,1)='N' and  sample_register.doc between '$start_date' and '$end_date' AND water_cssr.istp='Sr'  GROUP BY sample_register.location, sample_register.description, sample_allocation.cs order by villages.dist_group, villages.location) as b on a.location=b.location and a.description=b.description"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM