Solved

use columns to create types

Posted on 2014-03-24
12
89 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
 
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

863 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

26 Experts available now in Live!

Get 1:1 Help Now