SQL Group By和Inner Join语法

时间:2014-08-19 14:56:05

标签: sql ms-access syntax group-by inner-join

SELECT t.KeyID, t.VaR, t.DB
FROM 
(
    SELECT t1.[KeyID], t1.[VaR], '383' AS DB FROM Table1 AS t1
    UNION ALL  
    SELECT t2.[KeyID], t2.[VaR], '55' AS DB FROM Table2 AS t2
)  AS t
INNER JOIN

(
    (
        SELECT t3.[KeyID], t3.[VaR] FROM Table1 AS t3
        UNION ALL  
        SELECT t4.[KeyID], t4.[VaR] FROM Table2 AS t4
    ) AS u
    GROUP BY u.KeyID, u.VaR
) ON t.KeyID=u.KeyID AND t.VaR=u.VaR

ORDER BY t.KeyID, t.VaR, t.DB;

当我在MS Access 2010中运行上述SQL语句时,我在JOIN操作"中得到" SYntax错误。但是,如果我删除" GROUP BY"子句语法运行正常。

有什么想法吗?

4 个答案:

答案 0 :(得分:1)

我只能GROUP BY SELECT SELECT的主题,因此您需要添加额外的SELECT t.KeyID, t.VaR, t.DB FROM ( SELECT t1.[KeyID], t1.[VaR], '383' AS DB FROM Table1 AS t1 UNION ALL SELECT t2.[KeyID], t2.[VaR], '55' AS DB FROM Table2 AS t2 ) AS t INNER JOIN ( SELECT u.[KeyID], u.[VaR] FROM ( SELECT t3.[KeyID], t3.[VaR] FROM Table1 AS t3 UNION ALL SELECT t4.[KeyID], t4.[VaR] FROM Table2 AS t4 ) AS u GROUP BY u.KeyID, u.VaR ) ON t.KeyID=u.KeyID AND t.VaR=u.VaR ORDER BY t.KeyID, t.VaR, t.DB; 条款才能​​使其发挥作用。类似的东西:

{{1}}

答案 1 :(得分:1)

如果没有相应的GROUP BY,则SELECT无效。如果您希望GROUP BY应用于第二个UNION ALL的结果,则需要将该结果用作派生表 - 也就是说,您需要从中选择它,然后您将能够申请GROUP BY:

SELECT t.KeyID, t.VaR, t.DB
FROM 
(
    SELECT t1.[KeyID], t1.[VaR], '383' AS DB FROM Table1 AS t1
    UNION ALL  
    SELECT t2.[KeyID], t2.[VaR], '55' AS DB FROM Table2 AS t2
)  AS t
INNER JOIN

(
    SELECT u.KeyID, u.VaR
    FROM
    (
        SELECT t3.[KeyID], t3.[VaR] FROM Table1 AS t3
        UNION ALL  
        SELECT t4.[KeyID], t4.[VaR] FROM Table2 AS t4
    ) AS u
    GROUP BY u.KeyID, u.VaR
) ON t.KeyID=u.KeyID AND t.VaR=u.VaR

ORDER BY t.KeyID, t.VaR, t.DB;

另一方面,您似乎只想消除组合集中的重复项。在这种情况下,您只需要用UNION替换UNION ALL:

SELECT t.KeyID, t.VaR, t.DB
FROM 
(
    SELECT t1.[KeyID], t1.[VaR], '383' AS DB FROM Table1 AS t1
    UNION ALL  
    SELECT t2.[KeyID], t2.[VaR], '55' AS DB FROM Table2 AS t2
)  AS t
INNER JOIN

    (
        SELECT t3.[KeyID], t3.[VaR] FROM Table1 AS t3
        UNION
        SELECT t4.[KeyID], t4.[VaR] FROM Table2 AS t4
    ) AS u
    ON t.KeyID=u.KeyID AND t.VaR=u.VaR

ORDER BY t.KeyID, t.VaR, t.DB;

答案 2 :(得分:0)

此代码必须正常工作

SELECT t.KeyID, t.VaR, t.DB
FROM 
(
    SELECT t1.[KeyID], t1.[VaR], '383' AS DB FROM Table1 AS t1
    UNION ALL  
    SELECT t2.[KeyID], t2.[VaR], '55' AS DB FROM Table2 AS t2
)  AS t
INNER JOIN

(
    select * from
    (
        SELECT t3.[KeyID], t3.[VaR] FROM Table1 AS t3
        UNION ALL  
        SELECT t4.[KeyID], t4.[VaR] FROM Table2 AS t4
    ) AS u1
    GROUP BY u1.KeyID, u1.VaR
) as u ON t.KeyID=u.KeyID AND t.VaR=u.VaR

ORDER BY t.KeyID, t.VaR, t.DB;

答案 3 :(得分:0)

我不确定你想要计算什么,但对我来说,看起来你正试图对t3 UNION t4的结果返回的行进行分组。在这种情况下,您可以尝试以下方法:

SELECT t.KeyID, t.VaR, t.DB
FROM 
(
    SELECT t1.[KeyID], t1.[VaR], '383' AS DB FROM Table1 AS t1
    UNION ALL  
    SELECT t2.[KeyID], t2.[VaR], '55' AS DB FROM Table2 AS t2
)  AS t
INNER JOIN

(
    (

SELECT KeyID, VaR
FROM
(
        SELECT t3.[KeyID], t3.[VaR] FROM Table1 AS t3
        UNION ALL  
        SELECT t4.[KeyID], t4.[VaR] FROM Table2 AS t4
)
GROUP BY KeyID, VaR
    ) AS u


) ON t.KeyID=u.KeyID AND t.VaR=u.VaR

ORDER BY t.KeyID, t.VaR, t.DB;