¿Cómo hacer la Agrupación?

Tengo una mesa como debajo

enter image description here

I want the output to be as

enter image description here

I fire the below query

;WITH CTE AS
(
    Select script_Type = 'SP',detail_warnings ='Consider using EXISTS predicate instead of IN predicate' UNION ALL
    Select script_Type = 'SP',detail_warnings ='ExcludeItem does not exist in database SQLEye or is invalid for this operation' UNION ALL
    Select script_Type='SP',detail_warnings ='Values hardcoded in where-clause condition' UNION ALL
    Select script_Type='Table',detail_warnings ='Table name is not singular  Remarks :1:- Missing create index statement.' UNION ALL
    Select script_Type='Table',detail_warnings ='Check for existence object then Drop statement before create statement' UNION ALL
    Select script_Type='View',detail_warnings ='Invalid name'
)

SELECT script_Type,detail_warnings,COUNT(script_Type)
FROM  CTE c WITH(NOLOCK)
GROUP BY ROLLUP(script_Type,detail_warnings)

But the output is as under

enter image description here

What changes I need to do to get the desired result?

preguntado el 17 de mayo de 13 a las 11:05

1 Respuestas

You've done all the hard work here, really, you just need to deal with the various ROLLUP filas en el SELECT.

This looks pretty good to me:

WITH CTE AS
(
    Select script_Type = 'SP',detail_warnings ='Consider using EXISTS predicate instead of IN predicate' UNION ALL
    Select script_Type = 'SP',detail_warnings ='ExcludeItem does not exist in database SQLEye or is invalid for this operation' UNION ALL
    Select script_Type='SP',detail_warnings ='Values hardcoded in where-clause condition' UNION ALL
    Select script_Type='Table',detail_warnings ='Table name is not singular  Remarks :1:- Missing create index statement.' UNION ALL
    Select script_Type='Table',detail_warnings ='Check for existence object then Drop statement before create statement' UNION ALL
    Select script_Type='View',detail_warnings ='Invalid name'
)

SELECT script_Type = case
    when script_Type is null and detail_warnings is null then 'Total'
    when detail_warnings is null then script_Type + ' Count'
    else script_Type end
  ,detail_warnings = isnull(detail_warnings, '')
  ,COUNT(script_Type)
FROM  CTE c WITH(NOLOCK)
GROUP BY ROLLUP(script_Type,detail_warnings)

Violín SQL con demostración.

contestado el 17 de mayo de 13 a las 11:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.