Solved

MS SQL Server 2008, making vertical data horizontal

Posted on 2014-03-28
3
320 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now