Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS SQL Server 2008, making vertical data horizontal

Posted on 2014-03-28
3
Medium Priority
?
328 Views
Last Modified: 2014-04-03
I need to create a view based on this query:
select                  s.ord_No [shop_ord],
                            h.ord_no [oe_ord]
from                    shopfile s
left outer join      cusordfile h
        on                ltrim(s.ord_no) = rtrim(h.oe_po_no)
where                   h.ord_no is not null
 
which returns these results:
shop_ord      oe_ord
    3328        89907
    3328        90437
      32         131123

Note that the relationship of shop_ord to oe_ord is one to many and that "many" is undefined (currently believed to be a max of 4)

We would like to see the results like this:
shop_ord     oe_ord     oe_ord     oe_ord     oe_ord
    3328        89907      90437
      32          131123

etc.

Can that be done?
0
Comment
Question by:g_johnson
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1500 total points
ID: 39962893
there will be several ways to do this, check out for "crosstab" and "pivot" data.
0
 
LVL 12

Expert Comment

by:Tony303
ID: 39963398
Hi

Trying to go the PIVOT way to achieve this...

Close in this example but not perfect...Can we get input Guy?


DROP TABLE #shopfile
DROP TABLE #cusordfile
--first temp table
CREATE TABLE #shopfile (ord_No int)
INSERT INTO #shopfile (ord_No)
VALUES (3328),(32)
--second temp table
CREATE TABLE #cusordfile (oe_po_no int,ord_no int)
INSERT INTO #cusordfile (oe_po_no, ord_no)
VALUES (3328 ,89907),(3328, 90437),(32,131123)

--put the data into another temp table
select   s.ord_No [shop_ord], 
         h.ord_no [oe_ord] 
INTO #PivotTable
from				#shopfile s 
left outer join     #cusordfile h 
on       ltrim(s.ord_no) = rtrim(h.oe_po_no) 
where    h.ord_no is not null 

--PIVOT that bad boy up
SELECT *
FROM #PivotTable


SELECT shop_ord, [89907] AS OE1 ,[90437] AS OE2, [131123] AS OE3
FROM #PivotTable as p

 PIVOT (
 MAX(oe_ord) FOR [oe_ord] 
  IN ([89907],[90437],[131123] )
  ) AS pvt 

Open in new window

0
 
LVL 4

Author Comment

by:g_johnson
ID: 39975855
Ultimtely, this is what we did:

CReate view vw_OrderString
as

with cte as
(select distinct S.Ord_No FROM ShopFile S)       
select cte.ord_no as SF_Ord_No,  
      Quote_List= stuff((select ',  ' + Rtrim(Ltrim(ord_no)) FROM cusordfile
      where Rtrim(Ltrim(oe_po_no))= Rtrim(Ltrim(Cte.Ord_No))      
      order by ord_no asc
for XML path(''),TYPE).value('.','varchar(max)'),1,1,'') from CTE
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question