我想知道如何对行进行分组查询,这是一个更好地解释它的例子。
Column1 Column2 Column3
AD 2
AD AD 79
AD ES 2
AD US 2
我想要这个结果:
Column1 Column2 Column3
AD AD 79
AD NOT EQUAL 6
提前致谢...我正在尝试处理案件,但却取得了成功。
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
答案 0 :(得分:3)
在这里,试试这个:
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