Reputation: 985
I wonder how could I do a query for grouping rows, this is an example to explain it better.
Column1 Column2 Column3
AD 2
AD AD 79
AD ES 2
AD US 2
I would like this result:
Column1 Column2 Column3
AD AD 79
AD NOT EQUAL 6
Thanks in advance...I was trying with case when and having but any success.
SELECT UPPER(Column1), UPPER(Column2), COUNT(*) FROM MyTable
GROUP BY UPPER(Column1), UPPER(Column2)
HAVING UPPER(Column1) = UPPER(Column2)
ORDER BY UPPER(Column1) ASC
QUERY
SELECT UPPER(FirstCode), UPPER(SecondCode), COUNT(*) FROM [EducaWorkSpace].[dbo].[tblIPCountry]
GROUP BY UPPER(FirstCode), UPPER(SecondCode)
ORDER BY UPPER(FirstCode) ASC
SELECT P.FirstCode as PaisCodeC, CASE WHEN P.FirstCode<>ISNULL(P.SecondCode, '') THEN 'NOT EQUAL' ELSE P.SecondCode END AS PaisCodeIPC, SUM(Cantidad) as CantidadC
FROM
(
SELECT UPPER(FirstCode) FirstCode, UPPER(SecondCode) SecondCode, COUNT(*) Cantidad FROM [dbo].[tblCodes]
GROUP BY UPPER(FirstCode), UPPER(SecondCode)
) P
Group BY P.FirstCode, CASE WHEN P.SecondCode<>ISNULL(P.SecondCode, '') THEN 'Not Equal' ELSE P.SecondCode END
Upvotes: 0
Views: 38
Reputation: 35260
Here, try this:
SELECT Column1,
CASE WHEN Column1<>ISNULL(Column2, '')
THEN 'Not Equal'
ELSE Column2 END AS Column2,
SUM(Column3)
FROM MyTable
GROUP BY Column1,
CASE WHEN Column1<>ISNULL(Column2, '')
THEN 'Not Equal'
ELSE Column2 END
Upvotes: 3