JOIN SELECT子查询的替代方法

时间:2016-01-27 16:44:52

标签: sql oracle netezza

我正在尝试将SELECT子查询转换为JOIN语句,因此它适用于Netezza。我最初在Oracle数据库上工作,其中以下查询工作正常,但Netezza不支持SELECT语句中的子查询。我设法将SELECT子查询转换为ON语句,但Netezza也不支持。

我的查询是通过查询每日销售量和该日期的历史价格来查找单个产品销售的每日收入。

有关如何将此声明分解为Netezza可接受的内容的任何建议?我也有兴趣知道我的查询的任何重组是否会提高其效率。

我原来的Oracle SQL查询:

Select
    SALES.DATE,
    SALES.PRODUCT,
    SALES.QUANTITY,
    (
        Select PRICE
        from
        (
            Select PRODUCT_ID, PRICE, max(EFF_DATE) as EFF_DATE
            from HIST_PRICING
            Where
                PRODUCT_ID = SALES.PRODUCT and
                SALES.DATE > EFF_DATE
            GROUP BY
                PRODUCT_ID, PRICE
        )
    ) as PRICE,
    (SALES.QUANTITY * PRICE) as REVENUE
FROM SALES_RECORDS SALES
;

将子查询移动到JOIN ON语句:

SELECT
    SALES.DATE,
    SALES.PRODUCT,
    SALES.QUANTITY,
    H.PRICE,
    (SALES.QUANTITY * H.PRICE) as REVENUE
FROM SALES_RECORDS SALES
LEFT JOIN HIST_PRICING H ON
    SALES.PRODUCT = H.PRODUCT and
    SALES.DATE =
        (
            Select MAX(EFF_DATE) AS MOST_RECENT
            FROM HIST_PRICING
            WHERE SALES.PRODUCT = HIST_PRICING.PRODUCT
              AND EFF_DATE <= SALES.DATE
            GROUP BY SALES.PRODUCT
        )

作为参考,这里是我的表格数据的简化示例。

╔═════════════════════════════════════╗
║           SALES_RECORDS             ║
╠═══════════╦═════════╦═══════════════╣
║   DATE    ║ PRODUCT ║ QUANTITY_SOLD ║
╠═══════════╬═════════╬═══════════════╣
║ 1/1/2015  ║ SHOES   ║           500 ║
║ 2/5/2015  ║ SHOES   ║          1200 ║
║ 3/7/2015  ║ TOYS    ║           600 ║
║ 3/9/2015  ║ SHOES   ║           100 ║
║ 5/10/2015 ║ HATS    ║           400 ║
╚═══════════╩═════════╩═══════════════╝
╔══════════════════════════════╗
║          HIST_PRICING        ║
╠═══════════╦═════════╦════════╣
║ EFF_DATE  ║ PRODUCT ║  PRICE ║
╠═══════════╬═════════╬════════╣
║ 1/1/2015  ║ SHOES   ║ $50    ║
║ 1/1/2015  ║ TOYS    ║ $10    ║
║ 1/1/2015  ║ HATS    ║ $20    ║
║ 2/15/2015 ║ SHOES   ║ $45    ║
║ 2/15/2015 ║ HATS    ║ $15    ║
║ 3/1/2015  ║ HATS    ║ $20    ║
║ 5/1/2015  ║ TOYS    ║ $15    ║
║ 8/1/2015  ║ SHOES   ║ $55    ║
╚═══════════╩═════════╩════════╝

2 个答案:

答案 0 :(得分:0)

如果您可以在FROM子句中执行内联视图...或者,如果您拥有CREATE VIEW的DBA权限,那么您可以执行此操作:

Select
    SALES."DATE",
    SALES.PRODUCT,
    SALES.QUANTITY,
    PRICES.PRICE,
    (SALES.QUANTITY * PRICES.PRICE) as REVENUE
FROM SALES_RECORDS SALES LEFT JOIN
        (
            Select PRODUCT_ID, PRICE, max(EFF_DATE) as EFF_DATE
            from HIST_PRICING
            GROUP BY
                PRODUCT_ID, PRICE
        ) PRICES ON PRICES.PRODUCT_ID = SALES.PRODUCT AND PRICES.EFF_DATE <= SALES."DATE"
;

否则,你可以这样做:

Select
    SALES."DATE",
    SALES.PRODUCT,
    SALES.QUANTITY,
    PRICES.PRICE,
    (SALES.QUANTITY * PRICES.PRICE) as REVENUE
FROM SALES_RECORDS SALES LEFT JOIN HIST_PRICING PRICES ON PRICES.PRODUCT_ID = SALES.PRODUCT AND PRICES.EFF_DATE <= SALES."DATE"
WHERE NOT EXISTS ( SELECT 'later price for product prior to sales date'
                   FROM   hist_pricing p2
                   WHERE  p2.product_id = prices.product_id
                   AND    p2.eff_date <= sales."DATE"
                   -- NOTE: too simple - assumes you never have two prices for the same product on the same date.  
                   -- If that can happen, you need to adjust the logic below to include a tie-breaker.
                   AND    p2.eff_date > prices.eff_date )
;                   

Oracle有各种方法可以改进这两种方法(例如MAX() KEEP)。但这些是两种相当普遍的SQL方法来重新表达原始SQL并摆脱标量子查询。

答案 1 :(得分:0)

对于任务通过查找每日销售量和该日期的历史价格来查找单个产品销售的每日收入我会采用这种方法:

在第一步中定义(延长)产品价格有效期间隔。 这是通过一个简单的分析函数完成的(如果Netezza不支持,则替代自连接)。

select PRODUCT_ID, EFF_DATE eff_date_from, 
nvl(lead(EFF_DATE-1) over (partition by product_id order by EFF_DATE),to_date('1/1/2100','mm/dd/yyyy')) eff_date_to,
PRICE from HIST_PRICING order by PRODUCT_ID, EFF_DATE

使用PRICE和有效的FROM - 有效TO日期生成价格表。 请注意,这两个日期都包含在内(使用-1天完成),最后一个TO日期在远期,这可以使用BETWEEN进行简单的过滤。

注意 - 这仅适用于DATE(没有时间组件)。如果有效列也包含时间,则仅减去最小单位,例如1秒。

PRODUCT_ID EFF_DATE_FROM       EFF_DATE_TO              PRICE
---------- ------------------- ------------------- ----------
HATS       01.01.2015 00:00:00 14.02.2015 00:00:00         20 
HATS       15.02.2015 00:00:00 28.02.2015 00:00:00         15 
HATS       01.03.2015 00:00:00 01.01.2100 00:00:00         20 
SHOES      01.01.2015 00:00:00 14.02.2015 00:00:00         50 
SHOES      15.02.2015 00:00:00 31.07.2015 00:00:00         45 
SHOES      01.08.2015 00:00:00 01.01.2100 00:00:00         55 
TOYS       01.01.2015 00:00:00 30.04.2015 00:00:00         10 
TOYS       01.05.2015 00:00:00 01.01.2100 00:00:00         15

查询是一个简单的连接(如果历史表可能不完整,则使用外部连接,如果历史表可能不完整),并使用sales_date限制价格有效性。

Select
    SALES."DATE",
    SALES.PRODUCT_ID,
    SALES.QUANTITY,
    (SALES.QUANTITY * PRICE) as REVENUE,
    PRICE
from  SALES_RECORDS  SALES
join 
(  
select PRODUCT_ID, EFF_DATE eff_date_from, 
nvl(lead(EFF_DATE-1) over (partition by product_id order by EFF_DATE),to_date('1/1/2100','mm/dd/yyyy')) eff_date_to,
PRICE from HIST_PRICING order by PRODUCT_ID, EFF_DATE
) DAILY_PRICE
on SALES.PRODUCT_ID = DAILY_PRICE.PRODUCT_ID and
 SALES."DATE" BETWEEN DAILY_PRICE.eff_date_from and DAILY_PRICE.eff_date_to
;