Solved

MS SQL Server 2008, making vertical data horizontal

Posted on 2014-03-28
3
323 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
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…

738 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