次のシミュレートされた表にはcust_nbr
、注文番号を表す注文の詳細が含まれています。注文に90000が含まれてitem_nbr
いる場合、90000の価格が他のアイテムと税金の合計よりも高いかどうかを知る必要があります。このテーブルには数十万のレコードがあります。私はTeradataを使用しています。
CREATE TABLE Line_Item_Details_Tbl (
cust_nbr INT,
trn_dt DATE,
str_typ VARCHAR(6),
trn_nbr INT,
item_nbr INT,
price DECIMAL(6,2),
tax DECIMAL(6,2)
);
サンプルデータ:
INSERT INTO Line_Item_Details_Tbl VALUES
(5551, '12/22/2011', 'store', 215, 12345, 10.00, 1.25);
INSERT INTO Line_Item_Details_Tbl VALUES
(5551, '12/22/2011', 'store', 215, 65715, 6.25, 0.75);
INSERT INTO Line_Item_Details_Tbl VALUES
(5551, '12/22/2011', 'store', 215, 90000, 40.00, 0);
INSERT INTO Line_Item_Details_Tbl VALUES
(6875, '12/10/2011', 'online', 856, 72345, 8.50, 1.00);
INSERT INTO Line_Item_Details_Tbl VALUES
(6875, '12/10/2011', 'online', 856, 65715, 6.25, 0.75);
INSERT INTO Line_Item_Details_Tbl VALUES
(3500, '12/12/2011', 'store', 402, 54123, 45.00, 4.00);
INSERT INTO Line_Item_Details_Tbl VALUES
(3500, '12/12/2011', 'store', 402, 90000, 20.00, 0);
INSERT INTO Line_Item_Details_Tbl VALUES
クエリは次のことを行う必要があります。
Select cust_nbr, trn_dt, trn_nbr, sum(price + tax) as purchase
For a cust_nbr with str_typ = 'store' AND contains an item_nbr = 90000,
aggregate price + tax for all items related to cust_nbr except item_nbr 90000
したがって、予備的な結果は次のようになります。
cust_nbr : trn_dt : trn_nbr : purchase
5551 12/22/2011 215 $18.25
3500 12/12/2011 402 $49.00
次に、予備結果の各レコードについて、から90000の価格を減算し、購入が90000の価格よりも低い場合にのみ結果を返す必要がitem_nbr
ありpurchase
ますitem_nbr
。net_cb
したがって、私の最終結果は次のようになります。
cust_nbr trn_dt trn_nbr net_cb
5551 12/22/2011 215 ($21.75)