一个查询中每个州的顶级城市

时间:2014-05-29 07:54:41

标签: sql oracle sum

我在解决这个问题时遇到了问题,即如何根据销售情况获得每个州的最高城市。只选择每个州的最高城市。这是我的代码

SELECT c.STATE, C.CITY,  SUM(qty * saleprice) AS "TOTAL SALES"
FROM DWCUST C
INNER JOIN DWSALE S
ON C.DWCUSTID = S.DWCUSTID
GROUP BY c.STATE, C.CITY
ORDER BY 1;

提前致谢

1 个答案:

答案 0 :(得分:0)

试试这个! (IN SQL-SERVER

select * from 
(
SELECT c.STATE, C.CITY,TOTAL_SALES=row_number()over(partition by c.STATE order by  SUM(qty * saleprice))
FROM DWCUST C
INNER JOIN DWSALE S
ON C.DWCUSTID = S.DWCUSTID
GROUP BY c.STATE, C.CITY
)x1
where x1.TOTAL_SALES=1

可能在oracle工作吗?

 select * from 
    (
    SELECT c.STATE, C.CITY,SUM(qty * saleprice) as x1,row_number()over(partition by c.STATE order by x1))TOTAL_SALES
    FROM DWCUST C
    INNER JOIN DWSALE S
    ON C.DWCUSTID = S.DWCUSTID
    GROUP BY c.STATE, C.CITY
    )x
    where x.TOTAL_SALES=1