2016-02-28 56 views
0

我处理作业:内加入在ON语句

比较日均收入最高msa_income商店和实体店的(正如我在Teradata的第5周 锻炼指南定义它) 与最低的中位数msa_income(根据msa_income字段)。 这两家商店在哪个城市和州,哪家商店的平均每日收入增加了 ?

...并且答案关键字在ON声明中有一个内部联接,这使得我很困惑。我只学过FROM加入。所以我在网上搜索了关于ON声明中的一个内部联接,关于它的内容并不多。

我是一个新的学习者,所以这个问题可能是非常基础的。预先感谢您的耐心!

,我有一个问题,该生产线是我:ON m.store=t.store JOIN strinfo s

SELECT SUM(store_rev. tot_sales) 
    SUM(store_rev.numdays) AS daily_average, 
    store_rev.msa_income as med_income,  
    store_rev.city, store_rev.state 
    FROM (SELECT COUNT (DISTINCT t.saledate) as numdays, 
    EXTRACT(YEAR from t.saledate) as s_year, 
    EXTRACT(MONTH from t.saledate) as s_month, t.store, 
    sum(t.amt) as tot_sales, 
    CASE 
     when extract(year from t.saledate) = 2005 AND extract(month from t.saledate) = 8 then 'exclude' 
END as exclude_flag, m.msa_income, s.city, s.state 
FROM trnsact t JOIN store_msa m 
ON m.store=t.store JOIN strinfo s 
ON t.store=s.store 
WHERE t.stype = 'P' AND exclude_flag IS NULL 
GROUP BY s_year, s_month, t.store, m.msa_income, s.city, s.state 
HAVING numdays >= 20) as store_rev 
WHERE store_rev.msa_income IN ((SELECT MAX(msa_income) 
FROM store_msa),(SELECT MIN(msa_income) FROM store_msa)) 
GROUP BY med_income, store_rev.city, store_rev.state; 
+0

感谢您的版本!现在阅读确实容易得多。 :) –

回答

0

也许这将是更容易执行,如果这样写的:

FROM trnsact t JOIN 
    store_msa m 
    ON m.store = t.store JOIN 
    strinfo s 
    ON t.store = s.store 

JOIN不是的ON声明。子句由多个连接组成,并链接在一起。

您可以将JOIN关键字视为结束ON子句并开始新的连接条件。

+0

谢谢你的回答。因此,FROM子句中的第一个SELECT从表中连接的结果中选择:trnsact,store_msa和strinfo .RIght?它与以下内容相同:FROM trnsact t,store_msa m,strinfo s ON m.store = t.store AND t.store = s.store。 –

+0

@JuneChoo。 。 。简单的规则:*从不*在'FROM'子句中使用逗号。始终使用明确的'JOIN'语法。 –