1

首先,我应该说我发现很难用几句话来表达以下问题,因此问题标题很糟糕。非常欢迎提出改进建议。

现在到真正的问题...

给定以下示例,客户和发票的无序数据...

编辑 我为以下内容创建了一个SQL Fiddle

客户资料

customer_id   name
------------------
1             Gary
2             Jeremy
3             Marcia
4             Danielle

发票数据

invoice_id   customer_id   created_date   amount
------------------------------------------------
1            1             2008-01-01     500.00
2            1             2011-01-01     600.00
3            1             2012-01-01     100.00
4            1             2012-01-01     550.00
5            2             2008-01-01     600.00
6            2             2012-01-01     200.00
7            2             2013-01-01     1000.00
8            3             2012-01-01     300.00
9            3             2013-01-01     100.00
10           3             2009-01-01     250.00
11           4             2010-01-01     300.00
12           4             2011-01-01     700.00
13           4             2012-01-01     500.00

...如何编写查询以通过以下方式返回数据...

  1. 第一行中最早的发票。如果超过 1 张相同年龄的发票,则在这些相同年龄的发票中,金额最大的发票。如果超过 1 张相同年龄和金额的发票,排序变得无关紧要。
  2. 第二行中与上一行相同的客户的下一张最早的发票。再次,如果超过 1 张同龄发票,则以金额最大的发票为准。如果超过 1 张相同年龄和金额的发票,排序变得无关紧要。
  3. 重复 #2 直到该客户不再有发票。
  4. 另一个客户的下一张最早的发票。如果超过 1 张相同的发票,则在这些发票的年龄相同的情况下,金额最大的发票。如果超过 1 张相同年龄和金额的发票,排序变得无关紧要。
  5. 对与 #4 相同的客户重复 #2。
  6. 重复 #5 直到没有该客户的发票。
  7. 重复#4、#5、#6

因此,对于上面的示例数据,期望的结果将是......

customer_name   invoice_id   created_date   amount
--------------------------------------------------
Jeremy          5            2008-01-01     600.00   <-- this is the joint "oldest" invoice with id 1 but has a greater amount.
Jeremy          6            2012-01-01     200.00   <-- this is the next "oldest" invoice for the same customer as the previous row.
Jeremy          7            2013-01-01     1000.00
Gary            1            2008-01-01     500.00   <-- no more invoice for previous customer, so this is the next "oldest" invoice for a new customer
Gary            2            2011-01-01     600.00
Gary            4            2012-01-01     550.00  <-- same age as inv_id 3 but larger amount
Gary            3            2012-01-01     100.00
Marcia          10           2009-01-01     250.00
Marcia          8            2012-01-01     300.00
Marcia          9            2013-01-01     100.00
Danielle        11           2010-01-01     300.00
Danielle        12           2011-01-01     700.00
Danielle        13           2012-01-01     500.00

为了为这个问题提供更广泛的背景,结果将用于追踪发票的付款,最旧和最“昂贵”的优先级最高,然后还可以查看分组在一起的客户的所有发票。

PS 我正在使用 MS SQL Server 2008。

4

2 回答 2

1

希望这有效:)

with ordering as
(
  select
  row_number() over (order by o.created_date asc, o.amount desc) num,
  customer_id,
  customer_name
  from
  (
    select
    min(i.created_date) 
    over (partition by c.customer_id) as min_created_date,
    max(i.amount) 
    over (partition by c.customer_id, i.created_date) max_date_amount,
    c.name as customer_name,
    c.customer_id as customer_id,
    i.invoice_id,
    i.created_date,
    i.amount
    from
    invoice i
    join customer c on i.customer_id = c.customer_id
  )o
  where o.min_created_date = o.created_date
  and o.max_date_amount = o.amount
)
select
ord.customer_name,
i.invoice_id,
i.created_date,
i.amount
from
ordering ord
join invoice i on i.customer_id = ord.customer_id
order by ord.num asc, i.created_date asc, i.amount desc;
于 2013-04-10T09:43:17.123 回答
1

我只是要把它扔在这里作为已经接受的答案的替代品。

SELECT temp.name,
    temp.Invoice_Id,
    temp.created_Date,
    temp.amount
FROM(
  SELECT 
    c.name,
    i.invoice_id,
    i.created_date,
    i.amount,  
    min(i.created_date) over (partition by c.customer_id) as min_created_date,
    max(i.customer_id) over (partition by i.created_Date, i.amount ) as customerId
  FROM
    Customer c
  LEFT JOIN 
    Invoice i
  on 
    c.customer_ID=i.Customer_ID
) temp

ORDER BY  temp.min_created_date, 
    temp.customerId desc, 
    temp.created_Date, 
    temp.amount desc
于 2013-04-10T10:50:57.680 回答