3
I work as a technical support in an automation system, a customer asked if there was a report in the system that showed the total sales of 2018 divided by month and showing the total of each month and the overall total of the year, turns out that this report does not have in the system so I recommended it to export the reports of total sales month by month and so 12 reports.
has a function in the system for creating reports that requires an sql statement to get the results and show them in the report
the table that stores sales is as follows:
CREATE TABLE [dbo].[venda_geral](
[venda_id] [uniqueidentifier] NOT NULL,
[dt_contabil] [date] NOT NULL,
[modo_venda_id] [smallint] NOT NULL,
[maquina_id] [smallint] NOT NULL,
[codigo_situacao] [varchar](1) NULL,
[perfil_id] [int] NULL,
[cliente_id] [uniqueidentifier] NULL,
[vl_limite] [numeric](15, 2) NULL,
[vl_subtotal_itens] [dbo].[moeda] NULL,
[vl_subtotal_para_desconto] [dbo].[moeda] NULL,
[vl_subtotal_para_servico] [dbo].[moeda] NULL,
[qtd_fechamentos] [int] NULL,
[numero_chamada] [int] NULL,
[operacao_id] [uniqueidentifier] NOT NULL,
[operacao_origem_id] [uniqueidentifier] NULL,
[dt_hr_ultimo_consumo] [datetime] NULL,
[dt_alt] [datetime] NULL,
[obs] [varchar](200) NULL,
[func_atendeu_id] [int] NULL,
[codigo_ticket] [varchar](10) NULL,
[dt_hr_abertura] [datetime] NOT NULL,
[numero_venda] [int] NULL,
[situacao] AS (case [codigo_situacao] when 'N' then 'normal' when 'E' then 'estornada' when 'C' then 'cancelada' when 'T' then 'transferida' else 'desconhecida' end),
[cancelado] AS (CONVERT([bit],case [codigo_situacao] when 'N' then (0) else (1) end,(0))),
[transferido] AS (CONVERT([bit],case [codigo_situacao] when 'T' then (1) else (0) end,(0))),
[ticket] AS (case [modo_venda_id] when (1) then 'balcão ' when (2) then 'entrega ' when (3) then 'mesa ' when (4) then 'ficha ' else 'ticket ' end+[codigo_ticket]),
[vl_consumacao] [money] NOT NULL,
[vl_entrada] [money] NOT NULL,
CONSTRAINT [pk_venda_geral] PRIMARY KEY NONCLUSTERED
(
[venda_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
for being a dynamic query I think that a stored Procedure would be more appropriate to allow the declaration of variables, I spent a few hours trying to create one in which the user set the initial date ex: 01/01/2018 and final date 31/12/2018, and the report present the following result.
Periodo de 01/01/2018 até 31/12/2018
Mes total mensal
janeiro 10000
fevereiro 11000
março 10500
... e assim por diante e no final mostre um total anual
Total anual: 1.500.000,00
PS.: the closest I saw of the result was the following query
SELECT
'Ano' = YEAR(dt_contabil),
'Mes' = MONTH(dt_contabil),
'Total' = SUM(vl_subtotal_itens)
FROM venda_geral
GROUP BY
YEAR(dt_contabil), MONTH(dt_contabil)
Your query already gives you the results per month. What else do you need? Of the overall total?
– João Martins
I need something that allows me to set an initial date and a final date without having to change the query (dynamically like storing each date in a variable type "@data_initial and @data_final", and also the total of each month and at the end an annual total, so I would like a stored.
– icaro
TRY SELECT YEAR(dt_accounting) YEAR, MONTH(dt_accounting) MES, SUM(vl_subtotal_items) TOTAL FROM venda_general WHERE YEAR(dt_accounting) = '2018' GROUP BY CUBE (YEAR(dt_accounting), MONTH(dt_accounting))
– Motta
@Expensive Any Answers Solved Your Problem? If yes, mark it as an answer to the good health of ptSO data
– Tiedt Tech