Solved

SQL question

Posted on 2011-09-15
6
213 Views
Last Modified: 2012-05-12
I have two fields: type and proj_num. I would like to add an additional field to my result set called laborType. In field laborType if type = ‘Project’ then laborType would equal  = ‘Direct’ else laborType = type. The exception would be if proj_num = ‘998’ or proj_num = ‘001’ then laborType would be Indirect.
0
Comment
Question by:thenrich
6 Comments
 
LVL 18

Expert Comment

by:lludden
ID: 36544005
SELECT [Type], Proj_Num,
CASE WHEN [Type] = 'Project' AND Proj_Num NOT IN ('998','001') THEN 'Direct'
         ELSE 'Indirect' END AS LaborType
FROM myTable

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36544035
Hi.
It sounds like you need a CASE WHEN statement. The work like this:

CASE x 
   WHEN 'some value' THEN 'do one thing' 
   ELSE 'a default' 
END

Open in new window


or

CASE 
   WHEN x IN (a, b) THEN 'do one thing 
   WHEN y = 'some value' THEN 'do another' 
   ELSE 'a default' 
END

Open in new window


Does that make sense? If you have difficulty applying to your specific case, please advise.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36544051
@lludden: that is close. It should be something more like my second query, but wanted to give thenrich a chance to understand the concept before just posting the answer.
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 2

Expert Comment

by:akku101
ID: 36544102

select
      typee,
      proj_num,
      Case
            when typee = 'project' then 'Direct'
            When Proj_num in (998,001) then 'Indirect'
            Else 'leabourtype'
      end as leabourtype
From
(
      select 'type1' as typee, 998 as proj_num,'LType1' as LeabourType   union all
      select 'project' as typee, 2 as proj_num,'LType1' as LeabourType union all
      select 'type3' as typee, 3 as proj_num,'LType1' as LeabourType union all
      select 'project' as typee, 4 as proj_num,'LType1' as LeabourType union all
      select 'type5' as typee, 5 as proj_num,'LType1' as LeabourType
) x
0
 
LVL 12

Accepted Solution

by:
viralypatel earned 500 total points
ID: 36544154
tested and working:

select *,
case (type)
	when 'Project' then 
		case proj_num
			when 998 then 'Indirect'
			when 001 then 'Indirect'
			else 'Direct'
		end
	else [type]
end as proj_type
from tableName

Open in new window




Output would be as follows:

type       proj_num   proj_type
---------- ---------- ----------
Project    345        Direct
Project    998        Indirect
Project    001        Indirect
Project    123        Direct
Other      345        Other     
Other      998        Other     
Other      001        Other     
Other      123        Other     

(8 row(s) affected)

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36544158
@akku101, that is also not correct. Take a look:
select 
      typee,
      proj_num,
      Case 
            when typee = 'project' then 'Direct'
            When Proj_num in (998,001) then 'Indirect'
            Else 'leabourtype'
      end as leabourtype
From
(
      select 'project' as typee, 998 as proj_num,'LType1' as LeabourType   union all
      select 'project' as typee, 2 as proj_num,'LType1' as LeabourType union all
      select 'type3' as typee, 3 as proj_num,'LType1' as LeabourType union all
      select 'project' as typee, 4 as proj_num,'LType1' as LeabourType union all
      select 'type5' as typee, 5 as proj_num,'LType1' as LeabourType 
) x

Open in new window


As you can see the typee WHEN condition is met first, so it does not consider the exceptional case on the proj_num; therefore, that is why I listed the IN part in my example above. Here to keep this simple, I will post what this should look like:

CASE 
   WHEN [proj_num] IN ('998','001') THEN 'Indirect'
   WHEN [type] = 'Project' THEN 'Direct' 
   ELSE [type]
END

Open in new window


Kevin
0

Featured Post

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.

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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