Solved

MS SQL Server 2008, making vertical data horizontal

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Webservices in T-SQL 3 31
SSRS 2013 - Overlapping reports 2 22
SQL - Update field defined as Text 6 17
SQL Server Connection String through a VPN 8 26
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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…

809 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