Solved

SQL question

Posted on 2011-09-15
6
226 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SHOWPLAN permission denied in database 'AdventureWorks'. 13 111
ms sql + top 1 for each customer 3 57
SQL query with cast 38 53
SQL Error - Query 6 41
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

861 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