-1

I have a SQL table that i would like to change to the following format:

enter image description here

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.

davidmdem
  • 3,763
  • 2
  • 30
  • 33
maorw
  • 33
  • 6

2 Answers2

0

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]
Fuzzy
  • 3,810
  • 2
  • 15
  • 33
0

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))
gofr1
  • 15,741
  • 11
  • 42
  • 52