select
t.[Month]
,sum(t.Costtotal)
,sum(t.Repairtotal)
,sum(t.Servicetotal)
from
(
select
Datepart(MM, PurchaseDate) as [Month],
Costtotal,
0 as Repairtotal,
0 as Servicetotal
from tablea
union
select
Datepart(MM, RepairDate) as [Month],
0 as Costtotal,
Repairtotal,
0 as Servicetotal
from tableb
union
select
Datepart(MM, ServiceDate) as [Month],
0 as Costtotal,
0 as Repairtotal,
Servicetotal
from tableb
) as t
group by t.[Month]
order by t.[Month]
Group by month with mutliple tables and date fields
Pregunta
Good Day
I need to write a query which involves 3 tables. Each table has a date field. I want to run a query to group data by month eg
Month Costtotal Repairtotal Servicetotal
April 1243 2344 123123
May 3123 213123 21312
Each column comes from a different table that has its own date. Costtotal has purchasedate, repairtotal has repairdate and servicetotal has servicedate
Thanks in advance
Solución
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow