2011-12-31 88 views
1

以下仿真表包含訂單明細,其中cust_nbr代表訂單號。我試圖找到如果訂單中包含90000,我需要知道90000的價格是否大於其他項目加稅的總和。我在這張表中有數十萬條記錄。我正在使用Teradata。多聚合多過濾器單表SQL

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   

然後,對於每個記錄在初步結果,我需要減去的item_nbr 90000從purchase,價格也只有當購買小於 的item_nbr 90000價格爲net_cb

所以返回結果,我的結局結果應該是:

cust_nbr trn_dt  trn_nbr net_cb 
    5551 12/22/2011 215  ($21.75)    
+0

什麼是你的RDBMS? – 2011-12-31 06:41:09

+0

@Michal我正在使用Teradata。 – 2011-12-31 06:44:58

+0

@Donna:正確的[示例代碼](http://sscce.org/)(這裏是SQL語句)比任何ad hoc模式和示例數據格式更有用。請使用'CREATE TABLE'和'INSERT ... VALUES'作爲樣本。所需的結果不需要作爲示例代碼呈現,因爲結果是代碼的輸出,而不是代碼本身。 – outis 2011-12-31 07:48:01

回答

1

使用一個子查詢來確定要交易,然後使用CAS E來確定哪些記錄有助於您彙總或不彙總。

SELECT 
    transactions.cust_nbr, 
    transactions.trn_dt, 
    transactions.trn_nbr, 
    sum(price + tax)           AS total, 
    sum(CASE WHEN item_nbr = 9000 THEN 0 ELSE price + tax END) AS total_less_9000 
FROM 
(
    SELECT 
    cust_nbr, trn_dt, trn_nbr 
    FROM 
    yourTable 
    WHERE 
    str_typ = 'store' 
    AND item_nbr = 90000 
    GROUP BY 
    cust_nbr, trn_dt, trn_nbr 
) 
    AS transactions 
INNER JOIN 
    yourTable 
    ON transactions.cust_nbr = yourTable.cust_nbr 
    AND transactions.trn_dt = yourTable.trn_dt 
    AND transactions.trn_nbr = yourTable.trn_nbr 
GROUP BY 
    transactions.cust_nbr, transactions.trn_dt, transactions.trn_nbr 


或者乾脆用HAVING子句來確定包括交易。

SELECT 
    cust_nbr, 
    trn_dt, 
    trn_nbr, 
    sum(price + tax)           AS total, 
    sum(CASE WHEN item_nbr = 9000 THEN 0 ELSE price + tax END) AS total_less_9000 
FROM 
    yourTable 
GROUP BY 
    cust_nbr, 
    trn_dt, 
    trn_nbr 
HAVING 
    MAX(CASE WHEN item_nbr = 9000 THEN 1 ELSE 0 END) = 1 

或者......

HAVING 
    EXISTS (SELECT * FROM yourTable AS lookup 
      WHERE cust_nbr = yourTable.cust_nbr 
      AND trn_dt = yourTable.trn_dt 
      AND trn_nbr = yourTable.trn_nbr 
      AND item_nbr = 9000 
     ) 
+0

非常感謝您的回覆!我使用了第一個「HAVING」建議,它完美的工作!再次感謝您幫助新手。 – 2011-12-31 23:10:26

1

我已經在SQL Server 2005上進行了測試,所以請不要低估它是否無效,只是讓我知道,我會刪除我的答案:-)。我只是想幫忙。

把這個作爲你的樣本數據(CTE在SQL Server 2005):

;with ord_det (cust_nbr, trn_dt, str_typ, trn_nbr, item_nbr, price, tax) as (
    select 5551, convert(datetime, '12/22/2011', 101), 'store', 215, 12345, 10.00, 1.25 union all 
    select 5551, convert(datetime, '12/22/2011', 101), 'store', 215, 65715, 6.25, 0.75 union all 
    select 5551, convert(datetime, '12/22/2011', 101), 'store', 215, 90000, 40.00, null union all 
    select 6875, convert(datetime, '12/10/2011', 101), 'online', 856, 72345, 8.50, 1.00 union all 
    select 6875, convert(datetime, '12/10/2011', 101), 'online', 856, 65715, 6.25, 0.75 union all 
    select 3500, convert(datetime, '12/12/2011', 101), 'store', 402, 54123, 45.00, 4.00 union all 
    select 3500, convert(datetime, '12/12/2011', 101), 'store', 402, 90000, 20.00, null 
) 

最終查詢(我假設你的表名是ord_det,如果它不只是用正確的名稱):

select t.cust_nbr, t.trn_dt, t.trn_nbr, price - purchase as net_cb from (
    select cust_nbr, trn_dt, trn_nbr, sum(price + coalesce(tax, 0)) as purchase 
    from ord_det o 
    where item_nbr <> 90000 and str_typ = 'store' 
    group by cust_nbr, trn_dt, trn_nbr 
) t 
inner join (
    select cust_nbr, trn_dt, trn_nbr, price + coalesce(tax, 0) as price 
    from ord_det o 
    where item_nbr = 90000 and str_typ = 'store' 
) t1 on t.cust_nbr = t1.cust_nbr 
where purchase < price 

結果:

cust_nbr trn_dt     trn_nbr  net_cb 
5551  2011-12-22 00:00:00.000 215   21.75 
+0

謝謝你的回覆 - 我覺得這讓我困惑不已,儘管我真的很感謝你花時間幫助我! – 2011-12-31 23:11:26