JOIN和CASE重复查询结果

时间:2017-04-18 18:54:34

标签: sql select join case teradata

我正在使用Teradata中的两个表格,我正在尝试按商店编号,销售总天数和每个商店编号内的总天数查询每个活动项目。我当前的查询设置是由SKU提供两行数据,而不是提供显示相应信息的一行。

表1:显示班级编号,班级名称,状态和商店数据。

Store   Item    Class Number    Class Name         Status
100     Apple      10            Red Fruit         Active
200     Apple      10            Red Fruit         Active
100     Banana     12            Yellow Fruit      Active
200     Banana     12            Yellow Fruit      Active
100     Pear       14            Green Fruit       Active
200     Pear       14            Green Fruit       Active
100     Beans      20            Green Vegetable   Discontinued
200     Beans      20            Green Vegetable   Active

表2:显示按商店和商品销售的总天数

Store   Item    Total Days to sell
100     Apple         4
200     Apple         1
100     Banana        2
200     Banana        4
100     Pear          3
200     Pear          6
100     Beans        NULL
200     Beans         4

表3:当前查询结果

Item  Class Number   Class Name  Total Days to sell  Store 100   Store 200
Apple     10         Red Fruit           5               4         NULL
Apple     10         Red Fruit           5              NULL        1
Banana    12         Yellow Fruit        6               2         NULL
Banana    12         Yellow Fruit        6              NULL        4
Pear      14         Green Fruit         9               3         NULL
Pear      14         Green Fruit         9              NULL        6
Beans     20         Green Vegetable     4              NULL        4

以下是我如何寻找要组织的数据:

Item    Class Number    Class Name  Total Days to sell  Store 100   Store 200
Apple     10             Red Fruit          5              4           1
Banana    12             Yellow Fruit       6              2           4
Pear      14             Green Fruit        9              3           6
Beans     20             Green Vegetable    4             NULL         4

当前查询:

SELECT DISTINCT     
    A.ITEM, 
    A.CLASS_NUMBER, 
    A.CLASS_NAME,   
    SUM(B.TOTAL_DAYS_TO_SELL),  
    CASE    
        WHEN B.STORE=100 THEN B.TOTAL_DAYS_TO_SELL
        ELSE NULL
        END AS STORE 100,
    CASE    
        WHEN B.STORE=200 THEN B.TOTAL_DAYS_TO_SELL
        ELSE NULL
        END AS STORE 200
FROM TABLE 1 A      
RIGHT JOIN TABLE 2 B        
ON B.ITEM=A.ITEM        
WHERE A.STATUS='ACTIVE'     
GROUP BY        
    A.ITEM, 
    A.CLASS_NUMBER, 
    A.CLASS_NAME,   
    STORE 100,  
    STORE 200   
ORDER BY        
    A.CLASS_NUMBER ASC; 

如果您对信息有任何疑问,请与我们联系

谢谢!

2 个答案:

答案 0 :(得分:2)

首先,不需要DISTINCT,因为您使用的是GROUP BY

对于您想要的结果,您需要对正在使用的CASE表达式使用聚合函数。根据您展示的数据,您可以使用MINMAXSUM(甚至AVG):

SELECT DISTINCT     
    A.ITEM, 
    A.CLASS_NUMBER, 
    A.CLASS_NAME,   
    SUM(B.TOTAL_DAYS_TO_SELL),  
    MIN(CASE    
            WHEN B.STORE=100 THEN B.TOTAL_DAYS_TO_SELL
            ELSE NULL
         END) AS STORE 100,
    MIN(CASE    
            WHEN B.STORE=200 THEN B.TOTAL_DAYS_TO_SELL
            ELSE NULL
        END) AS STORE 200
FROM TABLE 1 A      
RIGHT JOIN TABLE 2 B        
ON B.ITEM=A.ITEM        
WHERE A.STATUS='ACTIVE'     
GROUP BY        
    A.ITEM, 
    A.CLASS_NUMBER, 
    A.CLASS_NAME
ORDER BY        
    A.CLASS_NUMBER ASC; 

答案 1 :(得分:1)

我会把它写成:

SELECT A.ITEM, A.CLASS_NUMBER, A.CLASS_NAME, SUM(B.TOTAL_DAYS_TO_SELL),  
       SUM(CASE WHEN B.STORE = 100 THEN B.TOTAL_DAYS_TO_SELL END) as STORE_100,
       SUM(CASE WHEN B.STORE = 200 THEN B.TOTAL_DAYS_TO_SELL END) as STORE_200
FROM TABLE1 A JOIN     
     TABLE2 B        
     ON B.ITEM = A.ITEM        
WHERE A.STATUS = 'ACTIVE'     
GROUP BY A.ITEM, A.CLASS_NUMBER, A.CLASS_NAME
ORDER BY A.CLASS_NUMBER ASC;

这基本上是Lamak的答案,但有更多的社论:

  • 除非你真的知道自己在做什么,否则不要使用SELECT DISTINCT。它几乎不适合GROUP BY
  • 您有三列未汇总;那些应该是GROUP BY
  • 中的那些
  • 您的RIGHT OUTER JOININNER JOIN条款转换为WHERE。只需使用INNER JOIN即可。此外,您按A中的列进行汇总。您真的想要NULL的汇总列吗?
  • ELSE上不需要CASE子句,因为默认值为NULL
  • 此版本为聚合选择SUM()。使用一个数值,SUM()MIN()MAX()都会返回相同的内容。但是,我认为如果你想删除一些聚合键,SUM()会更安全。