在内部加入的声明

时间:2015-01-23 16:36:24

标签: sql inner-join

我正在尝试从客户最近购买或放弃中选择信息,并在Where语句中收到错误消息。

SELECT Orders.EMAILADDRESS as EMAILADDRESS, [All Productions].REGIONID, [All Productions].EVENTTYPE, [All Productions].EVENTCATEGORY, [All Productions].EVENTNAME
FROM Orders INNER JOIN [All Productions]
WHERE Orders.ORDERDATE = (
     Select MAX(Orders.ORDERDATE)
     FROM Orders
     GROUP BY EMAILADDRESS
)
ON Orders. PRODUCTIONID = [All Productions].PRODUCTIONID

UNION
SELECT Carts.EMAILADDRESS as EMAILADDRESS, [All Productions].REGIONID, [All Productions].EVENTTYPE, [All Productions].EVENTCATEGORY, [All Productions].EVENTNAME
FROM Orders INNER JOIN [All Productions]
WHERE Carts.ABANDONDATE = (
     Select MAX(Carts.ABANDONDATE)
     FROM Carts
     GROUP BY EMAILADDRESS
)
ON Carts.ABANDONDID = [All Productions].PRODUCTIONID

3 个答案:

答案 0 :(得分:2)

加入ON条件应该在where子句

之前
SELECT Orders.EMAILADDRESS AS EMAILADDRESS,
       [All Productions].REGIONID,
       [All Productions].EVENTTYPE,
       [All Productions].EVENTCATEGORY,
       [All Productions].EVENTNAME
FROM   Orders
       INNER JOIN [All Productions]
               ON Orders. PRODUCTIONID = [All Productions].PRODUCTIONID
       JOIN (SELECT Max(Orders.ORDERDATE) ORDERDATE,
                    EMAILADDRESS
             FROM   Orders
             GROUP  BY EMAILADDRESS) B
         ON Orders.ORDERDATE = B.ORDERDATE
            AND Orders.EMAILADDRESS = b.EMAILADDRESS
UNION
SELECT Carts.EMAILADDRESS AS EMAILADDRESS,
       [All Productions].REGIONID,
       [All Productions].EVENTTYPE,
       [All Productions].EVENTCATEGORY,
       [All Productions].EVENTNAME
FROM   Orders
       INNER JOIN [All Productions]
               ON Carts.ABANDONDID = [All Productions].PRODUCTIONID
       JOIN (SELECT Max(Carts.ABANDONDATE),EMAILADDRESS
             FROM   Carts
             GROUP  BY EMAILADDRESS) b
         ON b.ABANDONDATE = Carts.ABANDONDATE
            AND Orders.EMAILADDRESS = b.EMAILADDRESS 

答案 1 :(得分:1)

第一个问题是whereon之后(onfrom子句的一部分; where子句跟在from之后}子句)。所以查询的结构不正确。

即使你修改了语法,你还有第二个问题;查询将返回错误,因为子查询很可能会返回多行。第一个是你似乎试图用第一个子查询做的事情:

SELECT o.EMAILADDRESS as EMAILADDRESS, ap.REGIONID,
       ap.EVENTTYPE, [All Productions].EVENTCATEGORY,
       ap.EVENTNAME
FROM Orders as o INNER JOIN
     [All Productions] as ap
     ON o.PRODUCTIONID = ap.PRODUCTIONID
WHERE o.ORDERDATE = (Select MAX(o2.ORDERDATE)
                     FROM Orders as o2
                     WHERE o2.EMAILADDRESS = o.EMAILADRESS
                    );

答案 2 :(得分:0)

您可以使用row_number功能获取每个分组的最新日期:

SELECT * FROM(
    SELECT
        Orders.EMAILADDRESS as EMAILADDRESS,
        [All Productions].REGIONID,
        [All Productions].EVENTTYPE,
        [All Productions].EVENTCATEGORY,
        [All Productions].EVENTNAME,
        row_number() over(partition by Orders.EMAILADDRESS
                          order by Orders.ORDERDATE desc) as LATEST
    FROM Orders
        INNER JOIN [All Productions]
            ON Orders. PRODUCTIONID = [All Productions].PRODUCTIONID
    UNION
    SELECT
        Carts.EMAILADDRESS as EMAILADDRESS,
        [All Productions].REGIONID,
        [All Productions].EVENTTYPE,
        [All Productions].EVENTCATEGORY,
        [All Productions].EVENTNAME,
        row_number() over(partition by Carts.EMAILADDRESS
                          order by Carts.ORDERDATE desc) as LATEST
    FROM Carts
        INNER JOIN [All Productions]
            ON Carts.ABANDONDID = [All Productions].PRODUCTIONID)
WHERE LATEST = 1