如何获得第二低成本的SQL

时间:2018-03-21 11:13:05

标签: sql sql-server tsql

需要查找哪个发票的总价中第二低的价格,其中不包括FiredAlways炉灶的销售。

我可以设法得到最低,但不是第二低。

我有什么:

SELECT TOP 1 WITH TIES
I.InvoiceNbr,
I.InvoiceDt,
I.TotalPrice
FROM INVOICE I 
 WHERE EXISTS(
       SELECT TOP 2 WITH TIES
       I.InvoiceNbr
       FROM INVOICE I
       WHERE EXISTS (
             SELECT FK_InvoiceNbr
             FROM INV_LINE_ITEM
             WHERE FK_StoveNbr NOT IN 
                   (SELECT S.SerialNumber
                   FROM STOVE AS S 
                   WHERE S.Type = 'FiredAlways'))
                   ORDER BY I.TotalPrice DESC)
 GROUP BY I.InvoiceNbr, I.InvoiceDt, I.TotalPrice
 ORDER BY I.TotalPrice ASC;

数据:

[INVOICE](
[InvoiceNbr] [numeric](18, 0) NULL,
[InvoiceDt] [datetime] NULL,
[TotalPrice] [numeric](18, 2) NULL,
[FK_CustomerID] [numeric](18, 0) NULL,
[FK_EmpID] [numeric](18, 0) NULL

[INV_LINE_ITEM](
[LineNbr] [numeric](18, 0) NULL,
[Quantity] [numeric](18, 0) NULL,
[FK_InvoiceNbr] [numeric](18, 0) NULL,
[FK_PartNbr] [numeric](18, 0) NULL,
[FK_StoveNbr] [numeric](18, 0) NULL,
[ExtendedPrice] [numeric](18, 2) NULL

[STOVE](
[SerialNumber] [int] NOT NULL,
[Type] [char](15) NOT NULL,
[Version] [char](15) NULL,
[DateOfManufacture] [smalldatetime] NULL,
[Color] [varchar](12) NULL,
[FK_EmpId] [int] NULL,

通缉输出:

Invoice # date         Price
--------- ------------ -------
 206      02/03/2002     28.11

3 个答案:

答案 0 :(得分:1)

获得 nth low 的两种通用方法:

DECLARE @tbl TABLE(SomeInt INT);
INSERT INTO @tbl VALUES(10),(2),(35),(44),(52),(56),(27);

- 在内部选择上使用TOP n,在外部使用相反顺序的TOP 1

SELECT TOP 1 innertbl.SomeInt
FROM
(
    SELECT TOP 2 SomeInt 
    FROM @tbl
    GROUP BY SomeInt
    ORDER BY SomeInt
) AS innertbl
ORDER BY innertbl.SomeInt DESC;

- 使用CTE DENSE_RANK()(thx to dnoeth for the hint)

WITH AddSortNumber AS
(
    SELECT SomeInt
          ,DENSE_RANK() OVER(ORDER BY SomeInt) AS SortNumber
    FROM @tbl 
)
SELECT SomeInt
FROM AddSortNumber
WHERE SortNumber=2

答案 1 :(得分:0)

试试这个:

WITH cte AS
(
SELECT
I.InvoiceNbr,
I.InvoiceDt,
I.TotalPrice,
dense_rank() over( ORDER BY I.TotalPrice ASC) as dnrnk
FROM INVOICE I 
INNER JOIN 
Inv_LineITEM
ON InvoiceNbr = FK_InvoiceNbr
INNER JOIN
Stove
ON
FK_StoveNbr = SerialNumber
WHERE Type != 'FiredAlways'   
)
select InvoiceNbr,
InvoiceDt,
TotalPrice from cte where dnrnk=2

答案 2 :(得分:-1)

SELECT TOP 1 TotalPrice FROM (
  SELECT TOP 2 TotalPrice FROM invoice
  ORDER BY TotalPrice DESC
) AS em ORDER BY TotalPrice ASC

本质:

按降序排列前2个TotalPrice。 在这2个中,按升序查找最高的TotalPrice。 所选值是第二高的TotalPrice。 如果TotalPrice不相同,则可以改为使用SELECT DISTINCT TOP ...