SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Multiple conditions in Case statement


Multiple conditions in Case statement

Author
Message
shakha79
shakha79
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 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


GSquared
GSquared
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23379 Visits: 9730
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
shakha79
shakha79
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 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
GSquared
GSquared
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23379 Visits: 9730
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search