sql查询以获取具有多个列的第二个最新结果

g6baxovj  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(296)

我正在尝试获取所有不同部分id的第二个最新结果(基于订单日期),以进入我正在制作的报告中,将其与最新结果进行比较。
评论的部分是来自另一种方法,我尝试,但不成功。
非常感谢您的帮助!
(旁注:我是一个新的如此张贴,我提前道歉,如果这是回答其他地方,但我找不到任何有关这个问题)
我正在使用以下查询:

SELECT
                              PURCHASE_ORDER.ORDER_DATE
                            , PURC_ORDER_LINE.PART_ID
                            , PURCHASE_ORDER.VENDOR_ID
                            , PURC_ORDER_LINE.LINE_STATUS
                            , PURC_ORDER_LINE.ORDER_QTY
                            , PURC_ORDER_LINE.UNIT_PRICE
                            --, ROW_NUMBER() over (ORDER BY PURCHASE_ORDER.ORDER_DATE DESC)AS ROW
                            , CAST (PURC_ORDER_LINE.ORDER_QTY * PURC_ORDER_LINE.UNIT_PRICE AS VARCHAR) AS TOTAL_COST
                    FROM
                              PURCHASE_ORDER
                              INNER JOIN
                                        PURC_ORDER_LINE
                              ON
                                        PURCHASE_ORDER.ID = PURC_ORDER_LINE.PURC_ORDER_ID

                    WHERE PURCHASE_ORDER.ORDER_DATE < (SELECT MAX(ORDER_DATE) FROM PURCHASE_ORDER) AND PURC_ORDER_LINE.PART_ID = 'XXXX'
                    ORDER BY ORDER_dATE DESC

                    --WHERE PURC_ORDER_LINE.PART_ID = 'XXXX' and PURCHASE_ORDER.ORDER_DATE = (SELECT MAX(ORDER_DATE) FROM PURCHASE_ORDER WHERE ORDER_DATE < (SELECT MAX(ORDER_DATE) FROM PURCHASE_ORDER))

编辑5/28深夜:
下面是我需要的数据集,每个零件id的第二个结果(第二个基于订单日期描述)

+-------------+---------+-----------+
| ORDER_DATE  | PART_ID | VENDOR_ID |
+-------------+---------+-----------+
| 2020-05-29  | XXXX    | CVVB      |
| 2020-05-27  | XXXX    | CVVB      |
| 2020-05-28  | XXXX    | CVVA      |
| 2020-05-28  | YYYY    | GGNB      |
| 2020-04-12  | YYYY    | GGNB      |
| 2020-02-08  | YYYY    | GGNB      |
| 2020-05-28  | ZZZZ    | LLNB      |
| 2019-10-28  | ZZZZ    | LLNB      |
| 2019-05-27  | ZZZZ    | OKIJ      |
+-------------+---------+-----------+

我希望收到以下输出(对于3个以上不同的零件id):

+------------+---------+-----------+
| ORDER_DATE | PART_ID | VENDOR_ID |
+------------+---------+-----------+
| 2020-05-28 | XXXX    | CVVA      |
| 2020-04-12 | YYYY    | GGNB      |
| 2019-10-28 | ZZZZ    | LLNB      |
+------------+---------+-----------+

查询中还有其他列,但将它们格式化为表将花费更长的时间。我删去了几个例子的专栏。
另一个编辑
我不确定这些信息是否有用,但我正在尝试将最近的结果与以前的结果进行比较,以显示定价和供应商之间的差异。我们正在将数据编译成报表生成器;我在这里的方法是创建两个独立的数据集,一个是最新的,另一个是第二个,并在报表生成器中组合来自数据集的数据。如果有一个更容易的方法,我在错误的方向前进,请让我知道!
例子:

+------------+---------+-----------+-------------+----------+------------+
| ORDER_DATE | PART_ID | VENDOR_ID | Porder_Date | Ppart_ID | pVendor_id |
+------------+---------+-----------+-------------+----------+------------+
| 2020-05-29 | XXXX    | CVVB      | 2020-05-28  | XXXX     | CVVA       |
| 2020-05-28 | YYYY    | GGNB      | 2020-04-12  | YYYY     | GGNB       |
| 2020-05-28 | ZZZZ    | LLNB      | 2019-10-28  | ZZZZ     | LLNB       |
+------------+---------+-----------+-------------+----------+------------+

第二天早上编辑
谢谢大家的帮助!在harry发布了他的解决方案之后,我继续进行了一些小的编辑,以获得我需要添加的专栏。我用原来的select语句替换了他的union部分。这里的一切似乎正是我要找的!
代码:

;
WITH mycte AS
          (
                    SELECT
                              PURCHASE_ORDER.ORDER_DATE
                            , PURC_ORDER_LINE.PART_ID
                            , PURCHASE_ORDER.VENDOR_ID
                            , PURC_ORDER_LINE.LINE_STATUS
                            , PURC_ORDER_LINE.ORDER_QTY
                            , PURC_ORDER_LINE.UNIT_PRICE
                            , CAST (PURC_ORDER_LINE.ORDER_QTY * PURC_ORDER_LINE.UNIT_PRICE AS VARCHAR) AS TOTAL_COST
                    FROM
                              PURCHASE_ORDER
                              INNER JOIN
                                        PURC_ORDER_LINE
                              ON
                                        PURCHASE_ORDER.ID = PURC_ORDER_LINE.PURC_ORDER_ID
          )
        , mycte2 AS
          (
                    SELECT
                              CONVERT(DATE,order_date) AS order_date
                            , part_id
                            , vendor_id
                            , order_qty
                            , unit_price
                            , total_cost
                            , ROW_NUMBER() over(
                                      PARTITION BY part_id
                                      ORDER BY
                                                CONVERT(DATE,order_date) DESC) AS row_num
                    FROM
                              mycte
          )
SELECT
          mycte2.order_date
        , mycte2.part_id
        , mycte2.vendor_id
        , mycte2.order_qty
        , mycte2.unit_price
        , mycte2.total_cost
        , previous.order_date porder_date
        , previous.part_id    ppart_id
        , previous.vendor_id  pvendor_id
        , previous.order_qty  poqrder_qty
        , previous.unit_price punit_price
        , previous.total_cost ptotal_cost
FROM
          mycte2
          LEFT JOIN
                    mycte2 previous
          ON
                    previous.row_num   = mycte2.row_num +1
                    AND mycte2.part_id = previous.part_id
WHERE
          mycte2.row_num = 1
5kgi1eie

5kgi1eie1#

根据您提供的数据,您可以使用cte和行号函数来实现这一点。
注意-它总是有助于显示整个图片,而不是仅仅要求你需要帮助的部分(通常)。。因为如果我们能理解整个问题,回答起来就容易多了!
参见下面的代码

;with mycte as (

select 
 '2020-05-29' as order_date  , 'XXXX'  as part_id   , 'CVVB' as vendor_id
 union all select
 '2020-05-27'  , 'XXXX'    , 'CVVB'  
  union all select
 '2020-05-28'  , 'XXXX'    , 'CVVA'      
  union all select
 '2020-05-28'  , 'YYYY'    , 'GGNB'     
  union all select
 '2020-04-12'  , 'YYYY'    , 'GGNB'      
  union all select
 '2020-02-08'  , 'YYYY'    , 'GGNB'     
  union all select
 '2020-05-28'  , 'ZZZZ'    , 'LLNB'      
  union all select
 '2019-10-28' , 'ZZZZ'    , 'LLNB'    
  union all select
 '2019-05-27'  , 'ZZZZ'    , 'OKIJ' 
 )
 , mycte2 as (
 Select 
  convert(date,order_date) as order_date
 ,part_id
 ,vendor_id
 ,ROW_NUMBER() over( partition by part_id order by convert(date,order_date) desc) as row_num
 from mycte
)

Select 
mycte2.order_date
,mycte2.part_id
,mycte2.vendor_id
,previous.order_date porder_date
,previous.part_id ppart_id
,previous.vendor_id pvendor_id
from  mycte2

 left join mycte2 previous
    on previous.row_num = mycte2.row_num +1
    and mycte2.part_id = previous.part_id

where mycte2.row_num = 1

结果

rt4zxlrg

rt4zxlrg2#

我想这样的方法可以得到第二个最新订单:

;WITH cteOrders AS (
 SELECT ROW_NUMBER() OVER (ORDER BY Order_Date DESC) AS row_num,
    PURCHASE_ORDER.ORDER_DATE
    , PURC_ORDER_LINE.PART_ID
    , PURCHASE_ORDER.VENDOR_ID
    , PURC_ORDER_LINE.LINE_STATUS
    , PURC_ORDER_LINE.ORDER_QTY
    , PURC_ORDER_LINE.UNIT_PRICE
 FROM PURCHASE_ORDER 
 INNER JOIN PURC_ORDER_LINE ON PURCHASE_ORDER.ID = PURC_ORDER_LINE.PURC_ORDER_ID
 WHERE PURCHASE_ORDER.ORDER_DATE < (SELECT MAX(ORDER_DATE) FROM PURCHASE_ORDER) AND PURC_ORDER_LINE.PART_ID = 'XXXX'
 )
 SELECT * FROM cteOrders WHERE row_num = 2

相关问题