0

私はデータを含むテーブルを持っています、

Item    sales_description   date        amount  sequence
----    -----------------   ----        ------  --------
 xyz    base cost           2013-03-31  2.50    1
 xyz    packing charges     2013-03-31  5.50    2
 xyz    miscellaneous       2013-03-31  1.50    3

 xyz    base cost           2013-06-30  3.50    1
 xyz    packing charges     2013-06-30  6.50    2
 xyz    miscellaneous       2013-06-30  1.00    3
 xyz    delivery charges    2013-06-30  5.00    4

 xyz    base cost           2013-09-30  5.00    1
 xyz    packing charges     2013-09-30  6.50    2
 xyz    labor charges       2013-09-30  3.50    3
 xyz    miscellaneous       2013-09-30  1.00    4
 xyz    delivery charges    2013-09-30  5.00    5

私が探している出力は

Item    sales_description   date1       amount1     date2       amount2     date3       amount3
----    -----------------   -----       -------     -----       -------     -----       -------
 xyz    base cost           2013-03-31  2.50        2013-06-30  3.50        2013-09-30  5.00
 xyz    packing charges     2013-03-31  5.50        2013-06-30  6.50        2013-09-30  6.50
 xyz    labor charges       NULL        NULL        NULL        NULL        2013-09-30  3.50
 xyz    miscellaneous       2013-03-31  1.50        2013-06-30  1.00        2013-09-30  1.00
 xyz    delivery charges    NULL        NULL        2013-06-30  5.00        2013-09-30  5.00

ストアド プロシージャを作成し、上記の出力形式で値を一時テーブルに格納して、一時テーブルからクエリを実行しようとしましたが、達成できませんでした。

結果を得る他の方法はありますか?どんな提案も役に立ちます。

ありがとう

4

1 に答える 1

0

大まかな答えは次のとおりです。

編集:この回答では、シーケンス番号に関する部分を見逃しました。修正させてください。

SELECT t.Item, t.sales_descriptions, 
 ( SELECT sub1.date, sub1.amount FROM table AS sub1 ORDER BY sub1.date asc limit 1) AS date1,
 ( SELECT sub2.date, sub2.amount FROM table AS sub2 ORDER BY sub2.date asc limit 1,1) AS date2,
 ( SELECT sub2.date, sub2.amount FROM table AS sub2 ORDER BY sub2.date asc limit 2,1) AS date3,
  ...
 FROM table as t
 GROUP BY t.Item, t.sales_description;
于 2013-07-24T17:01:30.097 に答える