有关简化代码的任何提示吗?

时间:2018-03-12 19:01:58

标签: sql

我正在编写sql以满足要求: 如果位置>获得平仓10 添加3行,位置值为800,810,这些新记录的结算库存值将是所有位置的汇总结算库存。 任何简化的线索。我特别想方法不多次阅读累计期末股票。

SELECT 
    I.CLOSING_STOCK_DATE
    ,I.LOCATION as store_id
    , I.SKU
    ,sum(I.CLOSING_STOCK_QTY) as closing_stock_qty
    ,sum(I.CLOSING_STOCK_COST_AMT) as closing_stock_cost_amt
    ,sum(I.CLOSING_STOCK_RETAIL_AMT) as closing_stock_retail_amt
FROM DAILY_CLOSING_STOCK I 
join ASSORTMENT_SEASON asrt_sea
on I.CLOSING_STOCK_DATE>=asrt_sea.ASSORTMENT_SEASON_START_DATE and 
   I.CLOSING_STOCK_DATE<=asrt_sea.ASSORTMENT_SEASON_END_DATE
where asrt_sea.ASSORTMENT_SEASON_CODE = 'S17' and I.LOCATION>10 and 
   (closing_stock_qty <> 0 or closing_stock_cost_amt <> 0 or 
    closing_stock_retail_amt <> 0)
group by I.CLOSING_STOCK_DATE, I."LOCATION", I.SKU
UNION 
SELECT 
    I.CLOSING_STOCK_DATE
    ,'800' as store_id
    , I.SKU
    ,sum(I.CLOSING_STOCK_QTY) as closing_stock_qty
    ,sum(I.CLOSING_STOCK_COST_AMT) as closing_stock_cost_amt
    ,sum(I.CLOSING_STOCK_RETAIL_AMT) as closing_stock_retail_amt
FROM DAILY_CLOSING_STOCK I 
join ASSORTMENT_SEASON asrt_sea
    on I.CLOSING_STOCK_DATE>=asrt_sea.ASSORTMENT_SEASON_START_DATE and             
    I.CLOSING_STOCK_DATE<=asrt_sea.ASSORTMENT_SEASON_END_DATE
where asrt_sea.ASSORTMENT_SEASON_CODE = 'S17' and (closing_stock_qty <> 0                     
    or closing_stock_cost_amt <> 0 or closing_stock_retail_amt <> 0)
group by I.CLOSING_STOCK_DATE, I.SKU
UNION
SELECT 
    I.CLOSING_STOCK_DATE
    ,'810' as store_id
    , I.SKU
    ,sum(I.CLOSING_STOCK_QTY) as closing_stock_qty
    ,sum(I.CLOSING_STOCK_COST_AMT) as closing_stock_cost_amt
    ,sum(I.CLOSING_STOCK_RETAIL_AMT) as closing_stock_retail_amt
FROM DAILY_CLOSING_STOCK I 
join ASSORTMENT_SEASON asrt_sea
    on I.CLOSING_STOCK_DATE>=asrt_sea.ASSORTMENT_SEASON_START_DATE and             
    I.CLOSING_STOCK_DATE<=asrt_sea.ASSORTMENT_SEASON_END_DATE
where asrt_sea.ASSORTMENT_SEASON_CODE = 'S17' and (closing_stock_qty <> 0 
    or closing_stock_cost_amt <> 0 or closing_stock_retail_amt <> 0)
group by I.CLOSING_STOCK_DATE, I.SKU

2 个答案:

答案 0 :(得分:0)

您可以使用&#34; as as&#34;制作临时表,防止您多次加入表。

with t as (
select 
    I.CLOSING_STOCK_DATE
    ,I.LOCATION
    , I.SKU
    ,sum(I.CLOSING_STOCK_QTY) as closing_stock_qty
    ,sum(I.CLOSING_STOCK_COST_AMT) as closing_stock_cost_amt
    ,sum(I.CLOSING_STOCK_RETAIL_AMT) as closing_stock_retail_amt

    FROM DAILY_CLOSING_STOCK I 
    join ASSORTMENT_SEASON asrt_sea
        on I.CLOSING_STOCK_DATE>=asrt_sea.ASSORTMENT_SEASON_START_DATE and             
        I.CLOSING_STOCK_DATE<=asrt_sea.ASSORTMENT_SEASON_END_DATE
    where asrt_sea.ASSORTMENT_SEASON_CODE = 'S17' and (closing_stock_qty <> 0                     
        or closing_stock_cost_amt <> 0 or closing_stock_retail_amt <> 0)
    group by I.CLOSING_STOCK_DATE, I.SKU
)

select
    closing_stock_date,
    location store_id,
    sku,
    closing_stock_qty,
    closing_stock_cost_amt,
    closing_stock_retail_amt
from t
    where location>10
union
select
    closing_stock_date,
    '800' store_id,
    sku,
    closing_stock_qty,
    closing_stock_cost_amt,
    closing_stock_retail_amt
from t
select
    closing_stock_date,
    '810' store_id,
    sku,
    closing_stock_qty,
    closing_stock_cost_amt,
    closing_stock_retail_amt
from t

答案 1 :(得分:0)

嗯,这不会更优雅......你可以选择每个位置的总和。从该数据集中,您可以选择所有位置的结果&gt; 10加上与虚拟位置800,810,820交叉连接的所有位置的总数。

with sums_per_location as
(
  select
    closing_stock_date,
    location as store_id,
    sku,
    sum(closing_stock_qty) as closing_stock_qty,
    sum(closing_stock_cost_amt) as closing_stock_cost_amt,
    sum(closing_stock_retail_amt) as closing_stock_retail_amt
  from daily_closing_stock dcs
  where exists
  (
    select *
    from assortment_season ass
    where dcs.closing_stock_date between ass.assortment_season_start_date
                                     and ass.assortment_season_end_date
    and ass.assortment_season_code = 'S17'
  )
  group by closing_stock_date, location, sku
)
, sums_for_all
(
  select
    closing_stock_date,
    store_ids.store_id,
    sku,
    sum(closing_stock_qty) as closing_stock_qty,
    sum(closing_stock_cost_amt) as closing_stock_cost_amt,
    sum(closing_stock_retail_amt) as closing_stock_retail_amt
  from sums_per_location
  cross join
  (
    select 800 as store_id
    union all
    select 810 as store_id
    union all
    select 820 as store_id
  ) store_ids
  group by closing_stock_date, sku
)
select * from sums_per_location where location > 10
union all
select * from sums_for_all;

我在聚合之前交叉加入,以使查询更短。您可以更改此项并在聚合之后加入,这甚至可以加快查询速度。

根据您的DBMS,您可以使用更优雅的值子句替换store_ids子查询。