Solved

SQL question

Posted on 2011-09-15
6
238 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
[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
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 60

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 60

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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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 60

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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