Solved

SQL question

Posted on 2011-09-15
6
194 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

11 Experts available now in Live!

Get 1:1 Help Now