How to Normalize or Parse Data into Columns?

Who is online?  0 guests and 0 members
Home  »  Forums   »  microsoft business intelligence   »  sql server   » How to Normalize or Parse Data into Columns?

How to Normalize or Parse Data into Columns?

Topic RSS Feed

Posts under the topic: How to Normalize or Parse Data into Columns?

Posted: 7/19/2011

Padawan 214  points  Padawan
  • Joined on: 4/16/2010
  • Posts: 66

Hi All,

Coming from an MS Access background, I used to parse multiple records per account into columns.  For example, the data would look like:

Account  DX  Seq

12345   333  1

12345  444   2

12345 555    3

67890 111    1

67890 222     2

67890 333     3

 

...and I would transform it into:

Account  DX1  DX2  DX3

12345    333   444  555

67890    111   222   333

 

using VBA attached to a Button Click event inside a Form.  Now that I'm using SQL Server 2005, I have a similar need to parse data into columns per above.  Since the query must be parameterized, I intend to use a stored procedure.  How would I structure the query to parse the diagnosis codes as I've shown above?

 

Thanks,

Sid 


Posted: 7/19/2011

Jedi Knight 2338  points  Jedi Knight
  • Joined on: 2/22/2010
  • Posts: 209

Sid, sounds like what you want to look into is the PIVOT/UNPIVOT function: http://msdn.microsoft.com/en-us/library/ms177410(v=SQL.90).aspx


Posted: 7/19/2011

Padawan 214  points  Padawan
  • Joined on: 4/16/2010
  • Posts: 66

Thanks for this link.  I had reviewed this functionality earlier but in my case I don't need to do any aggregation.  That is, I have a list of diagnoses and procedures that just need to be pivoted, without counting them, adding them etc.  Can PIVOT do this without including aggregation?


Posted: 7/19/2011

Jedi Knight 2338  points  Jedi Knight
  • Joined on: 2/22/2010
  • Posts: 209
Answered  Answered

Try out this solution of using PIVOT without aggregation http://www.calsql.com/2009/10/pivot-with-out-aggregate.html


Posted: 7/21/2011

Jedi Youngling 2  points  Jedi Youngling
  • Joined on: 7/21/2011
  • Posts: 1

For future reference, you can always use MAX() or MIN() as your aggregate function in PIVOT, even with non-numeric data types.


Page 1 of 1 (5 items)