sql 服务器结果 table 到图片

sql server result table to picture

Select 
  QID,
  (point), 
  count (0) as t
from WebSurveyResponse
group by QID,point
order by qid ,point

我想点赞

您可以使用 PIVOT 获取结果结构

Select QID, [1] as [point 1], [2] as [point 2], [3] as [point 3], [4] as [point 4] from 
(
Select 
  QID,
  (point), 
  count (0) as t
from WebSurveyResponse
group by QID,point
) src
PIVOT
(
Max(t) for point in ([1],[2],[3],[4])
)p

另外,按照评论中的建议,您可以像这样在 PIVOT 中使用计数作为聚合

Select QID, [1] as [point 1], [2] as [point 2], [3] as [point 3], [4] as [point 4] from 
(
Select 
  QID,
  point
from WebSurveyResponse
) src
PIVOT
(
Count(point) for point in ([1],[2],[3],[4])
)p

您可以使用 PIVOT 或者您可以使用 case 语句并为 point1、point2、point3.. 创建列,然后按 QID 对它们进行分组并取 MAX 值每列。

SELECT QID,
MAX(Point1) AS Point1,
MAX(Point2) AS Point2,
MAX(Point3) AS Point3,
MAX(Point4) AS Point4
FROM(
        SELECT QID,
               CASE WHEN Point=1 THEN t ELSE 0 END as Point1,
               CASE WHEN Point=2 THEN t ELSE 0 END as Point2,
               CASE WHEN Point=3 THEN t ELSE 0 END as Point3,
               CASE WHEN Point=4 THEN t ELSE 0 END as Point4
        FROM WebSurveyResponse
                                )
GROUP BY QID