我有一个大约25列的表格来表示位置,我想知道是否有任何方法可以将此结果转换为更清晰的布局?我在SQL中的内容是:
SELECT sum(isnull(p.[injuryFace],0)) AS [Face]
,sum(isnull(p.[InjuryHead],0)) AS [Head]
,sum(isnull(p.[InjuryEye],0)) AS [Eye]
,sum(isnull(p.[InjuryLeftFinger],0)) AS [Finger - Left]
,....
FROM tbl_OHS_IncidentsPeople P
这给了我一个类似于
的结果数据集Face | Head | Eye | Finger - Left | .... |
---------------------------------------------
0 | 1 | 2 | 0 | ... |
我最终想要的是
Area | Count |
------------------------
Face | 0 |
Head | 2 |
Eye | 3 |
Finger - Left | 0 |
不,我看了Simple way to transpose columns and rows in Sql?似乎有我需要的东西,但我似乎无法理解它在我脑海里,因为我不想转置整个表
任何帮助都会很棒
干杯 斯蒂芬
答案 0 :(得分:2)
一种方法是通过以下工会分割计数:
select 'Face' as area, sum(isnull(p. [ injuryFace ], 0)) as location
from tbl_OHS_IncidentsPeople P
union all
select 'Head' as area, sum(isnull(p. [ InjuryHead ], 0))
from tbl_OHS_IncidentsPeople P
union all
select 'Eye' as area, sum(isnull(p. [ InjuryEye ], 0))
from tbl_OHS_IncidentsPeople P
union all
select 'Finger - Left' as area, sum(isnull(p. [ InjuryLeftFinger ], 0))
from tbl_OHS_IncidentsPeople P