Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多聚合、多筛选器、单表SQL_Sql_Teradata_Aggregates - Fatal编程技术网

多聚合、多筛选器、单表SQL

多聚合、多筛选器、单表SQL,sql,teradata,aggregates,Sql,Teradata,Aggregates,下表包含订单详细信息,cust\u 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 DECIM

下表包含订单详细信息,
cust\u 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)
);
cust_nbr  :   trn_dt   : trn_nbr : purchase
5551        12/22/2011   215       $18.25           
3500        12/12/2011   402       $49.00           
cust_nbr  trn_dt       trn_nbr  net_cb
  5551    12/22/2011   215      ($21.75)            
样本数据:

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 
cust_nbr  :   trn_dt   : trn_nbr : purchase
5551        12/22/2011   215       $18.25           
3500        12/12/2011   402       $49.00           
cust_nbr  trn_dt       trn_nbr  net_cb
  5551    12/22/2011   215      ($21.75)            
查询应执行以下操作:

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           
cust_nbr  trn_dt       trn_nbr  net_cb
  5551    12/22/2011   215      ($21.75)            
因此,初步结果应该是:

cust_nbr  :   trn_dt   : trn_nbr : purchase
5551        12/22/2011   215       $18.25           
3500        12/12/2011   402       $49.00           
cust_nbr  trn_dt       trn_nbr  net_cb
  5551    12/22/2011   215      ($21.75)            
然后,对于初步结果中的每个记录,我需要从
购买
中减去
item_nbr
90000的价格,并且仅当购买少于
项目编号90000的价格为
净cb

cust_nbr  :   trn_dt   : trn_nbr : purchase
5551        12/22/2011   215       $18.25           
3500        12/12/2011   402       $49.00           
cust_nbr  trn_dt       trn_nbr  net_cb
  5551    12/22/2011   215      ($21.75)            
因此,我的最终结果应该是:

cust_nbr  :   trn_dt   : trn_nbr : purchase
5551        12/22/2011   215       $18.25           
3500        12/12/2011   402       $49.00           
cust_nbr  trn_dt       trn_nbr  net_cb
  5551    12/22/2011   215      ($21.75)            

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

cust_nbr  :   trn_dt   : trn_nbr : purchase
5551        12/22/2011   215       $18.25           
3500        12/12/2011   402       $49.00           
cust_nbr  trn_dt       trn_nbr  net_cb
  5551    12/22/2011   215      ($21.75)            
将其视为示例数据(SQL Server 2005中的CTE):

cust_nbr  :   trn_dt   : trn_nbr : purchase
5551        12/22/2011   215       $18.25           
3500        12/12/2011   402       $49.00           
cust_nbr  trn_dt       trn_nbr  net_cb
  5551    12/22/2011   215      ($21.75)            
最后一个查询(我假设您的表名是
ord_det
,如果它不只是使用正确的名称):

cust_nbr  :   trn_dt   : trn_nbr : purchase
5551        12/22/2011   215       $18.25           
3500        12/12/2011   402       $49.00           
cust_nbr  trn_dt       trn_nbr  net_cb
  5551    12/22/2011   215      ($21.75)            

使用子查询来标识您想要的事务,然后使用CASE来确定哪些记录对您的聚合有贡献

cust_nbr  :   trn_dt   : trn_nbr : purchase
5551        12/22/2011   215       $18.25           
3500        12/12/2011   402       $49.00           
cust_nbr  trn_dt       trn_nbr  net_cb
  5551    12/22/2011   215      ($21.75)            
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

cust_nbr  :   trn_dt   : trn_nbr : purchase
5551        12/22/2011   215       $18.25           
3500        12/12/2011   402       $49.00           
cust_nbr  trn_dt       trn_nbr  net_cb
  5551    12/22/2011   215      ($21.75)            
或者简单地使用HAVING子句来确定要包括哪些事务

cust_nbr  :   trn_dt   : trn_nbr : purchase
5551        12/22/2011   215       $18.25           
3500        12/12/2011   402       $49.00           
cust_nbr  trn_dt       trn_nbr  net_cb
  5551    12/22/2011   215      ($21.75)            
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
或者

cust_nbr  :   trn_dt   : trn_nbr : purchase
5551        12/22/2011   215       $18.25           
3500        12/12/2011   402       $49.00           
cust_nbr  trn_dt       trn_nbr  net_cb
  5551    12/22/2011   215      ($21.75)            

@Michal我正在使用Teradata。@Donna:proper(这里是SQL语句)比任何特殊模式和示例数据格式都更有用。请使用
CREATE TABLE
INSERT。。。样本的值
。期望的结果不需要以示例代码的形式呈现,因为结果是代码的输出,而不是代码本身。。。。一般来说,你应该通过更新你的帖子来回应澄清请求,而不是回复评论。首先,一个问题不需要阅读评论就可以理解。另一方面,QA&site也是如此,而不是论坛,评论也不适合讨论。在这种情况下,您可以编辑您的问题并在正文中提及RDBMS,以及添加“teradata”标记。谢谢您的反馈,outis。这是我在这个网站上的第一篇帖子,我尽了最大的努力来设置格式。你应该看到我在编辑之前第一次发布的内容!现在看起来好多了。再次感谢!非常感谢您的回复!!我使用了第一个“HAVING”建议,效果非常好!再次感谢您帮助一位新手。谢谢您的回复-我想这让我更困惑了,尽管我真的很感谢您抽出时间来帮助我!