【问题标题】:Joining two select statements into one with 2 WHERE clauses使用 2 个 WHERE 子句将两个 select 语句合并为一个
【发布时间】:2026-01-05 05:20:03
【问题描述】:

我正在使用这两个 select 语句,并希望将它们合并为一个。有没有人有什么建议。但是,问题是我正在使用访问权限,并且访问权限中没有 case 子句。

SELECT 
     Table_1.Table_Identification_Number,
     AVG (Table_2.Coulmn-2) AS avg_attribute
FROM 
    Table_1 JOIN
    Table_2 ON Table_1.Table_Identification_Number = Table_2.Table_Identification_Number
WHERE
    Table_2.Column-3='1'
GROUP BY 
    Table_1.Table_Identification_Number


SELECT 
     Table_1.Table_Identification_Number,
     AVG (Table_2.Coulmn-2) AS avg_attribute
FROM 
    Table_1 JOIN
    Table_2 ON Table_1.Table_Identification_Number = Table_2.Table_Identification_Number
WHERE
    Table_2.Column-3='2'
GROUP BY 
    Table_1.Table_Identification_Number

【问题讨论】:

  • 请提供样本数据和期望的结果。此外,修复数据库标签以确保您的数据库准确无误。

标签: sql database ms-access


【解决方案1】:

您似乎想要条件聚合。如果是这样,您可以在 MS Access 中将其表示为:

SELECT t1.Table_Identification_Number,
       AVG(IIF(t2.Column_3 = 1, t2.Column_2, NULL)) as avg_1,
       AVG(IIF(t2.Column_3 = 2, t2.Column_2, NULL)) as avg_2
FROM Table_1 as t1 JOIN
     Table_2 as t2
     ON t1.Table_Identification_Number = t2.Table_Identification_Number
WHERE t2.Column_3 IN (1, 2)
GROUP BY t1.Table_Identification_Number;

【讨论】:

    最近更新 更多