2

您好,我有以下查询可以从我的系统中获取所有定价。我需要将所有这些选择分组,然后我需要选择第二个最新日期以获得前一天的定价。问题是前一天的定价不是今天 - 1 天。例如,对于某个 stock_id,它可能是今天 - 5 天。我正在通过 excel 中的 microsoft 查询执行此任务,因此无法使用 row_number()。

该查询将输出以下内容:

MS_CODE LOCATION_CODE   MFG_CODE    GROUP_CODE  STOCK_ID  EFFECTIVE_DATE PRICE
COKE    12              23          HTG         23        01/12/2018     1.00
COKE    12              23          HTG         23        01/11/2018     0.99
COKE    12              23          HTG         23        01/10/2018     1.05
PEPSI   12              23          HTG         23        01/12/2018     1.10
PEPSI   12              23          HTG         23        01/11/2018     1.00
PEPSI   12              23          HTG         23        01/10/2018     0.60

我需要输出的是:

MS_CODE LOCATION_CODE   MFG_CODE    GROUP_CODE  STOCK_ID  EFFECTIVE_DATE PRICE
COKE    12              23          HTG         23        01/11/2018     0.99
PEPSI   12              23          HTG         23        01/11/2018     1.00

我当前的查询是:

SELECT
        dbo.PRICE_BOOK.ms_code, 
        dbo.PRICE_BOOK.location_code, 
        dbo.PRICE_BOOK.mfg_code, 
        dbo.PRICE_BOOK.group_code,
        dbo.PRICE_BOOK.stock_id,
        dbo.PRICE_BOOK.effective_date,
        dbo.PRICE_BOOK.price
FROM dbo.PRICE_BOOK

提前致谢。

4

3 回答 3

0

如果我理解正确,这样的事情应该可以工作:

SELECT pb.ms_code, 
       pb.location_code, 
       pb.mfg_code, 
       pb.group_code,
       pb.stock_id,
       MAX(effective_date) effective_date,
       price
FROM dbo.PRICE_BOOK pb
JOIN (SELECT MAX(effective_date) as dte,
             ms_code,
             location_code, 
             mfg_code, 
             group_code,
             stock_id
      FROM dbo.PRICE_BOOK
      GROUP BY ms_code,
               location_code, 
               mfg_code, 
               group_code,
               stock_id) pb2 on pb.ms_code = pb2.ms_code
                            and pb.location_code = pb2.location_code
                            and pb.mfg_code = pb2.mfg_code
                            and pb.group_code = pb2.group_code
                            and pb.stock_id = pb2.stock_id
                            and pb.effective_date < pb2.dte
GROUP BY pb.ms_code, 
         pb.location_code, 
         pb.mfg_code, 
         pb.group_code,
         pb.stock_id,
         price

根据一些谷歌,语法似乎是有效的,但我们会看到。stock_id如果不足以确定正确的日期,可能需要向内部选择/加入添加更多内容。

于 2018-01-12T21:25:13.370 回答
0

考虑一个相关计数子查询来生成row_num,稍后用于选择组中的第二行。

SELECT main.ms_code, 
       main.location_code, 
       main.mfg_code, 
       main.group_code,
       main.stock_id,
       main.effective_date,
       main.price
FROM
 (SELECT t.ms_code, 
         t.location_code, 
         t.mfg_code, 
         t.group_code,
         t.stock_id,
         t.effective_date,
         t.price,
         (SELECT COUNT(*) FROM dbo.PRICE_BOOK sub
          WHERE sub.ms_code = t.ms_code 
            AND sub.location_code = t.location_code
            AND sub.mfg_code = t.mfg_code
            AND sub.group_code = t.group_code
            AND sub.stock_id = t.stock_id
            AND sub.effective_date >= t.effective_date) AS row_num
   FROM dbo.PRICE_BOOK t) AS main
WHERE main.row_num = 2;
于 2018-01-16T15:59:12.843 回答
-1

使用 MS SQL。这将选择第二个最近的日期。我假设dbo.PRICE_BOOK.effective_date是日期字段。

            SELECT * FROM
            (SELECT
                    dbo.PRICE_BOOK.ms_code, 
                    dbo.PRICE_BOOK.location_code, 
                    dbo.PRICE_BOOK.mfg_code, 
                    dbo.PRICE_BOOK.group_code,
                    dbo.PRICE_BOOK.stock_id,
                    dbo.PRICE_BOOK.effective_date,
                    ROW_NUMBER() OVER (ORDER BY dbo.PRICE_BOOK.effective_date) AS ROWNUM,
                    dbo.PRICE_BOOK.price
            FROM dbo.PRICE_BOOK
            ORDER BY dbo.PRICE_BOOK.effective_date DESC
            ) AS TEMP
        WHERE ROWNUM  = MAX(ROWNUM)-1 
        GROUP BY  ms_code, 
                    location_code, 
                    mfg_code, 
                    group_code,
                    stock_id,
                    effective_date,
                    ROWNUM,
                    price
于 2018-01-12T19:38:32.270 回答