我想获得前2本昂贵的书籍和2张昂贵的书价格使用first_value&last_value SQL Server
零的存在给出了不正确的最小价格价值,我希望最小价格忽略nulls
Select top 2 FIRST_VALUE(price) Over(Order by price) as MinPrice,
FIRST_VALUE(title) Over (order by price) as MinName,
LAST_VALUE(price) Over (order by price desc) as MaxPrice,
LAST_VALUE(title) over (Order by price desc) as MaxName
from titles;
获取此输出
MINPrice MINName Maxprice MaxName
NULL The Psychology of Computer $22.95 But is it Friendly?
NULL The Psychology of Computer $21.59 Computer Phobic and
我期望的结果应该是
Minprice MinName Maxprice Maxname
$2.99 The Gourmet Microwave $22.95 But is it Friendly?
$2.99 You can Combat stress $21.59 Computer Phobic and
所以我如何消除最低价格
您可以尝试
;WITH ctemin AS
(
SELECT TOP 2 price AS minprice, title AS mintitle FROM titles WHERE price IS NOT NULL ORDER BY price
),
ctemax AS
(
SELECT TOP 2 price AS maxprice, title AS maxtitle FROM titles WHERE price IS NOT NULL ORDER BY price DESC
)
SELECT ctemin.minprice,ctemin.mintitle,ctemax.maxprice,ctemax.maxtitle FROM ctemax
INNER JOIN ctemin ON 1=1
SELECT min(value) FROM table WHERE value IS NOT NULL.
应该是这样的。