我有以下表格:從多個表中檢索數據 - SQL
表搜索:
Date Product Search_ID
2017-01-01 Nike 101
2017-01-01 Reebok 292
2017-01-01 Nike 103
2017-01-01 Adidas 385
2017-01-02 Nike 284
表採購
Date Product Total_sale
2017-01-01 Adidas 4
2017-01-01 Nike 1
2017-01-01 Adidas 2
2017-01-02 Nike 3
每件產品可以在同一天之內多行。當天產品的購買總數=總和(total_sale)
我需要找出每件產品每天的購買比率,即購買數量/搜索次數。
爲了參考,對於耐克上2017-01-01
,搜索的總數是702
而購買的總數爲47
,給我嘗試的4 7/702 = 0.0669
購買比率:
select t1.product, sum(t1.Total_sale), count(t2.Search_ID)
from db.purchases t1 join db.searches
on t1.date = t2.date and t1.product = t2.product
where t1.date = '2017-01-01' and t1.product = 'Nike'
group by t1.product, t1.date
;
,這給我一個奇怪的結果:
product | sum | count
----------+-------+-------
Nike | 32994 | 32994
......我在做什麼錯在這裏?