简体   繁体   English

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

[英]SQL Query to get 2nd most recent results with multiple columns

I am trying to obtain the 2nd most recent results for all distinct part_id's(based off of order_date) to go into a report I am working on making to compare it to the most recent results.我正在尝试将 go 的所有不同 part_id(基于 order_date)的第二个最新结果放入我正在制作的报告中,以将其与最新结果进行比较。

The commented sections are from another approach I was trying but unsuccessful with.评论部分来自我尝试但未成功的另一种方法。

Any help is greatly appreciated!任何帮助是极大的赞赏!

(Side note: I am new to posting on SO, and I apologize in advance if this is answered elsewhere, but I was unable to find anything that pertained to this issue) (旁注:我是在 SO 上发帖的新手,如果在其他地方得到回答,我提前道歉,但我找不到与此问题有关的任何内容)

I am using the following query:我正在使用以下查询:

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))

EDIT 5/28 LATE NIGHT:编辑 5/28 深夜:

Lets say below is the data set I need the 2nd result from each part_id (2nd based off of ORDER_DATE DESC)可以说下面是我需要每个 part_id 的第二个结果的数据集(第二个基于 ORDER_DATE DESC)

+-------------+---------+-----------+
| 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      |
+-------------+---------+-----------+

I am looking to receive the following output (for more than 3 different part id's):我希望收到以下 output(超过 3 个不同的零件 ID):

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

There are also additional columns in the query but formatting them as a table would have taken much longer.查询中还有其他列,但将它们格式化为表会花费更长的时间。 I have left off a few of the columns on the examples.我已经省略了示例中的一些列。

ANOTHER EDIT另一个编辑

I am not sure if this information helps, but I am trying to compare the most recent results to the previous results to show pricing and vendor differences.我不确定这些信息是否有帮助,但我正在尝试将最近的结果与之前的结果进行比较,以显示定价和供应商的差异。 We are compiling the data into Report Builder;我们正在将数据编译到报表生成器中; My approach here was to create 2 separate datasets one with the most recent and the other with the 2nd most recent and combine the data from the datasets in Report Builder.我的方法是创建 2 个单独的数据集,一个是最新的,另一个是第二个最新的,然后在 Report Builder 中合并来自数据集的数据。 If there is an easier approach and I am heading in the wrong direction please let me know!如果有更简单的方法并且我正朝着错误的方向前进,请告诉我!

Example:例子:

+------------+---------+-----------+-------------+----------+------------+
| 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       |
+------------+---------+-----------+-------------+----------+------------+

EDIT THE NEXT MORNING编辑第二天早上

Thanks everyone for all the help.感谢大家的帮助。 After Harry posted his solution I went ahead and made some tiny edits to get the columns I needed added on.在 Harry 发布了他的解决方案之后,我继续进行了一些微小的编辑以添加我需要的列。 I swapped his union portion with the original select statement.我用原来的 select 语句交换了他的联合部分。 Everything here seems to be exactly what I am looking for!这里的一切似乎正是我正在寻找的!

Code:代码:

;
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

I think something like this would work to get the second most recent order:我认为这样的事情可以得到第二个最近的订单:

;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

Based on the data you have provided, you can do this with a cte and row number function.根据您提供的数据,您可以使用 cte 和行号 function 执行此操作。

Note - it always helps to show the whole picture rather than just ask for the part you want help with (usually).. as it is easier to answer if we can understand the entire issue!注意 - 它总是有助于显示整个画面,而不是仅仅要求您需要帮助的部分(通常).. 因为如果我们能够理解整个问题,更容易回答!

See code below请参阅下面的代码

;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

result结果

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JOINS查找第二个最近日期SQL - Find 2nd Most Recent Date SQL with JOINS sql查询以获取给定一组列的最新数据 - sql query to get the most recent piece of data for a given set of columns SQL 查询以获取倒数第二行 - SQL query to get 2nd last row SQL 查询与 2 个表的连接,并在第二个表中使用多个外键在结果中创建一组新的列 - SQL Query with join for 2 tables and create new set of columns in result with multiple foreign key in 2nd table T SQL:在第二个表中查找行-最近的日期-当前表中的日期之前 - T SQL: Find row in 2nd Table - with most recent Date - Before Date in current Table 在多列上使用 GROUP BY 获取最新的 ROW - Get most recent ROW with a GROUP BY on multiple columns 对多个表的 SQL 查询以获取最新数据 - SQL query to multiple tables for the most recent data SQL查询返回最常见,第二最常见,第三最常见的情况 - SQL query to return most common, 2nd most common, 3rd most common occurrences MS SQL 2008:使用多个没有PK的列删除“重复项”以选择最新结果 - MS SQL 2008 : Delete “duplicates” to select most recent results using multiple columns with no PK SQL查询以找出不同的行是否具有相同的第二列 - SQL query to find out if different rows with same 2nd columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM