Solved

use columns to create types

Posted on 2014-03-24
12
94 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift 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 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 41

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

726 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