Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

SQL-Abfrage Zählelement für Bericht pro Tag des Monats abrufen?

Sie können dies mit einem PIVOT in Ihrer Abfrage tun

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