0
Good morning.
I need help solving a query question in the view. I need help creating this view.
Follows the table:
CREATE TABLE [dbo].[Controle](
[ID] [int] IDENTITY(1,1) NOT NULL,
[DataHora] [datetime] NOT NULL,
[Local] [varchar](30) NOT NULL,
[PessoaID] [int] NOT NULL,
[PessoaNome] [varchar](50) NOT NULL,
[PessoaDocNumero] [varchar](15) NOT NULL,
[PessoaGrupo] [varchar](30) NULL,
[Outros1] [varchar](30) NULL,
[Outros2] [varchar](30) NULL,
CONSTRAINT [PK_Controle] PRIMARY KEY CLUSTERED
(
[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
Follow the Insert:
INSERT INTO Controle (DataHora,Local,PessoaID,PessoaNome,PessoaDocNumero,PessoaGrupo,Outros1,Outros2)VALUES(GETDATE(),'TERREO',1,'MARIA','123456789','VISITANTE','XPTO1', 'XPTO2')
INSERT INTO Controle (DataHora,Local,PessoaID,PessoaNome,PessoaDocNumero,PessoaGrupo,Outros1,Outros2)VALUES(GETDATE(),'TERREO',1,'MARIA','123456789','VISITANTE','XPTO1', 'XPTO2')
INSERT INTO Controle (DataHora,Local,PessoaID,PessoaNome,PessoaDocNumero,PessoaGrupo,Outros1,Outros2)VALUES(GETDATE(),'TERREO',1,'MARIA','123456789','VISITANTE','XPTO1', 'XPTO2')
INSERT INTO Controle (DataHora,Local,PessoaID,PessoaNome,PessoaDocNumero,PessoaGrupo,Outros1,Outros2)VALUES(GETDATE(),'TERREO',1,'JOAO','32333232','FUNCIONARIO','XPTO1', 'XPTO2')
INSERT INTO Controle (DataHora,Local,PessoaID,PessoaNome,PessoaDocNumero,PessoaGrupo,Outros1,Outros2)VALUES(GETDATE(),'TERREO',1,'JOAO','32333232','FUNCIONARIO','XPTO1', 'XPTO2')
INSERT INTO Controle (DataHora,Local,PessoaID,PessoaNome,PessoaDocNumero,PessoaGrupo,Outros1,Outros2)VALUES(GETDATE(),'TERREO',1,'JOAO','32333232','FUNCIONARIO','XPTO1', 'XPTO2')
Objective: I need to know the amount of records per Persongroup in the month and year to be defined and in the query below I have the expected result. So far so good.
SELECT PessoaGrupo, 'Dia ' + '' + Convert(VARCHAR,day(DataHora)) as Dia, COUNT(*) as Valor
FROM Controle
WHERE ID > 0
and month(DataHora) = 05
and year(DataHora) = 2021
GROUP BY PessoaGrupo, day(DataHora)
return:
Campo Dia valor
FUNCIONARIO Dia 12 3
VISITANTE Dia 12 3
But how I use VIEW is where I’m in trouble. How do I return only the 3 columns as per the query and be able to filter through any field of the Control table?
I need help creating this view so I can filter it as follows:
SELECT * FROM View_Controle
WHERE PessoaGrupo = 'VISITANTE'
AND month(DataHora) = 05
AND year(DataHora) = 2021
retorno:
Campo Dia valor
FUNCIONARIO Dia 12 3
VISITANTE Dia 12 3
Can someone help me with creating this view?
Grateful
Teles
Good afternoon. Clarck Maciel. Thanks for your help, but I still have problems.... I should be able to filter through any field in the Control table, but the result should only be the 3 columns: Personal Group, Day , Value Ex: How to filter by Personally Name like '%MARIA%' without this column appearing in the results Entering Personal Name in selection I am required to put in group by and then the result changes
– Nádia Teles
"I should be able to filter through any field of the Control table" if you want to have this flexibility you should not use a view, it would be easier to use the table directly
– Ricardo Pontual
@Nádiateles, the
viewused with aCOUNT(*) as Valoris usually used for a specific data clipping as it will usually use theGroup byto group by a field. I had a question in the example oflike '%MARIA%', you want to inform Personname and want the aggregation to continue being by the fieldPessoaGrupo? Have you ever considered using severalviewsas needed? Will all fields really be query targets? If so, I see two possibilities: 1) Multiple views usingunion; 2) A function that returns a table instead of using only oneview.– Clarck Maciel
Thanks for everyone’s feedback. I ended up using a store Process where I have more flexibility and solved the issue.
– Nádia Teles