Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Multiple conditions in Case statement Expand / Collapse
Author
Message
Posted Thursday, December 3, 2009 12:33 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, December 3, 2009 1:12 PM
Points: 2, Visits: 2
Hi All,

Can i give multiple conditons in one statement ? Actually in my SQL i have two columns comes from view F

TS_USER_18 TS_USER_11
P1
P1 NP
P1 P1

I want to count only where Provider is P1 and AltProvider is empty. I don't know why following SQL is not returning correct result. When I give only one condition i get correct value from the table.


SELECT F.Requirements
, Count(F.TS_NAME)AS TotalTestCases
, COUNT(CASE WHEN TS_USER_18 Like '%P1%' And TS_USER_11 <> '' Then TS_USER_18 END) AS P1
, COUNT(CASE WHEN TS_USER_18 Like '%P2%' Then TS_USER_18 END) AS P2
, COUNT(CASE WHEN TS_USER_18 Like '%NP%' Then TS_USER_18 END) AS NP
, COUNT(CASE WHEN TS_USER_11 <> '' Then TS_USER_11 END) AS AltProvider
From (

SELECT Distinct TEST.TS_NAME,
(
SELECT TESTCYCL.TC_STATUS from TESTCYCL where TEST.TS_TEST_ID = TESTCYCL.TC_TEST_ID and TESTCYCL.TC_CYCLE_ID =
(Select max(TESTCYCL.TC_CYCLE_ID) from TESTCYCL WHERE TESTCYCL.TC_TEST_ID = TEST.TS_TEST_ID)) AS STATUS
,TEST.TS_USER_24 AS TC_VALIDATED, Requirements =
Case
WHEN RQ_REQ_PATH Like 'AAAAADAARAADAAC%' Then 'Medi'
WHEN RQ_REQ_PATH Like 'AAAAADAALAAN%' THEN 'MHS'
WHEN RQ_REQ_PATH Like 'AAAAADAALAAPAAB%' THEN 'OB90'
Else
'Unknown'
END, TEST.TS_USER_18, TEST.TS_USER_11, TEST.TS_USER_14, TEST.TS_USER_22
FROM TEST, REQ_COVER, REQ
WHERE TEST.TS_TEST_ID = REQ_COVER.RC_ENTITY_ID AND REQ.RQ_REQ_ID = REQ_COVER.RC_REQ_ID
) F
Group by F.Requirements

Post #828434
Posted Thursday, December 3, 2009 12:40 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 15,444, Visits: 9,596
Your text says you want the second column to be blank, but the actual code tests the exact opposite, so far as I can tell. What am I missing?

And TS_USER_11 <> ''

That part of the code is testing to make sure it is NOT blank.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #828442
Posted Thursday, December 3, 2009 12:45 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, December 3, 2009 1:12 PM
Points: 2, Visits: 2

oK Let me explain. Let say my table is something like this.

TS_USER_11 TS_USER_18
P1
P1 P2
P2
P1
P1 P1
P2
P1
NP P2
P1

I need out put like this . It will show how 9 rows are distributed across different providers (P1, P2, NP and Alternate)

P1 P2 NP Alt(Where TS_USER18 is not equal to null)
4 2 0 3
Post #828450
Posted Thursday, December 3, 2009 12:58 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 15,444, Visits: 9,596
This is how I like to do that kind of thing in SQL 2000. (It's easier in SQL 2005 with the Pivot command.)

if object_id(N'tempdb..#T') is not null
drop table #T;

create table #T (
ID int identity primary key,
TS_USER_11 char(2),
TS_USER_18 char(2));

insert into #T (TS_USER_11, TS_USER_18)
select 'P1','' union all
select 'P1','P2' union all
select 'P2','' union all
select 'P1','' union all
select 'P1','P1' union all
select 'P2','' union all
select 'NP','P2' union all
select 'P1','';

select
(select count(*)
from #T
where TS_USER_11 = 'P1'
and TS_USER_18 = '') as P1,
(select count(*)
from #T
where TS_USER_11 = 'P2'
and TS_USER_18 = '') as P2,
(select count(*)
from #T
where TS_USER_11 = 'NP'
and TS_USER_18 = '') as NP,
(select count(*)
from #T
where TS_USER_18 != '') as Alt;



- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #828457
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse