I need to insert a calculation column in this view below, but the message
Msg 205, Level 16, State 1, Procedure ConsolidatedStudyBaseSifix, 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 error is in UNION ALL
, but in case I did not want to insert more columns in the "percentage."
How can I resolve this issue?
Follow the code below:
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, it looks like this:
AndIwouldlikeittolooklikethis:
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