Hello I have a dataset from kaggle that I am using for a beginner level portfolio. I am stuck on a part that does not seem that complicated so I hoping one of you guys could help me out.
So what I just finished doing is making a query to return the monthly totals and profits by month and year with the following code:
Select Year(list.OrderDateFixed) as Year, Month(list.OrderDateFixed) as Month, Sum(od.Amount) as TotalAmount, sum(od.profit) as TotalProfit
from [List of Orders] list
join [Order Details] od
on list.[Order ID] = od.[Order ID]
Group by YEAR(list.OrderDateFixed), MONTH(list.OrderDateFixed)
Order by YEAR(list.OrderDateFixed), MONTH(list.OrderDateFixed)
It returns the following:
Year Month TotalAmount TotalProfit
2018 4 32726.00 -3960.00
2018 5 28545.00 -3584.00
2018 6 23658.00 -4970.00
2018 7 12966.00 -2138.00
2018 8 30899.00 -2180.00
2018 9 26628.00 -4963.00
2018 10 31615.00 3093.00
2018 11 48086.00 11619.00
2018 12 37579.00 5284.00
2019 1 61439.00 9760.00
2019 2 38424.00 5917.00
2019 3 58937.00 10077.00
I am trying to output something like that with the sales target of the month to the column on the right. The sales target table looks like this with all the categories:
MonthOrderDateFixed Category Target
2018-04-01 Furniture 10400.00
2018-05-01 Furniture 10500.00
2018-06-01 Furniture 10600.00
2018-07-01 Furniture 10800.00
2018-08-01 Furniture 10900.00
2018-09-01 Furniture 11000.00
2018-10-01 Furniture 11100.00
2018-11-01 Furniture 11300.00
................................
So I was thinking maybe I could use a cte from the first query and then join the sales target table, but I am not sure how to join it when the dates are divided up into separate columns. Also, I attempted to make a temp table but I think I ran into the same problem with the date formats.
I am hoping to be able to easily compare the targets with the actual amounts so then I could do something like finding the differences. If someone can help guide me in the right direction, I would really appreciate it.
Here is a way to extract the year and month from a date (it's also possible to connect the dates from the other table and you can present which info you want).
select *
from t full join t2 on year(MonthOrderDateFixed) = t.Year
and month(MonthOrderDateFixed) = t.Month
Year | Month | TotalAmount | TotalProfit | MonthOrderDateFixed | Category | Target |
---|---|---|---|---|---|---|
2018 | 4 | 32726 | -3960 | 2018-04-01 00:00:00.000 | Furniture | 10400 |
2018 | 5 | 28545 | -3584 | 2018-05-01 00:00:00.000 | Furniture | 10500 |
2018 | 6 | 23658 | -4970 | 2018-06-01 00:00:00.000 | Furniture | 10600 |
2018 | 7 | 12966 | -2138 | 2018-07-01 00:00:00.000 | Furniture | 10800 |
2018 | 8 | 30899 | -2180 | 2018-08-01 00:00:00.000 | Furniture | 10900 |
2018 | 9 | 26628 | -4963 | 2018-09-01 00:00:00.000 | Furniture | 11000 |
2018 | 10 | 31615 | 3093 | 2018-10-01 00:00:00.000 | Furniture | 11100 |
2018 | 11 | 48086 | 11619 | 2018-11-01 00:00:00.000 | Furniture | 11300 |
2018 | 12 | 37579 | 5284 | NULL | NULL | NULL |
2019 | 1 | 61439 | 9760 | NULL | NULL | NULL |
2019 | 2 | 38424 | 5917 | NULL | NULL | NULL |
2019 | 3 | 58937 | 10077 | NULL | NULL | NULL |
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.