如何将这两个查询合并为一个?

时间:2012-01-09 14:27:21

标签: sql ms-access vba ms-access-2010

查询1:

    SELECT DISTINCT FirstOfFrequencyMHz, Min(LicenceData.distance) AS CoFX
FROM [Freq List], LicenceData
WHERE ((([LicenceData].class_stat) Like 'F*') And (([LicenceData].type_lc)="A") And (([LicenceData].frequency) Between FirstOfFrequencyMHz-0.0249 And FirstOfFrequencyMHz+0.0249))
GROUP BY FirstOfFrequencyMHz

QUERY2:

    SELECT DISTINCT FirstOfFrequencyMHz, Min(LicenceData.distance) AS CoMO
FROM [Freq List], LicenceData
WHERE ((([LicenceData].class_stat) Like 'M*') And (([LicenceData].type_lc)="A") And (([LicenceData].frequency) Between FirstOfFrequencyMHz-0.0249 And FirstOfFrequencyMHz+0.0249))
GROUP BY FirstOfFrequencyMHz

综合查询尝试:

SELECT DISTINCT FirstOfFrequencyMHz, 

(SELECT Min(distance) AS Expr1
FROM [Freq List], LicenceData
WHERE ((([LicenceData].class_stat) Like 'F*') And (([LicenceData].type_lc)="A") And (([LicenceData].frequency) Between FirstOfFrequencyMHz-0.0249 And FirstOfFrequencyMHz+0.0249))) as COFX,

(SELECT Min(distance) AS Expr1
FROM [Freq List], LicenceData
WHERE ((([LicenceData].class_stat) Like 'M*') And (([LicenceData].type_lc)="A") And (([LicenceData].frequency) Between FirstOfFrequencyMHz-0.0249 And FirstOfFrequencyMHz+0.0249))) as COMO

FROM [Freq List]

我发布的合并查询尝试未返回正确的值,CoFX和CoMO列的所有单元格都包含相同的数字。

这两个查询独立工作我只需要知道如何组合它们(我需要组合4个总查询,类似于前2个发布的查询)或者甚至可以将它们组合起来。

1 个答案:

答案 0 :(得分:0)

这可能是最好的方法,因为表之间没有直接链接,您需要使用子选择以确保它们被正确引用:

SELECT DISTINCT F.FirstOfFrequencyMHz,
(
SELECT Min(L.Distance)
FROM LicenceData As L
WHERE L.Class_Stat Like 'F*'
AND L.Type_LC = 'A'
AND L.Frequency Between F.FirstOfFrequencyMHz-0.0249 And F.FirstOfFrequency+0.0249
) CoFX,
(
SELECT Min(L2.Distance)
FROM LicenceData As L2
WHERE L2.Class_Stat Like 'M*'
AND L2.Type_LC = 'A'
AND L2.Frequency Between F.FirstOfFrequencyMHz-0.0249 And F.FirstOfFrequency+0.0249
) CoMO
FROM [Freq List] As F

祝你好运:)