由于数据设置错误和格式错误而重新发布。我试图从两个具有不明确的列名称的单独表中划分数据。
我比SQL更新,我知道它应该很简单,但是我无法理解它。到目前为止,我已尝试重命名列,别名列,联合表,并选择多个数据集。
我一直在遇到障碍。
我试图衡量一周或一周的增长或下降。理想情况下,我想采用平板的总销售额,并采用以下等式:(75 / 100-1),与上周相比下降了-25%。
最好的方法是什么?
下面的两个示例表
LastWeekData
Product Day Month TotalSales
Plates 7 3 $100
Spoons 7 3 $150
Forks 7 3 $120
CurrentData
Product Day Month TotalSales
Plates 14 3 $75
Spoons 14 3 $100
Forks 14 3 $115
答案 0 :(得分:0)
您可以使用表别名来区分要显示的表列。请参阅此处的演示:http://sqlfiddle.com/#!9/0b0d81/29
select cur.Product,
cur.Day,
cur.Month,
cur.TotalSales as currweek_TotalSales,
pre.TotalSales as lastweek_TotalSales,
round((cur.TotalSales/pre.TotalSales-1)*100) as percent_change
from CurrentData as cur
inner join LastWeekData as pre
on pre.product=cur.product
where datediff(str_to_date(concat_ws('-','0001',cur.month,cur.day),'%Y-%m-%d'),
str_to_date(concat_ws('-','0001',pre.month,pre.day),'%Y-%m-%d'))
= 7
Result:
Product Day Month currweek_TotalSales lastweek_TotalSales percent_change
Plates 14 3 75 100 -25
Spoons 14 3 100 150 -33
Forks 14 3 115 120 -4