SQLTeam.com | Weblogs | Forums

Help with sql pivot vertical to horizontal


#1

Hi

I have SQL database where I have Boxes and 6 or 4 bottle serials inside boxes. I to have these like
BoxID1, ItemCode, Batch, Netweight, Grossweight,Bottle1,Bottle2,Bottle3
BoxID2, ItemCode, Batch, Netweight, Grossweight,Bottle1,Bottle2,Bottle3
BoxID3, ItemCode, Batch, Netweight, Grossweight,Bottle1,Bottle2,Bottle3..... to get it exported to text file (one per boxId)

so basicly I need to have sql pivot vertical to horizontal or something like it but cannot :slight_smile:

Can anyone advice? Thanks!

br Jake


#2

First, read the following article to help us help you in the future. Providing readily consumable test data will give your question a much better chance of being answered and a whole lot more quickly.
http://www.sqlservercentral.com/articles/Best+Practices/61537/

You're graphic is also pretty small and a bit difficult to read even after expanding it by clicking on it.

Getting to the problem as the problem is written the advice would be to either use the UNPIVOT operator or use CROSS APPLY with either a column based VALUES operator or a series of embedded SELECT/UNION ALLs to unpivot your data as desired.