Case-when-then logic in join condition
Is it possible to implement case when then logic in join condition?
Instead of:
INGRP1.COL1 = INGRP2.COL1 (+)
something like:
case
when INGRP1.COL2 IS NULL
then INGRP1.COL1 = INGRP2.COL1 (+)
else INGRP1.COL3 = INGRP2.COL3 (+)
end
expression builder for join condition.
Any help much appreciated.
Hi Kaiser,
this is my variant
1) join first table with second table twice (define two input group in join, include second table in mapping twice and map them to appropriate join group)
INGRP1.COL1=INGRP2.COL1(+) and INGRP1.COL3=INGRP3.COL3(+)
2) after join calculate attributes from second table with expression like
case when COL2 IS NULL the INGRP2_ATTR else INGRP3_ATTR end
Maybe anyone has a better solution...
Regards,
Oleg
Similar Messages
-
Hello everyone,
We have a function return Y or N.
We have 2 table YTable YT and NTable NT.
We are using UNION now.
SELECT ONE.CL1
FROM (
SELECT CL1Y FROM YTable WHERE FUNCTION = 'Y'
UNION
SELECT CL1N FROM NTable WHERE FUNCTION = 'N') ONEWe are wondering to use CASE, WHEN, THEN, END.
Is it possible or NOT?
If you have a chance, please share your experiences.
Thanks in advance,
NY
P.S.
If you have more questions, please let us know.
Edited by: New Yorker on Aug 31, 2010 10:08 AMNew Yorker,
According to the given SQL block, the case function can not be used. You can use the case function with multiple table joins but you can only use the CASE WHEN ELSE END block for one column.
If i got your question correct, that is my answer to your question.
Regards.
Ogan -
In my database, two variable ID and time (varchar2) are there. I want to populate another variable using case when then by putting below conditon to convert like this
But its not working. Can anyone help me
if time =1 day then '1 Day'
time > 2 days and time <=7 days then '2 to 7 days'
time > 8 days and time <=15 days then '8 to 15 days'
else 'not matching'
ID Time
12 1 days
23 244 days
12 2 days
14 4 days
15 6 days
17 9 days
select time,
case WHEN
UPPER(Time) = '1 DAY'
THEN
'1 day'
WHEN
UPPER(Time) Between '2 DAYS' and '7 DAYS'
THEN
'2 to 7 days'
WHEN
UPPER(Time) Between '8 DAYS' and '15 DAYS'
THEN
'8 to 15 days'
ELSE
'Not matching'
END "update time"
from table1900487 wrote:
In my database, two variable ID and time (varchar2) are there. If you are storing Days in a column store it as NUMBER or INTERVAL data type. The way you have stored the value looks incorrect.
What you can do is extract the number portion of your time column and apply the CASE statement. Something like this
SQL> with t
2 as
3 (
4 select 12 id, '1 days' time1 from dual union all
5 select 23 id, '244 days' time1 from dual union all
6 select 12 id, '2 days' time1 from dual union all
7 select 14 id, '4 days' time1 from dual union all
8 select 15 id, '6 days' time1 from dual union all
9 select 17 id, '9 days' time1 from dual
10 )
11 select id,
12 time1,
13 case when time1 = 1 then '1 day'
14 when time1 between 2 and 7 then '2 to 7 days'
15 when time1 between 8 and 15 then '8 to 15 days'
16 else 'not matching'
17 end "update time"
18 from (
19 select id, to_number(regexp_substr(time1, '^[[:digit:]]*')) time1
20 from t
21 )
22 /
ID TIME1 update time
12 1 1 day
23 244 not matching
12 2 2 to 7 days
14 4 2 to 7 days
15 6 2 to 7 days
17 9 8 to 15 days
6 rows selected. -
Using CASE WHEN THEN in dynamic Region Source area
Hi all,
I've been knocking my head around on this one for a while. Hope someone can show me some guidance. So far, the SQL Query in the Region Source works ok except after the THEN ''||'LINK'||'' . All text between the single quotes displays, and not just the hyperlink LINK. (My goal is to this simple test working, then go back an substitute in APP_ID, Page#, SESSION variables.) But I'm stuck with this problem. Searched the forum but this is as far as I got. Below is the code I'm using in the Region Source area. Perhaps a region attribute setting is needed but I'm unsure. Thanks for any assistance. -Mike
select DISTINCT
AWARD,
CASE
WHEN WINNER_NAME = 'India Retail Support Team'
THEN '<A HREF="http://www.hotmail.com">'||'LINK'||'</A>' *----------------HREF link (LINK) is working fine here but doesn't work ok in Region Source.
ELSE WINNER_NAME
END WN,
GBU_WINNER,
DORDER
from GBUSERVICEPACESETTERAWARDS
where FY = '2012'
and TYPE = 'Service Award'>
As previously requested, please update your forum profile with a real handle instead of "user10734329".
I've been knocking my head around on this one for a while. Hope someone can show me some guidance. So far, the SQL Query in the Region Source works ok except after the THEN ''||'LINK'||'' . All text between the single quotes displays, and not just the hyperlink LINK.Not clear from this what the problem is. It appears to me that LINK is all of the "text between the single quotes". What do you mean by "all text"? (An example on apex.oracle.com is a good way to resolve such ambiguities...)
The usual cause of unexpected results when the separation of concerns is breached and HTML is directly generated in report queries is having the Display Text As report column attribute wrongly set. Ensure it is Standard Report Column rather than the default Display as Text (escape special characters). -
My nested CASE WHEN THEN ELSE is not working.
I'm working on a report that will provide a status on a component based on a defined Matrix. The status to report is determined in a hierarchal fashion...the highest seqence number status existing is reported.
I've capped the sequence number at 10 (@SeqNumCap_sav)
The matrix table is defined as such:
CREATE TABLE [dbo].[tblAssyLineComponentStatusMatrix](
[MatrixAssyLine] [char](1) NOT NULL,
[MatrixComponentProduct] [char](15) NOT NULL,
[MatrixComponentStatusSequence] [decimal](3, 0) NOT NULL,
[MatrixComponentStatus] [char](3) NOT NULL,
[MatrixStatusDescription] [nvarchar](100) NOT NULL,
[MatrixReportedDescription1] [nvarchar](50) NOT NULL,
[MatrixReportedDescription2] [nvarchar](50) NULL,
[MatrixReportedDescription3] [nvarchar](50) NULL,
[MatrixReportedDescription4] [nvarchar](50) NULL,
[MatrixReportedDescription5] [nvarchar](50) NULL
) ON [PRIMARY]
This is a sample of the tblAssyLineComponentStatusMatrix data:
MatrixAssyLine MatrixComponentProduct MatrixComponentStatusSequence MatrixComponentStatus MatrixStatusDescription MatrixReportedDescription1 MatrixReportedDescription2 MatrixReportedDescription3 MatrixReportedDescription4 MatrixReportedDescription5
E Mast/PullRod 1 W The part has been stamped or welded. Class3PullRods NULL NULL NULL NULL
E Mast/PullRod 2 P The part has been clicked off in a paint hang station. TSMastPaintHang NULL NULL NULL NULL
E Mast/PullRod 3 Y The part has been clicked off in paint pulldown. TSMastPaintUnload NULL NULL NULL NULL
the tblProductionControlComponentReporting is defined as:
CREATE TABLE [dbo].[tblProductionControlComponentReporting](
[WorkUnit] [nvarchar](15) NOT NULL,
[Description] [nvarchar](50) NOT NULL,
[Completed] [datetime] NOT NULL,
[UserID] [nvarchar](30) NULL,
[StationID] [nvarchar](30) NULL,
[Undo] [bit] NULL,
[CompletedUndo] [datetime] NULL,
[UserIDUndo] [nvarchar](30) NULL,
[StationIDUndo] [nvarchar](30) NULL,
[ComponentPartNo] [varchar](15) NULL,
[ComponentClass] [varchar](3) NULL,
[ComponentQty] [decimal](6, 2) NULL,
[ComponentScheduleDate] [datetime] NULL,
[ComponentScheduleShift] [decimal](1, 0) NULL,
[ComponentScheduleWorkunitSequence] [int] NULL,
[ComponentComment] [varchar](200) NULL
) ON [PRIMARY]
A sample of the tblProductionControlComponentreporting data:
assyline WorkUnit Description Completed UserID StationID
E 639422 Class3PullRods 2014-09-15 13:15:44.607 GLOBAL\agmesusr ag2100156
E 639422 Class3PullRods 2014-09-15 13:15:44.607 GLOBAL\agmesusr ag2100156
E 639422 TSFrameFabDeliver 2014-09-25 11:31:44.380 NULL MCA
E 639422 TSMastPaintHang 2014-09-25 22:56:43.740 009932 AG2100294
The problem is that the below code is returning multple records which is causing an error ... if I resequence the above Matrix table data where #3 becomes #10, 2 becomes #9 and 1 becomes #8, the query works fine - but this approach doesn't allow for easy
expansion of the data. Can anyone help me see the problem here?
use eschedule
declare @Workunit varchar(max)
declare @SeqNumCap_sav dec(3, 0)
set @WorkUnit='639422'
set @SeqNumCap_sav= (select top 1 SequenceValueCap from tblAssyLineComponentStatusMatrix_SequenceCap order by SequenceValueCap)
select
Mast_PullRodStatus = case
--Seq 10 Mast/PullRod Component Status
when (select top 1 [description] from tblproductioncontrolcomponentreporting
where ([description] = (select MatrixReportedDescription1 from tblAssyLineComponentStatusMatrix where MatrixComponentProduct='Mast/PullRod' and MatrixComponentStatusSequence=@SeqNumCap_sav and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit))))
and Workunit=@WorkUnit
) is not null
then
(select MatrixComponentStatus from tblAssyLineComponentStatusMatrix
where MatrixComponentProduct='Mast/PullRod'
and MatrixComponentStatusSequence=@SeqNumCap_sav
and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit)))
--Seq 9 Mast/PullRod Component Status
else case
when (select top 1 [description] from tblproductioncontrolcomponentreporting
where ([description] = (select MatrixReportedDescription1 from tblAssyLineComponentStatusMatrix
where MatrixComponentProduct='Mast/PullRod'
and MatrixComponentStatusSequence=(@SeqNumCap_sav-1)
and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit))))
and Workunit=@WorkUnit
) is not null
then (select MatrixComponentStatus from tblAssyLineComponentStatusMatrix
where MatrixComponentProduct='Mast/PullRod'
and MatrixComponentStatusSequence=(@SeqNumCap_sav-1)
and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit)))
--Seq 8 Mast/PullRod Component Status
else case
when (select top 1 [description] from tblproductioncontrolcomponentreporting
where ([description] = (select MatrixReportedDescription1 from tblAssyLineComponentStatusMatrix
where MatrixComponentProduct='Mast/PullRod'
and MatrixComponentStatusSequence=(@SeqNumCap_sav-2)
and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit))))
and Workunit=@WorkUnit
) is not null
then (select MatrixComponentStatus from tblAssyLineComponentStatusMatrix
where MatrixComponentProduct='Mast/PullRod'
and MatrixComponentStatusSequence=(@SeqNumCap_sav-2)
and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit)))
--Seq 7 Mast/PullRod Component Status
else case
when (select top 1 [description] from tblproductioncontrolcomponentreporting
where ([description] = (select MatrixReportedDescription1 from tblAssyLineComponentStatusMatrix
where MatrixComponentProduct='Mast/PullRod'
and MatrixComponentStatusSequence=(@SeqNumCap_sav-3)
and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit))))
and Workunit=@WorkUnit
) is not null
then (select MatrixComponentStatus from tblAssyLineComponentStatusMatrix
where MatrixComponentProduct='Mast/PullRod'
and MatrixComponentStatusSequence=(@SeqNumCap_sav-3)
and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit)))
--Seq 6 Mast/PullRod Component Status
else case
when (select top 1 [description] from tblproductioncontrolcomponentreporting
where ([description] = (select MatrixReportedDescription1 from tblAssyLineComponentStatusMatrix
where MatrixComponentProduct='Mast/PullRod'
and MatrixComponentStatusSequence=(@SeqNumCap_sav-4)
and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit))))
and Workunit=@WorkUnit
) is not null
then (select MatrixComponentStatus from tblAssyLineComponentStatusMatrix
where MatrixComponentProduct='Mast/PullRod'
and MatrixComponentStatusSequence=(@SeqNumCap_sav-4)
and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit)))
--Seq 5 Mast/PullRod Component Status
else case
when (select top 1 [description] from tblproductioncontrolcomponentreporting
where ([description] = (select MatrixReportedDescription1 from tblAssyLineComponentStatusMatrix
where MatrixComponentProduct='Mast/PullRod'
and MatrixComponentStatusSequence=(@SeqNumCap_sav-5)
and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit))))
and Workunit=@WorkUnit
) is not null
then (select MatrixComponentStatus from tblAssyLineComponentStatusMatrix
where MatrixComponentProduct='Mast/PullRod'
and MatrixComponentStatusSequence=(@SeqNumCap_sav-5)
and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit)))
--Seq 4 Mast/PullRod Component Status
else case
when (select top 1 [description] from tblproductioncontrolcomponentreporting
where ([description] = (select MatrixReportedDescription1 from tblAssyLineComponentStatusMatrix
where MatrixComponentProduct='Mast/PullRod'
and MatrixComponentStatusSequence=(@SeqNumCap_sav-6)
and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit))))
and Workunit=@WorkUnit
) is not null
then (select MatrixComponentStatus from tblAssyLineComponentStatusMatrix
where MatrixComponentProduct='Mast/PullRod'
and MatrixComponentStatusSequence=(@SeqNumCap_sav-6)
and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit)))
--Seq 3 Mast/PullRod Component Status
else case
when (select top 1 [description] from tblproductioncontrolcomponentreporting
where ([description] = (select MatrixReportedDescription1 from tblAssyLineComponentStatusMatrix
where MatrixComponentProduct='Mast/PullRod'
and MatrixComponentStatusSequence=(@SeqNumCap_sav-7)
and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit))))
and Workunit=@WorkUnit
) is not null
then (select MatrixComponentStatus from tblAssyLineComponentStatusMatrix
where MatrixComponentProduct='Mast/PullRod'
and MatrixComponentStatusSequence=(@SeqNumCap_sav-7)
and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit)))
--Seq 2 Mast/PullRod Component Status
else case
when (select top 1 [description] from tblproductioncontrolcomponentreporting
where ([description] = (select MatrixReportedDescription1 from tblAssyLineComponentStatusMatrix
where MatrixComponentProduct='Mast/PullRod'
and MatrixComponentStatusSequence=(@SeqNumCap_sav-8)
and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit))))
and Workunit=@WorkUnit
) is not null
then (select MatrixComponentStatus from tblAssyLineComponentStatusMatrix
where MatrixComponentProduct='Mast/PullRod' and MatrixComponentStatusSequence=(@SeqNumCap_sav-8))
--Seq 1 Mast/PullRod Component Status
else case
when (select top 1 [description] from tblproductioncontrolcomponentreporting
where ([description] = (select MatrixReportedDescription1 from tblAssyLineComponentStatusMatrix
where MatrixComponentProduct='Mast/PullRod'
and MatrixComponentStatusSequence=(@SeqNumCap_sav-9)
and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit))))
and Workunit=@WorkUnit
--and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit))
) is not null
then (select MatrixComponentStatus from tblAssyLineComponentStatusMatrix
where MatrixComponentProduct='Mast/PullRod'
and MatrixComponentStatusSequence=(@SeqNumCap_sav-9)
and (MatrixAssyLine=(select assyline from tblProductionControlHeader h where h.workunit=@WorkUnit)))
else ' '
end
end
end
end
end
end
end
end
end
end
Assume Assyline='E'
Eva LeggettGood job posting the DDL.
Your example data should be in a table format though.
DECLARE @tblAssyLineComponentStatusMatrix TABLE (MatrixAssyLine char(1) NOT NULL, MatrixComponentProduct char(15) NOT NULL, MatrixComponentStatusSequence decimal(3, 0) NOT NULL, MatrixComponentStatus char(3) NOT NULL, MatrixStatusDescription nvarchar(100) NOT NULL, MatrixReportedDescription1 nvarchar(50) NOT NULL, MatrixReportedDescription2 nvarchar(50) NULL, MatrixReportedDescription3 nvarchar(50) NULL, MatrixReportedDescription4 nvarchar(50) NULL, MatrixReportedDescription5 nvarchar(50) NULL)
INSERT INTO @tblAssyLineComponentStatusMatrix (MatrixAssyLine, MatrixComponentProduct, MatrixComponentStatusSequence, MatrixComponentStatus, MatrixStatusDescription, MatrixReportedDescription1, MatrixReportedDescription2, MatrixReportedDescription3, MatrixReportedDescription4, MatrixReportedDescription5) VALUES ('E' ,'Mast/PullRod' ,1 ,'W' ,'The part has been stamped or welded. ','Class3PullRods ' ,NULL, NULL, NULL, NULL)
INSERT INTO @tblAssyLineComponentStatusMatrix (MatrixAssyLine, MatrixComponentProduct, MatrixComponentStatusSequence, MatrixComponentStatus, MatrixStatusDescription, MatrixReportedDescription1, MatrixReportedDescription2, MatrixReportedDescription3, MatrixReportedDescription4, MatrixReportedDescription5) VALUES ('E' ,'Mast/PullRod' ,2 ,'P' ,'The part has been clicked off in a paint hang station. ','TSMastPaintHang ' ,NULL, NULL, NULL, NULL)
INSERT INTO @tblAssyLineComponentStatusMatrix (MatrixAssyLine, MatrixComponentProduct, MatrixComponentStatusSequence, MatrixComponentStatus, MatrixStatusDescription, MatrixReportedDescription1, MatrixReportedDescription2, MatrixReportedDescription3, MatrixReportedDescription4, MatrixReportedDescription5) VALUES ('E' ,'Mast/PullRod' ,3 ,'Y' ,'The part has been clicked off in paint pulldown. ','TSMastPaintUnload ' ,NULL, NULL, NULL, NULL)
DECLARE @tblProductionControlComponentReporting TABLE (WorkUnit nvarchar(15) NOT NULL, Description nvarchar(50) NOT NULL, Completed datetime NOT NULL, UserID nvarchar(30) NULL, StationID nvarchar(30) NULL, Undo bit NULL, CompletedUndo datetime NULL, UserIDUndo nvarchar(30) NULL, StationIDUndo nvarchar(30) NULL, ComponentPartNo varchar(15) NULL, ComponentClass varchar(3) NULL, ComponentQty decimal(6, 2) NULL, ComponentScheduleDate datetime NULL, ComponentScheduleShift decimal(1, 0) NULL, ComponentScheduleWorkunitSequence int NULL, ComponentComment varchar(200) NULL)
INSERT INTO @tblProductionCOntrolComponentReporting (WorkUnit, Description, Completed, UserID, StationID) VALUES (639422, 'Class3PullRods', '2014-09-15 13:15:44.607', 'GLOBAL\agmesusr', 'ag2100156')
INSERT INTO @tblProductionCOntrolComponentReporting (WorkUnit, Description, Completed, UserID, StationID) VALUES (639422, 'Class3PullRods', '2014-09-15 13:15:44.607', 'GLOBAL\agmesusr', 'ag2100156')
INSERT INTO @tblProductionCOntrolComponentReporting (WorkUnit, Description, Completed, UserID, StationID) VALUES (639422, 'TSFrameFabDeliver', '2014-09-25 11:31:44.380', NULL, 'MCA')
INSERT INTO @tblProductionCOntrolComponentReporting (WorkUnit, Description, Completed, UserID, StationID) VALUES (639422, 'TSMastPaintHang', '2014-09-25 22:56:43.740', '009932', 'AG2100294')
Your code is a mess, it would take longer to untangle it that it would to solve your problem.
Can you give us an expected output, and perhaps the rules governing what your case statement should be doing? -
Error when attempting to validate Join Condition
In a mapping, I include a joiner and establish the join condiiton. When clicking on the Validate button, I recieve the following error: "An error occurred during expression validation. Bad expression return type."
At first, we thought this was a problem when creating a new mapping. However, we went back to existing mappings that previously validated successfully and each join is returning this OWB error now. This happens for each user with OWB installed. The mapping itself will validate successfully in the end but the join itself will not validate during development.
We are currently using OWB Client 10.2.0.4.36 and OWB Repository 10.2.0.4.0. Did the latest patch cause this issue? Any way to resolve this issue?
Thanks,
Jennifer... it's a bug, not a feature:
09/17/08 03:26 pm
Version & machine info: ========================
OWB Client/OS: OWB 10.2.0.4.36/Windows
OWB Design Repository Version: 10.2.0.4
Runtime Repository Server Platform/Version: Windows XP/10.2.0.4.36
Oracle Database/OS : 10.2.0.3/Windows XP
SOURCE & TARGET on the same machine or different machine ? yes
SOURCE database version/OS: 10.2.0.3
TARGET database version/OS: 10.2.0.3 .
Problem Description ======================== .
Filter and Join Mapping Expressions always fail the Validation Check with the following dialogue: "An error occured during expression validation. Bad expression return type". It does not matter what the expression is. . This is a new bug in 10.2.0.4. The same exppressions validate successfully in 10.2.0.3 . The code generates and deploys successfully after this message. . . . Reproducible Case (For All bugs) ================================= . 1)START OWB Design and connect 2)Import the uploaded OWB 10.2.0.4 MDL file: expression_validate_bug.mdl 3)OPEN the Project TESTCASE 4)OPEN The Oracle Module EXPRESSION_VALIDATE_BUG 5)OPEN the mapping EXPRESSION_VALIDATE_ERROR 8)Highlight the Filter 9)Open the Filter Condition Doialogue Box. 10)Select the validate button. 11)Observe error.
BugNo: 7417869 vom 17.09.08Regards,
Detlef -
Using case when statement or decode stament in where clause
hi gems..
i have a problem in the following query..
i am trying to use case when statement in the where clause of a select query.
select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
cr.salary as salary
from customer_details cr
where (case when '>' = '>' then 'cr.salary > 5000'
when '>' = '<' then 'cr.salary < 5000'
when '>' = '=' then 'cr.salary = 5000'
else null
end);
the expression in the when clause of the case-when statement will come from UI and depending on the choice i need to make the where clause.
thats why for running the query, i have put '>' in that place.
so the original query will look like this(for your reference):
select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
cr.salary as salary
from customer_details cr
where (case when variable = '>' then 'cr.salary > 5000'
when variable = '<' then 'cr.salary < 5000'
when variable = '=' then 'cr.salary = 5000'
else null
end);
so, in actual case,if the user selects '>' then the filter will be "where cr.salary > 5000"
if the user selects '<' then the filter will be "where cr.salary < 5000"
if the user selects '=' then the filter will be "where cr.salary = 5000"
but i am getting the error "ORA 00920:invalid relational operator"
please help..thanks in advance..Hi,
select cr.customer_name || ' - ' ||cr.customer_number as cust_name,
cr.salary as salary
from customer_details cr
where ( v_variable = 'bigger'
and cr.salary > 5000
or ( v_variable = 'less'
and cr.salary < 5000
or ( v_variable = 'eq'
and cr.salary = 5000
)Edited by: user6806750 on 22.12.2011 14:56
For some reason I can't write in sql '<', '>', '=' -
I have a situation where i need to write a case when sql expression to fetch certain rows from a table.
How to use CASE WHEN THEN ELSE END in a report query ?
Explanation with a simple example will be of great help.
thanksThe javadoc for oracle.toplink.expressions.Expression
contains some information.
The following is an example of how to build the case statement.
This will use case to replace the keys in the caseTable with the values. The default value (the ELSE) is provided as the second argument.
Hashtable caseTable = new Hashtable(3);
caseTable.put("Bob", "Bobby");
caseTable.put("Susan", "Susie");
caseTable.put("Eldrick", "Tiger");
Expression expression = builder.get("firstName").caseStatement(caseTable, "No-Nickname").equal("Bobby"); -
How to return multiple rows in case when condition?
I want to display the list of product names owned by a company.If no products found it should return a string like "NO PRODUCTS FOUND'
The query I wrote working well but when the condition does not satisfy Its not returning "NO PRODUCTS FOUND''. Can someone help me how can I do this?
declare @Country varchar(100)='Netherlands'
SELECT CASE WHEN count(table4.ProductName) >0 THEN table4.ProductName
ELSE 'NO PRODUCTS FOUND' END AS Product_list
FROM table2 INNER JOIN
table1 ON table2.AccountID = table1.AccountID INNER JOIN
table3 ON table1.[company_ID] = table3.[company_ID] INNER JOIN
table4 ON table4.ProductID = table2.ProductID
WHERE
table1.[country] IN (@Country)) AND table4.ProductID IN (24410, 24413, 24418, 24383, 24384))
GROUP BY table4.ProductNameYour DDL does not match your query.
You also supplied no test data.
You're using INNER JOINS to enforce relationships that may not exist. You're using a WHERE clause to do the same.
Try this:
DECLARE @table1 TABLE (accountid int not null, productID varchar(20), company_id int)
DECLARE @table2 TABLE (accountid int not null, country varchar(20))
DECLARE @table3 TABLE (company_id int not null, company_name varchar(20))
DECLARE @table4 TABLE (productID int not null, productname varchar(20))
INSERT INTO @table2 (accountid, country) VALUES (1, 'Netherlands'),(2, 'Netherlands'),(3, 'Germany')
INSERT INTO @table1 (accountid, productID, company_id) VALUES (1,24410,1)
INSERT INTO @table3 (company_id, company_name) VALUES (1,'one company'),(2,'two company'),(3,'three company')
INSERT INTO @table4 (productID, productname) VALUES (24410,'a product')
DECLARE @Country varchar(100)='Netherlands'
SELECT t3.company_name,
CASE WHEN count(t4.ProductName) > 0 THEN t4.ProductName
ELSE 'NO PRODUCTS FOUND'
END AS Product_list
FROM @table2 t2
INNER JOIN @table3 t3
ON t2.accountid = t3.company_id
LEFT OUTER JOIN @table1 t1
ON t2.accountid = t1.accountid
LEFT OUTER JOIN @table4 t4
ON t1.productID = t4.productID
WHERE t2.country = @Country
GROUP BY t3.company_name, t4.productName -
CASE WHEN statement IS NULL THEN ......
-- i've table's acoount and owner
create table account
( code-number(30) , name-nvarchar(200))
create table owner
( code-nvarchar2(20 BYTE) ,
name -nvarchar(100),
account_code-number(30)) account_code is foreign key
code
name
account_code
1.1
a
1.1
5
x
5
3.3
c
3.3
7
y
19
dd
code
name
1.1
z
5
b
3.3
c
3.3
c
now my result should be
code(owner)
name(owner)
code(account)
name(account)
1.1
a
1.1
z
5
x
5.
b
3.3
c
3.3
c
7
y
19
dd
my script
SELECT CASE WHEN O.ACCOUNT_CODE IS NULL THEN A.CODE=NULL ELSE A.CODE=(VALUE) END, // HOW SHOULD I WRTIE HERE ???
CASE WHEN O.ACCOUNT_CODE IS NULL THEN A.NAME=NULL ELSE A.NAME=(VALUE) END,
O.CODE,
O.NAME
FROM OWNER O,
ACCOUNT A
WHERE A.CODE = O.ACCOUNT_CODE;I don't see the need for case statement here. Cant you just do this.
SQL> with owner
2 as
3 (
4 select 1.1 code, 'a ' name, 1.1 account_code from dual union all
5 select 5 code, 'x ' name, 5 account_code from dual union all
6 select 3.3 code, 'c ' name, 3.3 account_code from dual union all
7 select 7 code, 'y ' name, null account_code from dual union all
8 select 19 code, 'dd' name, null account_code from dual
9 ), account
10 as
11 (
12 select 1.1 code, 'z' name from dual union all
13 select 5 code, 'b' name from dual union all
14 select 3.3 code, 'c' name from dual union all
15 select 3.3 code, 'c' name from dual
16 )
17 select o.code "code (owner)"
18 , o.name "name (owner)"
19 , a.code "code (account)"
20 , a.name "name (account)"
21 from owner o
22 left
23 join (
24 select distinct code, name
25 from account
26 ) a
27 on o.account_code = a.code
28 order
29 by 1;
code (owner) name (owner) code (account) name (account)
1.1 a 1.1 z
3.3 c 3.3 c
5 x 5 b
7 y
19 dd -
Is there any way to simplyfy this sql using case when or any other condition
Hi,
Please ca you advise me to simplyfy the given sql
SELECT
CASE
WHEN AxleNo=1 and AxleType = 'OFFSIDE OUTER'
THEN SIZE
END AS off_1st_size_txt_out,
CASE
WHEN AxleNo=1 and AxleType = 'OFFSIDE OUTER'
THEN make
END AS off_1st_make_txt_out,
CASE
WHEN AxleNo=1 and AxleType = 'OFFSIDE OUTER'
THEN pattern
END AS off_1st_pat_txt_out,
CASE
WHEN AxleNo=1 and AxleType = 'OFFSIDE OUTER'
THEN TreadDepth1
END AS off_1st_size_out,
CASE
WHEN AxleNo=1 and AxleType = 'OFFSIDE OUTER'
THEN TreadDepth2
END AS off_1st_make_out,
CASE
WHEN AxleNo=1 and AxleType = 'OFFSIDE OUTER'
THEN TreadDepth3
END AS off_1st_pat_out,
--1st near side outer
CASE
WHEN AxleNo=1 and AxleType = 'NEARSIDE OUTER'
THEN SIZE
END AS near_1st_size_txt_out,
CASE
WHEN AxleNo=1 and AxleType = 'NEARSIDE OUTER'
THEN make
END AS near_1st_make_txt_out,
CASE
WHEN AxleNo=1 and AxleType = 'NEARSIDE OUTER'
THEN pattern
END AS near_1st_pat_txt_out,
CASE
WHEN AxleNo=1 and AxleType = 'NEARSIDE OUTER'
THEN TreadDepth1
END AS near_1st_size_out,
CASE
WHEN AxleNo=1 and AxleType = 'NEARSIDE OUTER'
THEN TreadDepth2
END AS near_1st_make_out,
CASE
WHEN AxleNo=1 and AxleType = 'NEARSIDE OUTER'
THEN TreadDepth3
END AS near_1st_pat_out,
--2nd Offside Outer
CASE
WHEN AxleNo=2 and AxleType = 'OFFSIDE OUTER'
THEN SIZE
END AS off_2nd_size_txt_out,
CASE
WHEN AxleNo=2 and AxleType = 'OFFSIDE OUTER'
THEN make
END AS off_2nd_make_txt_out,
CASE
WHEN AxleNo=2 and AxleType = 'OFFSIDE OUTER'
THEN pattern
END AS off_2nd_pat_txt_out,
CASE
WHEN AxleNo=2 and AxleType = 'OFFSIDE OUTER'
THEN TreadDepth1
END AS off_2nd_size_out,
CASE
WHEN AxleNo=2 and AxleType = 'OFFSIDE OUTER'
THEN TreadDepth2
END AS off_2nd_make_out,
CASE
WHEN AxleNo=1 and AxleType = 'OFFSIDE OUTER'
THEN TreadDepth3
END AS off_2nd_pat_out
from my fleets
With Many Thanks
Pol
polachanHello,
the same result ( maybe with records in slightly different order ), you can obtain using this code snippet.
SELECT SIZE AS off_1st_size_txt_out,
make AS off_1st_make_txt_out,
pattern AS off_1st_pat_txt_out,
TreadDepth1 AS off_1st_size_out,
TreadDepth2 AS off_1st_make_out,
TreadDepth3 AS off_1st_pat_out,
NULL AS near_1st_size_txt_out,
NULL AS near_1st_make_txt_out,
NULL AS near_1st_pat_txt_out,
NULL AS near_1st_size_out,
NULL AS near_1st_make_out,
NULL AS near_1st_pat_out,
NULL AS off_2nd_size_txt_out,
NULL AS off_2nd_make_txt_out,
NULL AS off_2nd_pat_txt_out,
NULL AS off_2nd_size_out,
NULL AS off_2nd_make_out,
NULL AS off_2nd_pat_out
WHERE AxleNo=1 and AxleType = 'OFFSIDE OUTER'
from my fleets
UNION
SELECT NULL AS off_1st_size_txt_out,
NULL AS off_1st_make_txt_out,
NULL AS off_1st_pat_txt_out,
NULL AS off_1st_size_out,
NULL AS off_1st_make_out,
NULL AS off_1st_pat_out,
SIZE AS near_1st_size_txt_out,
make AS near_1st_make_txt_out,
pattern AS near_1st_pat_txt_out,
TreadDepth1 AS near_1st_size_out,
TreadDepth2 AS near_1st_make_out,
TreadDepth3 AS near_1st_pat_out,
NULL AS off_2nd_size_txt_out,
NULL AS off_2nd_make_txt_out,
NULL AS off_2nd_pat_txt_out,
NULL AS off_2nd_size_out,
NULL AS off_2nd_make_out,
NULL AS off_2nd_pat_out
WHERE AxleNo=1 and AxleType = 'NEARSIDE OUTER'
from my fleets
UNION
SELECT NULL AS off_1st_size_txt_out,
NULL AS off_1st_make_txt_out,
NULL AS off_1st_pat_txt_out,
NULL AS off_1st_size_out,
NULL AS off_1st_make_out,
NULL AS off_1st_pat_out,
NULL AS near_1st_size_txt_out,
NULL AS near_1st_make_txt_out,
NULL AS near_1st_pat_txt_out,
NULL AS near_1st_size_out,
NULL AS near_1st_make_out,
NULL AS near_1st_pat_out,
SIZE AS off_2nd_size_txt_out,
make AS off_2nd_make_txt_out,
pattern AS off_2nd_pat_txt_out,
TreadDepth1 AS off_2nd_size_out,
TreadDepth2 AS off_2nd_make_out,
TreadDepth3 AS off_2nd_pat_out
WHERE AxleNo=2 and AxleType = 'OFFSIDE OUTER'
from my fleets
If you find it easier to understand and to maintain. The result is made using "UNION" .
Šimon -
In which join condition case, it will go to infiniti loop , in Oracle data
Hi Experts,
Can any one please tell me , in which join condition case, it will go to infiniti loop , in Oracle database.
Thanks&Regards,
Sanjeev.user13483989 wrote:
Hi Experts,
Can any one please tell me , in which join condition case, it will go to infiniti loop , in Oracle database.
Thanks&Regards,
Sanjeev.with faulty data
Handle: user13483989
Status Level: Newbie (15)
Registered: Jan 16, 2011
Total Posts: 192
Total Questions: 87 (73 unresolved)
why so many unanswered questions? -
'max case when...then' does it work in Desktop reports as a calculation?
Hi!
I've used 'max case when...then' in my SQL in Admin edition. It works fine. However when I try to use it as a part of my calculation:
MAX(CASE
WHEN (:Payroll Month 1 - Gross Sal)
THEN TO_NUMBER(NVL("Gross Salary ".Basic Salary, 0)) Else 0 END)
in this manner, it gives me the error message that
'Error in formula - Unexpected end of formula - syntax error : Token THEN at position 52'
Can someone guide me please?
Thanks and regards,
AparnaAparna.
You cannot - without some function coding - pass Discoverer run time parameters to the Admin edition (ie: to a folder, SQL view, etc.).
If you're tyring to write a calculation like you describe, can't this just be in the Plus or Desktop version and not Admin?
So, it's a calculation in the report and not the EUL?
Russ -
This might be very basic question still I'm confused about it.
Can you please explain me "CASE WHEN 1=0 THEN.......ELSE....END"?
Where and why to use this statement??
Help appreciated.First let's break down what it means. Since 1 is never equal to 0, OBI will ignore what comes immediately after and execute what comes after the ELSE.
So why would we want to do this? Well, often we want to perform some action that has nothing to do with a particular column in our subject area. By using this CASE statement, we trick OBI into thinking we are using a column for a calculation or action, but in effect, the column is unchanged.
For example, suppose you were building a dashboard prompt. You want to use a column twice in your dashboard prompt, say an ACCOUNT_OPEN_DATE column to get a range of dates. You cannot use the same column twice in a dashboard prompt. (Try it, OBI will ignore your attempt to put the same column twice into your workspace.)
So what do you do? First you get a column (in this example, I'm using a CHAR column) like Branch Name. It doesn't matter what column you use. The CHAR is used so the syntax makes sense.
You move the column to your workspace. Now you click on the Edit Formula and you type CASE WHEN 1=0 THEN Organization."Branch Name" ELSE 'TEST' END.
So in this case, the values of Branch Name are unaffected and the dummy column just represents the word 'TEST.' Now in the "Show" part of the prompt, we switch to "SQL Results" and type SELECT "Account Attributes".Account_Open_Date FROM Subject Area and set this to a PV called StartDate.
Because the column is a dummy, you can actually call it again. Use the same CASE statement to make it a "dummy" column. Then use the same SQL you used above, but this time save it to a PV called EndDate.
Now you have two date values you can use on your ACCOUNT_OPEN_DATE column to get a range of dates.
So, in conclusion, normally you choose a column from your subject area, because you need the values of that column in your report. But when you just need a column to do something unrelated to any column, you use the CASE 1=0 to make it a "dummy" column and then you can perform your action.
HTH, -
"case when not m[1] is present then" outputs ORA-03113
I find that "case when not m[1] is present then" outputs ORA-03113:
Why?
SQL> select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
select * from dual model
dimension by(0 as d)
measures(1 as m)
rules(
m[any] = case when not m[1] is present then 1 end);
ORA-03113:Below one is alterNative solution.
select * from dual model
dimension by(0 as d)
measures(1 as m)
rules(
m[any] = case when presentv(m[1],1,0) = 0 then 3 end);
D M
0 3I get:
ops$me%DB> select * from dual model
2 dimension by(0 as d)
3 measures(1 as m)
4 rules(
5 m[any] = case when not m[1] is present then 1 end);
select * from dual model
ERROR at line 1:
ORA-03002: operator not implemented
Elapsed: 00:00:00.03
ops$me%DB> select banner from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
TNS for IBM/AIX RISC System/6000: Version 10.2.0.4.0 - Productio
NLSRTL Version 10.2.0.4.0 - Production
Maybe you are looking for
-
Web item : List of documents of an infoprovider
I have problems to display all documents of an infoprovider in a web item "list of documents" of a web template. I chose as type of data provider : SELECTOR_DATA_PROVIDER and selected an infoprovider but I have the error message : " The referenced da
-
HT6058 how do i do this update iOS 7.0.4
I do not know how to do this iOS 7.0.4 , my I PHONE4S is telling me that i have an udate to do and i am not able to do the update its requesting for me to do. Because i just dont know how to do it. Please help me out here with this . Thank you so muc
-
Accessing a shared mailbox in addition to regular account.
I'm new to Blackberry tech talk so please bare with me. I have a blackberry and the company I work for has a BES. At work, on Outlook, I have a shared mailbox that I have access to in addition to my Outlook account/proflie. Is there a way that I ca
-
Hi, I am a new Mac user and I am trying to install Adobe Reader. It keeps telling me to close Safari before it completes download but there is nothing open!? I am confused.
-
Hi there, I started to use iPhoto and my fotostream showed up. But there are also a lot of different events named as date and they are empty (almost all of them). I also see my fotostream is divided into months. I have two fotostreams with month 4 an