Solved

Change Columns into rows

Posted on 2013-02-03
6
230 Views
Last Modified: 2013-02-03
I have a table consisting of 3 columns ITEMNMBR, MAX QTY, MIN QTY I want the the Max and Min to display as rows instead of columns

Current results
ITEMNMBR                             MAX QTY                            MIN QTY
------------------------------ -------------------------------------------------- ----------------------------------
ATI47BB                                    50                                      5
ATIAOPS                                    50                                      5
ATIARWRENCH                          50                                     5


Desired Results
 ITEMNMBR                          MAX_MIN  QTY                          
------------------------------ --------------------------------------------------
ATI47BB                                   50          
ATI47BB                                  5
ATIAOPS                                   50    
ATIAOPS                                       5
ATIARWRENCH                            50  
ATIARWRENCH                          5
0
Comment
Question by:skull52
6 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 38849332
SELECT ITEMNMBR, MAX_QTY AS MAX_MIN_QTY
FROM SomeTable
UNION ALL
SELECT ITEMNMBR, MIN_QTY AS MAX_MIN_QTY
FROM SomeTable
ORDER BY 1 ASC, 2 DESC
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 38849334
Or to show on each row whether it's min or max...

SELECT ITEMNMBR, 'Max' AS MIN_MAX, MAX_QTY AS MAX_MIN_QTY
FROM SomeTable
UNION ALL
SELECT ITEMNMBR, 'Min' AS MIN_MAX, MIN_QTY AS MAX_MIN_QTY
FROM SomeTable
ORDER BY 1 ASC, 2 DESC
0
 
LVL 9

Expert Comment

by:Aeriden
ID: 38849351
Here is one potential approach that may work for you if have more complex requirements than unions (the other options are better, but thought to share this approach, just in case a more "manual" approach was warranted)...

/* Change the @ variables size/type to match itemnmbrs size/type */
declare @itemnmbr char(11)
declare @maxqty int
declare @minqty int

/* replace table1 name with the actual old table name */
select @itemnmbr = min(itemnmbr) from table1

while @itemnmbr is not null
begin
  /* use the actual variable names for maxqty, minqty from table1 */
  select @maxqty = maxqty, @minqty = minqty from table1 where itemnmbr = @itemnmbr

  /* replace table2 anme with the actual new table name, min_max_qty in the new table */
  insert into table2 (itemnmbr, min_max_qty) values (@itemnmbr, @maxqty)
  insert into table2 (itemnmbr, min_max_qty) values (@itemnmbr, @minqty)

  select @itmnmbr = min(itemnmbr) from table1 where itemnmbr > @itemnmbr
end
0
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 
LVL 74

Expert Comment

by:sdstuber
ID: 38849380
if you don't want to query your table multiple times...


select itemnbr, case when n = 1 then max_qty else min_qty end as max_min
from yourtable, (select 1 n union all select 2 n) as x


or, to include the max/min flag as shown above....

select itemnbr, case when n = 1 then max_qty else min_qty end as max_min,
case when n= 1 then 'Max' else 'Min' end as max_min_type
from yourtable, (select 1 n union all select 2 n) as x
0
 

Author Comment

by:skull52
ID: 38849753
Thanks Matthew that worked perfectly, and thanks to all those that responded .
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 38849780
You can use a Common Table Expression  (CTE) to help reduce the query overhead.
Using "union all" rather than "union" ensures no rows get suppressed (I assume that's what you want).
You might also want an indicator of 'min' or 'max' (or something similar) to help sort the output
with src_data as (
                   /* place query for your source data here
                      e.g.
                          select * from my_table 
                          where something...
                   */
                 )

    select itemnmbr, max_qty as max_min_qty, 'max' as min_or_max
    from src_data
union all
    select itemnmbr, min_qty as max_min_qty, 'min' as min_or_max
    from src_data
order by itemnmbr, min_or_max

Open in new window

 ITEMNMBR     MAX_MIN_QTY     MIN_OR_MAX    
 -----------  --------------  ------------- 
 ATI47BB      50              max           
 ATI47BB      5               min           
 ATIAOPS      50              max           
 ATIAOPS      5               min           
 ATIARWRENVH  50              max           
 ATIARWRENVH  5               min           

Open in new window

0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Trouble with <> 2 26
Syntax error creating JSON recordset 4 26
Display field if column exists 7 31
Sql case statement to calculate totals 5 32
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

713 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