Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

La query SQL ottiene l'elemento di conteggio per il rapporto per giorno del mese?

Puoi farlo usando un PIVOT nella tua query

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