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
Solved

use columns to create types

Posted on 2014-03-24
12
91 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
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 40

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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 24

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 24

Accepted Solution

by:
chaau earned 350 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 40

Assisted Solution

by:Sharath
Sharath earned 150 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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