SQL JOIN和GROUP BY

时间:2016-11-05 22:44:34

标签: sql join group-by

我从5列中提取数据,其中有两个连接,我需要按其中一列进行分组。但是,当我尝试在最后分组时,我收到了错误。我需要某种子查询吗?

SELECT 
    TERRITORY_T.TERRITORYID, 
    TERRITORY_T.TERRITORYNAME, 
    CUSTOMER_T.CUSTOMERID, 
    ORDER_T.ORDERID, 
    PRODUCT_T.PRODUCTID
FROM 
    TERRITORY_T, 
    CUSTOMER_T, 
    ORDER_T, 
    PRODUCT_T, 
    DOESBUSINESSIN_T
WHERE  
    TERRITORY_T.TERRITORYID = DOESBUSINESSIN_T.TERRITORYID
    AND CUSTOMER_T.CUSTOMERID = DOESBUSINESSIN_T.CUSTOMERID
GROUP BY 
    ORDERID
ORDER BY 
    TERRITORYID DESC, 
    CUSTOMERID

客户表和订单表都有CustomerID。我是否需要先查询它们,使用group by,然后与其他表连接?

0 个答案:

没有答案