I have a SQL table that i would like to change to the following format:
I already have that table, but I wasn't able to make it count the users from another table and insert them into the column.
I have a SQL table that i would like to change to the following format:
I already have that table, but I wasn't able to make it count the users from another table and insert them into the column.
This should do it if you are using tSQL, but what about same months in different years?
;WITH CTE AS (
SELECT DATENAME(month,SignUpTime) AS [MonthName],
COUNT(UserID) AS [CountUsers]
FROM Tbl_Users
GROUP BY DATENAME(month,SignUpTime))
UPDATE A
SET A.[CountUsers]=B.[CountUsers]
FROM Tbl_CountUsersPerMonth AS A LEFT OUTER JOIN
CTE AS B ON A.[MonthName]=B.[MonthName]
I suggest you instead of table use view like this:
CREATE VIEW vw_CountUsersPerMonth
AS
SELECT YEAR(SignUpTime) as [Year],
DATENAME(month,SignUpTime) AS [MonthName],
COUNT(UserID) AS [CountUsers]
FROM Tbl_Users
GROUP BY YEAR(SignUpTime), DATENAME(month,SignUpTime))