I'm trying to build a SQL consult that populates a data table that will give the info that will draw a js graph (on canvasjs library).
The graph will compare the sales table vs. the expenses table, so they don't have a column on which I can make a JOIN, only the dates, that will be shown as yyyy-mm.
In order to group all the sales, I need to Join two tables, the one that stores the main invoices info ([dbo].[Facturas]), and the one that stores the items on each invoice ([dbo].[FacturasItems])
This is the consult that retrieves the info for all the sales, and groups them in months: DECLARE @facTotal numeric(18,2)
SET @facTotal =
(
SELECT
SUM(([dbo].[FacturasItems].[ValorU] * [dbo].[FacturasItems].[Cantidad]) + (([dbo].[FacturasItems].[ValorU] * [dbo].[FacturasItems].[Cantidad]) * [dbo].[Facturas].[ValorIVA]))
FROM [dbo].[FacturasItems]
INNER JOIN
[dbo].[Facturas]
ON [dbo].[Facturas].[ID] = [dbo].[FacturasItems].[IdFactura]
)
SELECT
CAST(YEAR([dbo].[Facturas].[FechaCr]) AS VARCHAR(4)) + '-' + right('00' + CAST(MONTH([dbo].[Facturas].[FechaCr]) AS VARCHAR(2)), 2) AS [Periodo],
CONVERT(numeric(18,0), SUM(([dbo].[FacturasItems].[ValorU] * [dbo].[FacturasItems].[Cantidad]) + (([dbo].[FacturasItems].[ValorU] * [dbo].[FacturasItems].[Cantidad]) * [dbo].[Facturas].[ValorIVA]))) AS [VentasRaw],
'$' + CONVERT(varchar, CONVERT(money, SUM(([dbo].[FacturasItems].[ValorU] * [dbo].[FacturasItems].[Cantidad]) + (([dbo].[FacturasItems].[ValorU] * [dbo].[FacturasItems].[Cantidad]) * [dbo].[Facturas].[ValorIVA]))), 1) AS [VentasForm],
CONVERT(varchar(6), CONVERT(numeric(18,2), SUM(([dbo].[FacturasItems].[ValorU] * [dbo].[FacturasItems].[Cantidad]) + (([dbo].[FacturasItems].[ValorU] * [dbo].[FacturasItems].[Cantidad]) * [dbo].[Facturas].[ValorIVA])) / (@facTotal) * 100)) + '%' AS [Prc100]
FROM [dbo].[Facturas]
INNER JOIN
[dbo].[FacturasItems]
ON [dbo].[FacturasItems].[IdFactura] = [dbo].[Facturas].[ID]
GROUP BY
CAST(YEAR([dbo].[Facturas].[FechaCr]) AS VARCHAR(4)) + '-' + right('00' + CAST(MONTH([dbo].[Facturas].[FechaCr]) AS VARCHAR(2)), 2)
This consult is working just fine, and it returns this table data:
The second consult, expenses, goes like this:
DECLARE @gasTotal numeric(18,2)
SET @gasTotal =
(
SELECT
SUM(([dbo].[Gastos].[Valor]) + (([dbo].[Gastos].[Valor]) * [dbo].[Gastos].[IVAVal]))
FROM [dbo].[Gastos]
)
SELECT
CAST(YEAR([dbo].[Gastos].[Fecha]) AS varchar(4)) + '-' + RIGHT('00' + CAST(MONTH([dbo].[Gastos].[Fecha]) AS varchar(2)), 2) AS [Periodo],
CONVERT(numeric(18,0), SUM([dbo].[Gastos].[Valor] + ([dbo].[Gastos].[Valor] * [dbo].[Gastos].[IVAVal]))) AS [ValorGraph],
'$' + CONVERT(varchar, CONVERT(money, SUM([dbo].[Gastos].[Valor] + ([dbo].[Gastos].[Valor] * [dbo].[Gastos].[IVAVal]))), 1) AS [ValorForm],
CONVERT(varchar, CONVERT(money, SUM(([dbo].[Gastos].[Valor] + ([dbo].[Gastos].[Valor] * [dbo].[Gastos].[IVAVal])) / @gasTotal) * 100), 1) + '%' AS [Prc100]
FROM [dbo].[Gastos]
GROUP BY
CAST(YEAR([dbo].[Gastos].[Fecha]) AS varchar(4)) + '-' + RIGHT('00' + CAST(MONTH([dbo].[Gastos].[Fecha]) AS varchar(2)), 2)
This will create this table:
So, as you can see, there where no sales on 2017-10, zero invoices, so the table 1 won't start on 2017-10 but on 2017-11, while the expenses table starts on 2017-10.
I need that the table 1 (invoices and sales), starts in 2017-10 with it's values in 0, so that the graph will show correctly.
In other words, I need this kind of tables:
I tried an inner join, a left join and an outer join, but the will add all the records, add the sales + expenses.
Thanks!
2条答案
按热度按时间8zzbczxx1#
Just add SELECT 2017-10 as Periodo,0 as VentasRow, 0 as VentasForm, 0 as Prc100 UNION Your first query here....
Hope this helps.
8mmmxcuj2#
You could certainly use a full outer join if it's possible to have sales but no expenses.