2017-11-02 12 views
1

トランザクションデータを含む次のデータセットがあるとします。説明のために以下のサブセット:単一行のグループトランザクション

BillName Item Number 
Customer A 477565 
Customer A 8400212 
Customer A 8400213 
Customer A 461230 
Customer A 461240 
Customer A 477545 
Customer A 8657915 
Customer B 672050 
Customer B 892223 
Customer C 640741 
Customer C 640772 
Customer C 640660 

ExcelまたはSQL Serverを使用して次のように変換するにはどうすればよいですか。

Customer A 477565 8400212 8400213 461230 461240 477545 8657915 
Customer B 672050 892223     
Customer C 640741 640772 640660 
+0

?あなたはVBAでこのような何かに常に、ブルートフォースソリューションのための可能性が探しているなら、言っ

MS SQL Server? – Hambone

答えて

2

Excelで、エントリの順序を示すために別の行を追加して、ピボットテーブルを作成する:

BillNameグループ商品番号優先

BillName group Item Number precedence 
Customer A 477565 1 
Customer A 8400212 2 
Customer A 8400213 3 
Customer A 461230 4 
Customer A 461240 5 
Customer A 477545 6 
Customer A 8657915 7 
Customer B 672050 1 
Customer B 892223 2 
Customer C 640741 1 
Customer C 640772 2 
Customer C 640660 3 

このようになり、ピボットテーブルを作成します:

enter image description here

またはtabluarフォームrepeaでティンすべてのラベル enter image description here

EDIT:SQLの場合、この種のものを行うにはピボット関係演算子に見えます。

+0

これは良い考えですが、データセットが大きすぎます。私はそれをMS SQL Server – user1765523

+0

@ user1765523に入れておきます。あなたはその質問にそれを述べませんでした。それが非選択的だったのなら、なぜあなたはそれをExcelで尋ねましたか?私はあなたの質問に答えました:「Excelを使って次のように変換するにはどうすればいいですか?」 – ivan7707

+0

データセットはExcelにあり、正常に動作します。私は最大限の組み合わせが何であるか分からず、ピボットテーブルアプローチを使用して列の限界に達するという考えを知っていました。 – user1765523

1

@ ivan7707による解決策があると思います。それはExcelの力を活用し、MS Query経由でデータセットを取り込むことさえできます。

Dim Conn As New ADODB.Connection 
Dim cmd As ADODB.Command 
Dim rs As ADODB.Recordset 
Dim bill, item, priorBill As String 
Dim row, col As Integer 
Dim ws As Worksheet 

Set ws = ActiveWorkbook.ActiveSheet 

Conn.Open "Your Connection String" 
Set rs = Conn.Execute("select bill_name, item_number from bills order by 1") 

row = 1 
col = 2 

Do While Not rs.EOF 
    bill = rs.Fields(0).Value 
    item = rs.Fields(1).Value 

    If bill <> priorBill Then 
    row = row + 1 
    col = 2 

    ws.Cells(row, 1).Value = bill 
    priorBill = bill 
    End If 

    ws.Cells(row, col).Value = item 
    col = col + 1 

    rs.MoveNext 
Loop 

Conn.Close 

結果::どのようなDBMS

enter image description here

関連する問題