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. 最初の行の最も古い請求書。同じ年齢の請求書が複数ある場合は、同じ年齢の請求書のうち、最も金額の高い請求書。同じ年齢と金額の請求書が複数ある場合、並べ替えは無関係になります。
  2. 2 行目の前の行と同じ顧客の次に古い請求書。繰り返しますが、同じ年齢の請求書が複数ある場合は、最大額の請求書です。同じ年齢と金額の請求書が複数ある場合、並べ替えは無関係になります。
  3. その顧客の請求書がなくなるまで #2 を繰り返します。
  4. 別の顧客の次に古い請求書。同一の請求書が複数ある場合は、同じ年齢の請求書のうち、最も金額の高い請求書。同じ年齢と金額の請求書が複数ある場合、並べ替えは無関係になります。
  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 に答える