使用2个WHERE子句将两个select语句合并为一个

时间:2019-04-30 15:58:08

标签: sql database ms-access

我正在使用这两个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

1 个答案:

答案 0 :(得分:0)

您似乎想要条件聚合。如果是这样,您可以在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;