個々のメンバーのトランザクションデータを表示し、同じタイプのトランザクションを行った頻度を示すレポートを作成するように求められました。例えばトランザクション数に基づくMySqlピボットテーブル
:
Xはその同じ組織 から10 別の機会/トランザクションおよびボブに一度に5本のバナナを購入した組織からボブは20の異なる 機会/取引に一度3本のバナナを購入しました。
データベースは、この形式のトランザクションデータ
|TransactionID | Organization | MemberNumber | ItemID| QuantityPurchased
が含まれている私は現在、次の形式で一時テーブルを作成するクエリを持っています。
|Organization | MemberNumber | ItemID | QuantityItemsPurchased | QuantityOfTransactions
----------------------------------------------------------------------------------------
|Company X | 2044 | B2 | 5 | 10
|Company X | 2044 | B2 | 3 | 20
|Company Y | 2035 | A3 | 5 | 5
各番号列が販売された商品の数量との値との取引であり、私は、(レポートの形式は、最大私のための議論にはありません)次の形式でレポートを作成するように依頼されました各行は、その数量のアイテムを販売したトランザクションの数です。
|Organization | MemberNumber | ItemID |1 |2 |3 |4 |5 |6+ |
-----------------------------------------------------------------
|Company X | 2044 | B2 |0 |0 |20 |0 |10 |0 |
|Company Y | 2035 | A3 |0 |0 |0 |0 |5 |0 |
私は、個々の列にquantityOfItemsPurchasedをソートしQuantityOfTransactionsに値を表示することができ、クエリを作成する方法を確認していません。
私は以下に類似したクエリを書いてみましたが、それはだけではなく、実際のQuantityOfTransactions値の全ての数の列の下に1または0の値を与えるとして、それは私のために働いていません。
Select Organization, MemberNumber, ItemId,
count(Case when tempTable.quantityOfItemsPurchased = 1 then tempTable.QuantityOfTransactions end) as '1',
count(Case when tempTable.quantityOfItemsPurchased = 2 then tempTable.QuantityOfTransactions end) as '2',
count(Case when tempTable.quantityOfItemsPurchased = 3 then tempTable.QuantityOfTransactions end) as '3',
count(Case when tempTable.quantityOfItemsPurchased = 4 then tempTable.QuantityOfTransactions end) as '4',
count(Case when tempTable.quantityOfItemsPurchased = 5 then tempTable.QuantityOfTransactions end) as '5',
count(Case when tempTable.quantityOfItemsPurchased >= 6 then tempTable.QuantityOfTransactions end) as '6+'
from TempTable group by Organization, MemberNumber, ItemId
この種のことは、アプリケーションコードで非常に簡単です。 :-( – Strawberry
Strawberry、それは簡単だろうと同意しますが、営業/管理者は常にレポートの変更を求めており、新しいビルドを常に公開する必要はないため、これらの種類のレポートをアプリケーションとは別に保ちます私のDBの知識は限られていますが、それはそれです。 – Jcross