-2
Good night!
I have an sql query based on a CTE and I want to take the largest value between a subconsulta, but when I run the main query the subconsulta error returns more than one value appears. Could you help me?
Follows code:
WITH CTE AS(
SELECT TOP 1 PERSON.PERSON.FirstName AS NOME,
(SELECT COUNT(*) AS A FROM PERSON.PERSON
WHERE EmailPromotion = 0
UNION ALL
SELECT COUNT(*) FROM PERSON.PERSON
WHERE EmailPromotion = 1) AS EMAIL
FROM PERSON.PERSON)SELECT MAX(EMAIL) FROM CTE
Try to explain which result you want to get because this query is strange. The subselect with UNION ALL will return two lines to the first with the total number of records of the PERSON.PERSON table with Emailpromotion = 0 and the second with the quantity with Emailpromotion = 1.
– anonimo
I want to get the highest value between Emailpromotion = 1 and Emailpromotion = 0. I made a Union all to based on that get the highest value (MAX)
– Almino
I tried to put a TOP 1 but it didn’t solve
– Almino
Evaluate the use of the GREATEST function of these COUNT.
– anonimo
In SQL SERVER there is no such function. Know any alternative?
– Almino
Do you want the highest value Emailpromotion (count) per person (person) ? Give an example of the required data and output.
– Motta