?
Solved

use columns to create types

Posted on 2014-03-24
12
Medium Priority
?
96 Views
Last Modified: 2014-03-28
I would like to have a column named: TypeGroup
that will contains acronyms like: FGC, CatCode, FTC, SGC, PGC

those acronyms (type groups) will be from columns:

ItemCategoryCode ItemFamilyGroupCode  ItemFamilyTypeCode   ItemSweetenerGroupCode ItemProductGroupCode
---------------- -------------------- -------------------- ---------------------- --------------------
BULK             WHITE                STANDARD             SUGAR                                      
DROP             PEANUT               COLOR OR FLAVOR      SUGAR                  4M                  
DROP             PEANUT               STANDARD             SUGAR                  1M                  
DROP             PEANUT               STANDARD             SUGAR                  4M    


I know I have to use a CASE statement, but I am not sure how to accomplish what I need to that I end up with the TypeGroup column first.

Any help would be great.

Thank you for your help.
0
Comment
Question by:metropia
[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
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 39951923
How do you come up with those acronyms. Explain in plain English.
0
 

Author Comment

by:metropia
ID: 39951947
I just did this:

FGC = ItemFamilyGroupCode  
CatCode = ItemCategoryCode
FTC = ItemFamilyTypeCode  
SGC = ItemSweetenerGroupCode
PGC = ItemProductGroupCode


Is that good example?
0
 
LVL 3

Expert Comment

by:englanddg
ID: 39951969
I'm a bit confused as to what you are trying to do...could you elaborate a bit more?
0
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

 
LVL 25

Expert Comment

by:chaau
ID: 39951978
I think what you want is column alias. If for example your column is called ItemFamilyGroupCode  but you want in the select statement to show FGC you need to use an alias, like this:
SELECT ItemFamilyGroupCode  AS FGC, 
ItemCategoryCode AS CatCode,
ItemFamilyTypeCode  AS FTC,
ItemSweetenerGroupCode AS SGC,
ItemProductGroupCode AS PGC 
FROM Table1

Open in new window

0
 

Author Comment

by:metropia
ID: 39951997
Yes.

There are 5 columns returned:

ItemCategoryCode
ItemFamilyGroupCode  
ItemFamilyTypeCode  
ItemSweetenerGroupCode
ItemProductGroupCode

I just want to create a column of GroupType based off on those columns (I eventually want to pivot my results, not there yet) - I came up with "acronyms" for the columns it just follows the first initial of the column name.

In the end I need tscreen shot mockuphe TypeGroup column to look like this:
0
 

Author Comment

by:metropia
ID: 39951999
perhaps a union all? i don't know :/
0
 
LVL 3

Expert Comment

by:englanddg
ID: 39952029
I think @chaau has a good solution for you, if you are dumping it into Excel for manipulation.  The column header would be the TypeGroup you are looking for.

Then you could manipulate it with VBA pretty easily to get you the format you are looking for.

However, if you want it to come straight out of SQL like that?  Hrm...

Is that excel screenshot above the end result you are looking for?  If so, I think it's doable...but I don't have it offhand.  At least, not without adding columns to your table that autopopulate the "groupcode" or dumping it into a temp table or something...

Hrm...interesting question.  Hopefully someone has the answer offhand...  <grin>
0
 

Author Comment

by:metropia
ID: 39952073
I am using t-sql stored procedure. These results will go into a variable table.
0
 

Author Comment

by:metropia
ID: 39952074
any suggestions on how to do this using just t-sql would be awesome. thank you so much.
0
 
LVL 25

Accepted Solution

by:
chaau earned 1400 total points
ID: 39952113
Use the following syntax:
SELECT 'FGC' AS ItemType, ItemFamilyGroupCode  AS Value FROM Table1
UNION ALL
SELECT 'CatCode' AS ItemType, ItemCategoryCode AS Value  FROM Table1
UNION ALL
SELECT 'FTC' AS ItemType, ItemFamilyTypeCode   AS Value  FROM Table1
UNION ALL
SELECT 'SGC' AS ItemType, ItemSweetenerGroupCode   AS Value FROM Table1
UNION ALL
SELECT 'PGC' AS ItemType, ItemProductGroupCode  AS Value FROM Table1

Open in new window

0
 
LVL 3

Expert Comment

by:englanddg
ID: 39952115
Yep, I think @chaanu has your solution!
0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 600 total points
ID: 39954328
You can also try UNPIVOT.
 select distinct 
        case TypeGroup 
			when 'ItemCategoryCode' then 'CatCode' 
            when 'ItemFamilyGroupCode' then 'FGC' 
			when 'ItemFamilyTypeCode' then 'FTC' 
			when 'ItemSweetenerGroupCode' then 'SGC' 
			when 'ItemProductGroupCode' then 'PGC' 
         end TypeGroup,
        Value
   from test	   
unpivot (Value for TypeGroup in (ItemCategoryCode,
                                 ItemFamilyGroupCode,
								 ItemFamilyTypeCode,
								 ItemSweetenerGroupCode,
								 ItemProductGroupCode)) p
 order by TypeGroup

Open in new window

http://sqlfiddle.com/#!3/de0c0/5
0

Featured Post

How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

770 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