我很难理解为什么这两个陈述给出相同的输出:
案例1:
SELECT sum(DISTINCT prod_price)
FROM PRODUCTS
案例2:
SELECT SUM(SQ.COST)
FROM (SELECT DISTINCT [VEND_ID] as PROD,[PROD_PRICE] as COST FROM PRODUCTS) SQ
SELECT * FROM Products
的输出结果如下:
答案 0 :(得分:2)
考虑一下这个问题
SELECT SUM(SQ.COST)
FROM (SELECT DISTINCT [VEND_ID] as PROD,[PROD_PRICE] as COST
FROM PRODUCTS) SQ
AS
SELECT DISTINCT [VEND_ID] as PROD,[PROD_PRICE] as COST FROM PRODUCTS
进入临时表称为SQ,有两列:
首先评估此查询,然后评估下一部分:
SELECT SUM(SQ.COST)
FROM SQ
这与您的原始查询相同,因此答案相同。
答案 1 :(得分:0)
您的Product表中有6个不同的product_price
和vend_id
组合。
巧合的是,如果我只采用product_price
,那么还有6个不同的记录:
3.49
5.99
8.99
11.99
4.99
9.49.
因此总和是相同的