我正在尝试构建一个填充数据表的SQL参考,该数据表将提供将绘制js图形的信息(在canvasjs库上)。
该图表将比较销售表和费用表,因此他们没有列我可以加入的列,只有日期,这将显示为yyyy-mm。
为了对所有销售进行分组,我需要加入两个表,一个存储主发票信息([dbo]。[Facturas]),以及在每张发票上存储项目的表([dbo] [FacturasItems])
这是检索所有销售信息的咨询,并在几个月内对它们进行分组: DECLARE @facTotal数字(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)
这个咨询工作正常,它返回此表数据:
第二次咨询费用是这样的:
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)
这将创建此表:
所以,正如你所看到的那样,2017-10没有销售,零发票,因此表1不会在2017-10开始,但在2017-11,而费用表在2017年开始 - 10。
我需要表1(发票和销售),从2017年10月开始,其值为0,以便图表显示正确。
换句话说,我需要这种表:
我尝试了内连接,左连接和外连接,但是会添加所有记录,添加销售+费用。
谢谢!
答案 0 :(得分:1)
添加 选择2017-10作为Periodo,0作为VentasRow,0作为VentasForm,0作为Prc100 联盟 你在这里的第一个询问....
希望这会有所帮助。
答案 1 :(得分:1)
WITH Ventas as (
SELECT
CAST(YEAR(f.FechaCr) AS VARCHAR(4)) + '-'
+ right('00' + CAST(MONTH(f.FechaCr) AS VARCHAR(2)), 2) AS Periodo,
SUM(fi.ValorU * fi.Cantidad * (1 + f.ValorIVA)) AS VentasRaw,
SUM(fi.ValorU * fi.Cantidad * (1 + f.ValorIVA)) OVER () AS VentasTot
FROM dbo.Facturas as f INNER JOIN dbo.FacturasItems as fi
ON fi.IdFactura = f.ID
GROUP BY datepart(year, f.FechaCr), datepart(month, f.FechaCr)
),
Gastos as (
SELECT
CAST(YEAR(g.Fecha) AS varchar(4)) + '-'
+ RIGHT('00' + CAST(MONTH(g.Fecha) AS varchar(2)), 2) AS Periodo,
SUM(g.Valor * (1 * g.IVAVal) AS ValorGraph,
SUM(g.Valor * (1 * g.IVAVal) OVER () AS GastosTot
FROM dbo.Gastos as g
GROUP BY datepart(year, g.Fecha), datepart(month, g.Fecha)
)
SELECT g.Periodo,
COALESCE(v.VentasRaw, 0), COALESCE(v.VentasTot, 0),
g.ValorGraph, g.GastosTot
FROM Ventas as v RIGHT OUTER JOIN Gastos g ON g.Periodo = v.Periodo;
如果可以有销售但没有费用,你当然可以使用全外连接。