SELECT ID, Name,
SUM(CASE WHEN `Page Visited` = 'Page A' THEN 1 ELSE 0 END) `Page A Visit`,
SUM(CASE WHEN `Page Visited` = 'Page B' THEN 1 ELSE 0 END) `Page B Visit`,
SUM(CASE WHEN `Page Visited` = 'Page C' THEN 1 ELSE 0 END) `Page C Visit`
FROM tableName
GROUP BY ID, Name
om du har okänt antal page
, kan du också PreparedStatement
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'SUM(CASE WHEN `Page Visited` =''',
`Page Visited`,
''' then 1 ELSE 0 end) AS ',
CONCAT('`',`Page Visited`, ' Visits`')
)
) INTO @sql
FROM TableName;
SET @sql = CONCAT('SELECT ID, Name, ', @sql, '
FROM tableName
GROUP BY ID, Name');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;