SELECT ID, Title, Content,
MAX(CASE WHEN Rating = 'Rating 1' THEN totalCount ELSE NULL END) 'Rating 1',
MAX(CASE WHEN Rating = 'Rating 2' THEN totalCount ELSE NULL END) 'Rating 2',
MAX(CASE WHEN Rating = 'Rating 3' THEN totalCount ELSE NULL END) 'Rating 3'
FROM
(
SELECT a.ID, a.Title, a.Content,
b.Rating, COUNT(*) totalCount
FROM Message a
LEFT JOIN Rating b
ON a.ID = b.MessageID
GROUP BY a.ID, a.Title, a.Content, b.Rating
) r
GROUP BY ID, Title, Content
样本结果:
╔════╦═══════════════╦══════════╦══════════╦══════════╦══════════╗
║ ID ║ TITLE ║ CONTENT ║ RATING 1 ║ RATING 2 ║ RATING 3 ║
╠════╬═══════════════╬══════════╬══════════╬══════════╬══════════╣
║ 1 ║ Lost Prophets ║ CONTENT1 ║ 1 ║ 5 ║ 7 ║
║ 2 ║ Cannibal ║ CONTENT2 ║ 3 ║ 4 ║ 6 ║
║ 3 ║ Sigbin ║ CONTENT3 ║ 4 ║ 4 ║ 1 ║
╚════╩═══════════════╩══════════╩══════════╩══════════╩══════════╝