MS-Access -> SELECT AS + ORDER BY = error

前端 未结 7 2061
我在风中等你
我在风中等你 2020-12-07 01:52

I\'m trying to make a query to retrieve the region which got the most sales for sweet products. \'grupo_produto\' is the product type, and \'regiao\' is the region. So I got

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-07 02:18

    Try using a subquery and order the results in an outer query.

    SELECT TOP 1 * FROM
    (
        SELECT
            r.nm_regiao, 
            (SELECT COUNT(*)
             FROM Dw_Empresa
             WHERE grupo_produto='1' AND cod_regiao = d.cod_regiao) as total 
        FROM Dw_Empresa d
        INNER JOIN tb_regiao r ON r.cod_regiao = d.cod_regiao
    ) T1
    ORDER BY total DESC
    

    (Not tested.)

提交回复
热议问题