July 1, 2016 at 11:56 am
Hi,
I'm trying to add a condition in sql where I have used ®iondivision and &divisionid as variables. I want if the case 1st condition is satisfied then the column segmentname should appear else column regionname should appear. Can anybody please confirm the code below is correct or not?
Select REGIONNAME, divisionname, SEGMANTNAME, DIVISIONID, SEGMENTID,
CASE WHEN ®IONDIVISION <> 'null' AND (&DIVISIONID <> '_FOC_NULL' OR 'Technical' OR 'Non-Technical') THEN SEGMENTNAME
ELSE REGIONNAME END,
sUM(spreadamt) AS SPREAD, FROM TABLEA GROUP BY REGIONNAME, DIVISIONNAME, SEGMENTNAME
July 1, 2016 at 12:10 pm
MMITTAL 758 (7/1/2016)
Hi,I'm trying to add a condition in sql where I have used ®iondivision and &divisionid as variables. I want if the case 1st condition is satisfied then the column segmentname should appear else column regionname should appear. Can anybody please confirm the code below is correct or not?
Select REGIONNAME, divisionname, SEGMANTNAME, DIVISIONID, SEGMENTID,
CASE WHEN ®IONDIVISION <> 'null' AND (&DIVISIONID <> '_FOC_NULL' OR 'Technical' OR 'Non-Technical') THEN SEGMENTNAME
ELSE REGIONNAME END,
sUM(spreadamt) AS SPREAD, FROM TABLEA GROUP BY REGIONNAME, DIVISIONNAME, SEGMENTNAME
NULL doesn't compare to anything, so I don't know if your 'null' should really be the word null or NULL the missing value indicator. If the later, you will need WHEN @regiondivision IS NOT NULL.
Also, you can't string multiple value comparisons with OR like you did.
@divisionid NOT IN ('a', 'b', 'c') would be what you are looking for here I think.
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
July 1, 2016 at 12:21 pm
Thank you that worked. But now I want to have the distinct values for the column which is satisfied in the case statement and also want to get the condition satisfied for the variable &divisionbox but oracle does not accept Boolean. So how can I put the Boolean condition?
Select CASE WHEN '®IONDIVISION' is not null AND ('&DIVISIONID' NOT IN ( 'Non-Technical', 'Technical')) AND '&DIVISIONBOX' = FALSE THEN SEGMENTNAME
ELSE REGIONNAME END as Segment,
sUM(spreadamt) AS SPREAD, FROM table a
GROUP BY CASE WHEN '®IONDIVISION' is not null AND('&DIVISIONID' NOT IN ( 'Non-Technical', 'Technical')) THEN SEGMENTNAME
ELSE REGIONNAME END
July 1, 2016 at 2:06 pm
MMITTAL 758 (7/1/2016)
Hi,I'm trying to add a condition in sql where I have used ®iondivision and &divisionid as variables. I want if the case 1st condition is satisfied then the column segmentname should appear else column regionname should appear. Can anybody please confirm the code below is correct or not?
Select REGIONNAME, divisionname, SEGMANTNAME, DIVISIONID, SEGMENTID,
CASE WHEN ®IONDIVISION <> 'null' AND (&DIVISIONID <> '_FOC_NULL' OR 'Technical' OR 'Non-Technical') THEN SEGMENTNAME
ELSE REGIONNAME END,
sUM(spreadamt) AS SPREAD, FROM TABLEA GROUP BY REGIONNAME, DIVISIONNAME, SEGMENTNAME
Are you executing this statement against Oracle or MS SQL Server?
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
July 1, 2016 at 3:38 pm
I'm executing it against Oracle.
July 2, 2016 at 10:38 am
Please don't double-post.
Answer here: http://www.sqlservercentral.com/Forums/Topic1799126-392-1.aspx
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply