Solved

SQL question

Posted on 2011-09-15
6
240 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

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