SQL组行选择

时间:2018-01-12 19:28:19

标签: sql excel microsoft-query

您好我有以下查询来获取我的系统的所有定价。 我需要将所有这些选择分组,然后我需要选择第二个最新日期来获得前一天的定价。问题是前一天的定价不是今天 - 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

提前致谢。

3 个答案:

答案 0 :(得分: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不足以决定正确的日期,可能需要向内部选择/加入添加更多内容。

答案 1 :(得分:0)

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

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;

答案 2 :(得分:-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