Solved

Help with an SQL select query

Posted on 2009-04-08
8
242 Views
Last Modified: 2012-05-06
Hi all, I have a table called products (a dummy data version is in the code box below) and I want to change the output data (not the data stored just what comes out from the query) in the column "ProductClass" to one of the following below: (dependent on what is already in that field)

Hats
Bags
Other

Example of the Table:

ID          NAME          PRODUCTCLASS
1          foo               123
2          som              456
3          gee               789
4          hhh               xyz
5          sss               asd
6          ppp               abc
7          bbb               bar


The result should then be (in the output from the Select not updating the DB)

ID          NAME          PRODUCTCLASS
1          foo               Hats
2          som              Hats
3          gee               Hats
4          hhh               Bags
5          sss               Bags
6          ppp               Bags
7          bbb               Other

A dummy if statement is in the code field.

Any help would be greatly appreciated.

Xavier.
if(ProductClass = "abc" or ProductClass = "asd" or ProductClass = "xyz") then

{

   ProductClass = "Bags";

}

else

{

   if(ProductClass = "123" or ProductClass = "456" or ProductClass = "789") then ProductClass = "Hats";

}

else

{

ProductClass = "Other";

}

Open in new window

0
Comment
Question by:drxav
8 Comments
 
LVL 39

Accepted Solution

by:
appari earned 400 total points
ID: 24104394
Select
ID          , NAME          ,
PRODUCTCLASS,
case when PRODUCTCLASS in ('abc','asd','xyz') then 'bags'
when PRODUCTCLASS in ('123','456','789') then 'Hats'
else 'Other' end from yourtableName
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24104408
Do you mean this:

SELECT ID, NAME,
CASE WHEN PRODUCTCLASS IN ('123', '456', '789') THEN 'Hats'
         WHEN PRODUCTCLASS IN ('xyz', 'asd','abc') THEN 'Bags'
         WHEN PRODUCTCLASS IN ('bar') THEN 'Other'
END AS PRODUCTCLASS
FROM products
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24104415
you can do something like
SELECT ID,NAME,

ProductClass=CASE WHEN ProductClass='abc' or ProductClass='asd'  or ProductClass='xyz' then 'bags' end

from product

Open in new window

0
 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 100 total points
ID: 24104417
full query

SELECT ID,NAME,
ProductClass=CASE
WHEN ProductClass='abc' or ProductClass='asd'  or ProductClass='xyz' then 'bags'
WHEN ProductClass='123' or ProductClass='456'  or ProductClass='789' then 'hats'
else 'Other'
end
from product

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 31

Expert Comment

by:RiteshShah
ID: 24104420
;) bit late in answering!!!
0
 
LVL 4

Author Comment

by:drxav
ID: 24104434
Is there a way to use * rather than specifying the fields? Nb. the query below uses actual objects and field names:

Select TOP 100 *
CASE
WHEN PRODUCTCLASS in ('HATS','IHAT') THEN 'HATS'
WHEN PRODUCTCLASS in ('JACK','IJAC') THEN 'JACK'
ELSE 'Other Shit' END
from dbo.ArTrnDetail
0
 
LVL 4

Author Comment

by:drxav
ID: 24104440
scratch that missing a comma thanks
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24104445
you can do like this.

SELECT top 100 *,
ProductClass=CASE
WHEN ProductClass='abc' or ProductClass='asd'  or ProductClass='xyz' then 'bags'
WHEN ProductClass='123' or ProductClass='456'  or ProductClass='789' then 'hats'
else 'Other'
end
from product


0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

867 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

18 Experts available now in Live!

Get 1:1 Help Now