慕姐8265434
表设置:CREATE TABLE dbo.tbl (
action VARCHAR(20) NOT NULL,
view_edit VARCHAR(20) NOT NULL);INSERT INTO dbo.tbl (action, view_edit)VALUES ('Action1', 'VIEW'),
('Action1', 'EDIT'),
('Action2', 'VIEW'),
('Action3', 'VIEW'),
('Action3', 'EDIT');您的桌子:SELECT action, view_edit FROM dbo.tbl查询时不使用枢轴: SELECT Action, [View] = (Select view_edit FROM tbl WHERE t.action = action and view_edit = 'VIEW'),[Edit] = (Select view_edit FROM tbl
WHERE t.action = action and view_edit = 'EDIT')FROM tbl tGROUP BY Action使用枢轴查询: SELECT [Action], [View], [Edit] FROM(SELECT [Action], view_edit FROM tbl) AS t1
PIVOT (MAX(view_edit) FOR view_edit IN ([View], [Edit]) ) AS t2这两个查询结果都是: