1
I need to insert a calculation column in this view below, but the message appears
Msg 205, Level 16, State 1, Procedure Consolidadoestudobasesifix, Line 4 [Batch Start Line 9] All queries Combined using a UNION, INTERSECT or EXCEPT Operator must have an Equal number of Expressions in their target lists.
I know the mistake is in UNION ALL
, but in the case did not want to insert more columns in the "percentage."
How can I resolve this issue?
Follows code:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER VIEW [dbo].[ConsolidadoBaseSifis] AS
with contabilidade as (
SELECT iUnidade,
COUNT(iUnidade) AS iQuantidade,
COUNT(vSimNao) AS iSimNaoRespondido,
COUNT(vResp) AS iJustificRespondidas
FROM [dbo].[BaseSifis]
GROUP BY iUnidade
),
percentual as(
SELECT
(((iSimNaoRespondido+iJustificRespondidas)/iUnidade)*100) as iPorc
from contabilidade
)
select * FROM contabilidade
Union ALL
select * FROM percentual
In short, she’s like this:
And I’d like it to stay that way:
The original script is this:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[ConsolidadoBaseSifis] AS
with contabilidade as (
SELECT iUnidade,
COUNT(iUnidade) AS iQuantidade,
COUNT(vSimNao) AS iSimNaoRespondido,
COUNT(vResp) AS iJustificRespondidas
FROM [dbo].[EstudoBaseSifis]
GROUP BY iUnidade
)
select * FROM contabilidade
GO
In the
UNION
there must be a match between the columns of each table. What do you want to join in the two tables? The columniPorc
corresponds to which table columncontabilidade
?– João Martins
In this case, I want to create an iPorc column that is = (((iSimNaoResponsed+iJustificResponsed)/iUnidade)*100) ... I tried to create this iPorc within accounting, but it references columns that are being created by it...
– Daniel Gomes
Edit your question and put the code there, otherwise it’s hard to understand.
– João Martins
Okay, I’ve inserted two images to make it easier to view
– Daniel Gomes
And put the original script
– Daniel Gomes