sql >> Base de Datos >  >> RDS >> Sqlserver

¿La consulta SQL obtiene el elemento de conteo para el informe por día del mes?

Puede hacer esto usando un PIVOT en su consulta

SELECT name, 
       [1], 
       [2], 
       [3], 
       [4], 
       [5],
       [6],
       [7],
       [8],
       [9],
       [10],
       [11],
       [12],
       [13],
       [14],
       [15],
       [16],
       [17],
       [18],
       [19], 
       [20], 
       [21], 
       [22], 
       [23], 
       [24], 
       [25], 
       [26], 
       [27], 
       [28], 
       [29], 
       [30], 
       [31],
       ([1] + [2] + [3] + [4] + [5] + [6] + [7] + [8] + [9] + [10] + [11] + [12] + [13] + [14] + [15] + [16] + [17] + [18] + [19] +  [20] + [21] + [22] + [23] + [24] + [25] + [26] + [27] + [28] + [29] + [30] + [31]) as total
FROM   
(
    SELECT Name, 
        id, 
        Datepart(day, [date]) day 
    FROM   item 
    WHERE  MONTH([date]) = 2 AND YEAR([date]) = 2015
) x 
PIVOT 
(
    count(id) 
    FOR day IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19],  [20], [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31]) 
) p