How to get Feature Wise Count Per User? Table_1
Name   |   Feature
A      |    F1
A      |    F1
A      |    F1
A      |    F2
A      |    F3
B      |    F1
B      |    F1
C      |    F3


Desired Result
Name |   F1  |  F2  |   F3
A    |   3   |  1   |    1
B    |   2   |  0   |    0
B    |   0   |  0   |    1


I tried below query

SELECT Name,Feature,COUNT(Feature) FeatureCount FROM Table_1 GROUP BY Feature,Name


But it is not giving as Desired
Instead it is giving

Name | Feature | FeatureCount
A    |   F1    |     3
A    |   F2    |     1
A    |   F3    |     1
B    |   F1    |     2
C    |   F3    |     1


Confuse on How to get Feature Wise Count Per User
Here is a solution using dynamic SQL

--Demo setup
DROP TABLE IF EXISTS Table1;

CREATE TABLE Table1
    ([Name] varchar(1), [Feature] varchar(2))
;

INSERT INTO Table1
    ([Name], [Feature])
VALUES
    ('A', 'F1'),
    ('A', 'F1'),
    ('A', 'F1'),
    ('A', 'F2'),
    ('A', 'F3'),
    ('B', 'F1'),
    ('B', 'F1'),
    ('C', 'F3')
;

--Solution using dynamic SQL
Declare @sql nvarchar(4000)
Declare @col varchar(max)
Declare @colCoalesceNull varchar(max)
SELECT @col = 
        Coalesce(@col + ', ', '') + QUOTENAME([Feature])
    ,@colCoalesceNull = 
        Coalesce(@colCoalesceNull + ', ', '') + 'coalesce(' + QUOTENAME([Feature]) + ',0) as ' + QUOTENAME([Feature])
FROM (
    SELECT DISTINCT 
        [Feature] AS [Feature]
    FROM Table1
    ) AS Table1;

PRINT @col
PRINT @colcoalescenull

SET @sql = N'
with _cte as
(
SELECT Name,Feature,COUNT(Feature) FeatureCount FROM Table1 GROUP BY Feature,Name
)
SELECT Name, ' + @colCoalesceNull + ' FROM _cte
    PIVOT(SUM(FeatureCount)
    FOR Feature IN (' + @col + ')) AS PivotData';

print @sql
EXEC sp_executesql @sql;




| Name | F1 | F2 | F3 |
|------|----|----|----|
| A    | 3  | 1  | 1  |
| B    | 2  | 0  | 0  |
| C    | 0  | 0  | 1  |