Solved

# How to use CASE

Posted on 2005-04-22
220 Views
Hi I have a query below

SELECT     contract.contractid, SUM(vw_job_newtailcount.r1b + vw_job_newtailcount.r1c) AS Expr1
FROM         job INNER JOIN
contract ON job.contractid = contract.contractid INNER JOIN
vw_job_newtailcount ON job.jobid = vw_job_newtailcount.jobid
WHERE     (job.statusid NOT IN (3, 4, 5, 6, 7)) AND (contract.contractid IN (5, 6, 12, 13))
GROUP BY contract.contractid

I now want to produce a select statement as follows

select SUM(vw_job_newtailcount.r1b + vw_job_newtailcount.r1c) when contract.contractid in (5,12) as AAP

How can I do this

Thanks

Mark
0
Question by:polynominal
1 Comment

LVL 44

Accepted Solution

Hi polynominal,

Use:

SELECT     contract.contractid, SUM(vw_job_newtailcount.r1b + vw_job_newtailcount.r1c) AS Expr1,
Sum(Case When Contract.ContractID IN (5,12) Then vw_job_newtailcount.r1b+vw_job_newtailcount.r1c Else 0 End) As AAP
FROM         job INNER JOIN
contract ON job.contractid = contract.contractid INNER JOIN
vw_job_newtailcount ON job.jobid = vw_job_newtailcount.jobid
WHERE     (job.statusid NOT IN (3, 4, 5, 6, 7)) AND (contract.contractid IN (5, 6, 12, 13))
GROUP BY contract.contractid

Tim Cottee
0

## Featured Post

### Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.