多聚合,多重过滤,单表SQL

时间:2011-12-31 06:24:32

标签: sql teradata aggregates

以下模拟表包含订单详细信息,其中cust_nbr表示订单号。我试图找到订单包含item_nbr 90000的位置,我需要知道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           

然后,对于初步结果中的每条记录,我需要从item_nbr中减去purchase 90000的价格,并且只有在购买量小于item_nbr时才返回结果 net_cb 90000的价格为cust_nbr trn_dt trn_nbr net_cb 5551 12/22/2011 215 ($21.75)

所以,我的结局应该是:

{{1}}

2 个答案:

答案 0 :(得分:1)

我已经在SQL Server 2005上进行了测试,所以如果它根本不起作用请不要downvote,请告诉我,我将删除我的答案:-)。我只是想帮忙。

将其视为您的示例数据(SQL Server 2005中的CTE):

;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

答案 1 :(得分:1)

使用子查询来识别您想要的交易,然后使用CASE来确定哪些记录对您的聚合有贡献。

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
         )
相关问题