-1

I'm trying to follow this code

SQL Server: Examples of PIVOTing String data

I've imported the table into sql server, and see that

"Select view_edit FROM tbl WHERE t.action = action" does

give me the view_edit that is in "select * from tbl" but I am clueless why the author used this line of code.

Community
  • 1
  • 1
Derick
  • 135
  • 1
  • 3
  • 12
  • 2
    Please paste all the **relevant** code here rather than linking to it, so that the question makes sense even if the link goes dead. – shree.pat18 Jan 16 '15 at 02:00
  • 1
    There is a whole bunch of codes in that link. Please paste relevant codes... – StoneBird Jan 16 '15 at 02:06
  • just do a find and you'll find the code. Code is by mxasim – Derick Jan 16 '15 at 02:18
  • Why would you assume people will spend their time finding the code for your problem when you don't even bother spending your own time pasting it? – StoneBird Jan 16 '15 at 21:32

1 Answers1

0

This is too big to put in a comment, so adding it in answer section

i think you are referring to this SQL code.

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 t
GROUP BY Action

Here correlated subquery is used instead of MAX aggregation or pivot keyword for each action in the table , the corresponding view_edit value is fetched in the correlated sub query , if corresponding action is not found then it will have NULL

To understand more remove group by and run the query and you will understand the correlated subquery and the need for group by

radar
  • 13,270
  • 2
  • 25
  • 33