sql group by all

SELECT 书名,出版社,SUM(金额) AS 总计金额
FROM tb_BookSell
WHERE 单价=48 or 单价=20
GROUP BY ALL 书名,出版社

 

相当于

SELECT
    *
FROM (SELECT
    *,
    ROW_NUMBER() OVER (PARTITION BY t.书名, t.出版社 ORDER BY t.总计金额 DESC) AS mm
FROM (SELECT
    书名,
    出版社,
    SUM(金额) AS 总计金额
FROM tb_BookSell
WHERE 单价 = 48 OR 单价 = 20
GROUP BY    书名,
            出版社 UNION ALL SELECT
    书名,
    出版社,
    NULL AS 总计金额
FROM tb_BookSell
WHERE 单价 != 48 AND 单价 != 20
GROUP BY    书名,
            出版社)
t)
t2
WHERE t2.mm = 1

 

你可能感兴趣的:(我的数据库3)