SQL将先前的订单与客户的当前订单进行比较以比较项目

时间:2014-08-12 23:59:14

标签: sql

我需要一个查询,而我似乎无法使其正常工作。

我的数据如下:

╔══════════╦═════════╦═════════╦═══════╦════════════╗
║ OrderNum ║ Custnum ║  Items  ║ Units ║    Date    ║
╠══════════╬═════════╬═════════╬═══════╬════════════╣
║    15985 ║   75369 ║ Nuts    ║    12 ║ 02/15/2014 ║
║    15985 ║   75396 ║ Berries ║    14 ║ 02/15/2014 ║
║    65894 ║   75369 ║ Nuts    ║    10 ║ 03/23/2014 ║
║    65894 ║   75396 ║ Tarts   ║    14 ║ 03/23/2014 ║
║    95473 ║   75396 ║ Nuts    ║     3 ║ 06/01/2014 ║
║    95473 ║   75396 ║ Tarts   ║    19 ║ 06/01/2014 ║
║    95473 ║   75396 ║ Berries ║    19 ║ 06/01/2014 ║
╚══════════╩═════════╩═════════╩═══════╩════════════╝

我需要报告数百名客户和约25项。

我要求显示的是当前订单,最高订单号,并将其与上次客户订单进行比较。显示查询的输出将是:

╔═════════╦═════════╦═══════╦═══════════════╗
║ Current ║ Current ║       ║               ║
║ Custnum ║  Items  ║ Units ║   Comments    ║
╠═════════╬═════════╬═══════╬═══════════════╣
║   75396 ║ Nuts    ║     3 ║ Decreased(-7) ║
║   75396 ║ Tarts   ║     5 ║ Increase(5)   ║
║   75396 ║ Berries ║    19 ║ New           ║
╚═════════╩═════════╩═══════╩═══════════════╝

如果单位从先前到现在相同,则评论将为“无变化”。

我无法创建只提取当前订单和先前订单进行比较的查询。我试过了MAX(Date)MAX(ORDERNUM) ......

我有大量的Excel并限制访问权限背景,无法在我的脑海中或论坛上找到答案。任何帮助将不胜感激。

最近的查询看起来像:

Select CustNum ,OrderNum ,Items ,Units 
   ,Case Date > (Select MAX(Date) - 2 From TABLE)
   Then Date
   End
From TABLE

Where CustNum IN (
    etc.....
 )

通过此查询,我确实收到了客户的所有订单和带有单位的项目。

2 个答案:

答案 0 :(得分:0)

你需要像这样的查询(从头开始编写,因此可能包含语法错误,mysql语法):

select q2.CustNum, q2.Items, q2.Units as LastOrderUnits, ifnull((
    select Units
    from table as b
    where b.CustNum=q2.CustNum and b.Items=q2.Items and b.OrderNum<q1.maxOrderNum
    order by b.OrderNum desc
    limit 1
), 0) as prevOrderUnits
from
(
    select CustNum, max(OrderNum) as maxOrderNum
    from table
    group by CustNum
) as q1
inner join
table as q2
on (q1.CustNum = q2.CustNum and q1.maxOrderNum = q2.OrderNum)

此查询将为您提供有关上次订单数量和之前订单数量的信息,您可以使用if..then...进一步修改它以获得所需的输出

答案 1 :(得分:0)

试试这个:

with last2_orders as (
  select * 
  from (
    select dense_rank() over(partition by custnum order by ordernum desc) as orderseq, orders.*
    from orders) as temp 
  where orderseq <=2
),
last_orders as(
  select * 
  from last2_orders
  where orderseq = 1),
prior_orders as (
  select *
  from last2_orders
  where orderseq = 2),
temp as (
  select
    last_orders.custnum,
    last_orders.items,
    last_orders.units,
    last_orders.units - coalesce(prior_orders.units, 0) as changes
  from last_orders
  left join prior_orders
  on last_orders.custnum = prior_orders.custnum
    and last_orders.items = prior_orders.items)
select 
  temp.*,
  case 
    when changes>0 and changes<> units 
      then 'Decreased('+convert(varchar,changes)+')' 
    when changes>0 and changes = units 
      then 'New' 
    when changes<0 then 'Increase('+convert(varchar,changes)+')' 
    else 'No Change'
  end as comment
from temp
  1. 第一个临时表通过使用获得所有客户的最后2个订单 dens_rank函数。
  2. 然后你可以加入last_orders和prior_orders 使用客户编号和项目名称来获得最后两个订单之间的差异。
  3. 结果:

    CUSTNUM   ITEMS     UNITS   CHANGES     COMMENT
    75396     Nuts      3       -7        Increase(-7)
    75396     Tarts     19      5         Decreased(5)
    75396     Berries   19      19        New
    

    这是Sql Fiddle。您应该添加更多客户的数据并进行测试。