Solved

use columns to create types

Posted on 2014-03-24
12
88 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
Comment Utility
How do you come up with those acronyms. Explain in plain English.
0
 

Author Comment

by:metropia
Comment Utility
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
Comment Utility
I'm a bit confused as to what you are trying to do...could you elaborate a bit more?
0
 
LVL 24

Expert Comment

by:chaau
Comment Utility
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
Comment Utility
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
Comment Utility
perhaps a union all? i don't know :/
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 3

Expert Comment

by:englanddg
Comment Utility
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
Comment Utility
I am using t-sql stored procedure. These results will go into a variable table.
0
 

Author Comment

by:metropia
Comment Utility
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
Comment Utility
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
Comment Utility
Yep, I think @chaanu has your solution!
0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 150 total points
Comment Utility
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 run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now