View previous topic :: View next topic
|
Author |
Message |
srajanbose
New User
Joined: 11 Oct 2004 Posts: 69 Location: chennai
|
|
|
|
Hi ,
I am trying to execute the below query
Code: |
SELECT * FROM USI.TSSIMB
WHERE SIMB_CD_BP IN
(CASE
WHEN (SELECT COUNT(*) FROM USI.TSSIMD
WHERE SIMD_ID_SIM = 23
AND SIMD_TYPE ='BP') > 0
THEN
(SELECT SIMD_VALUE1 FROM USI.TSSIMD
WHERE SIMD_TYPE ='BP'
AND SIMD_ID_SIM = 23)
ELSE
(SELECT DISTINCT SIMB_CD_BP FROM USI.TSSIMB)
END);
|
and i am getting sqlcode -811 with below description
Code: |
DSNT408I SQLCODE = -811, ERROR: THE RESULT OF AN EMBEDDED SELECT STATEMENT OR
A SUBSELECT IN THE SET CLAUSE OF AN UPDATE STATEMENT IS A TABLE OF
MORE THAN ONE ROW, OR THE RESULT OF A SUBQUERY OF A BASIC PREDICATE
IS MORE THAN ONE VALUE |
Please Guide me , how to solve this issue |
|
Back to top |
|
|
Nic Clouston
Global Moderator
Joined: 10 May 2007 Posts: 2455 Location: Hampshire, UK
|
|
|
|
Use DISTINCT as per your last line of the query. If that is not an option and you need one row then add more WHERE conditions. |
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3049 Location: NYC,USA
|
|
|
|
use
Code: |
FETCH FIRST ROW ONLY |
but it looks like you need to place this under cursor as you have a IN operator which meant to get more than 1 row. |
|
Back to top |
|
|
GuyC
Senior Member
Joined: 11 Aug 2009 Posts: 1281 Location: Belgium
|
|
|
|
A case can only return 1 value.
I think you want
Code: |
SELECT * FROM USI.TSSIMB
where SIMB_CD_BP IN
(SELECT SIMD_VALUE1 FROM USI.TSSIMD
WHERE SIMD_TYPE ='BP'
AND SIMD_ID_SIM = 23)
or
not exists (SELECT SIMD_VALUE1 FROM USI.TSSIMD
WHERE SIMD_TYPE ='BP'
AND SIMD_ID_SIM = 23)
|
|
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3049 Location: NYC,USA
|
|
|
|
GuyC , I think you might wants to consider
Code: |
SELECT DISTINCT SIMB_CD_BP FROM USI.TSSIMB |
condition as well. But I may be missing something.
He already might have got the solution but can also try below IF HE HAS A COBOL PROGRAM.
Code: |
SELECT col1,col2.....,'Y' FROM USI.TSSIMB
WHERE SIMB_CD_BP IN
(SELECT SIMD_VALUE1 FROM USI.TSSIMD
WHERE SIMD_TYPE ='BP'
AND SIMD_ID_SIM = 23)
UNION
SELECT col1,col2.....,'N' FROM USI.TSSIMB
WHERE SIMB_CD_BP IN
(SELECT DISTINCT SIMB_CD_BP FROM USI.TSSIMB)
FETCH FIRST ROW ONLY; |
And then check for Y and N in COBOL and process accordingly. I mean if Y is found then ignore 'N' else process 'N' record. |
|
Back to top |
|
|
chandan.inst
Active User
Joined: 03 Nov 2005 Posts: 275 Location: Mumbai
|
|
|
|
Hi,
I guess OP want to give 1st preference to below criteria
Code: |
SELECT SIMD_VALUE1 FROM USI.TSSIMD
WHERE SIMD_TYPE ='BP'
AND SIMD_ID_SIM = 23 |
if does not find anything with above criteria then only go for below criteria
Code: |
SELECT DISTINCT SIMB_CD_BP FROM USI.TSSIMB
|
Ranjan, Can you please confirm this is what your requirement is?
May be I am missing something, but GuyC's query will return rows with both conditions.
Rohit's query seems to work fine if row with 'Y' come on top always as a result of UNION query.
But my question to OP is why DISTINCT is required there as anyways with IN clause you will return multiple rows for given SIMB_CD_BP if there are any in table.
Correct me if I am missing anything
Regards,
Chandan |
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3049 Location: NYC,USA
|
|
|
|
Quote: |
But my question to OP is why DISTINCT is required there as anyways with IN clause you will return multiple rows for given SIMB_CD_BP if there are any in table. |
Right, Thats is why in my first reply I suggested this, Let us wait for TS to reply. However, DISTINCT is to get unique and multiple SIMB_CD_BP ( so that should be fine for him per his requirement)
Quote: |
Rohit's query seems to work fine if row with 'Y' come on top always as a result of UNION query. |
by adding
|
|
Back to top |
|
|
chandan.inst
Active User
Joined: 03 Nov 2005 Posts: 275 Location: Mumbai
|
|
|
|
Quote: |
However, DISTINCT is to get unique and multiple SIMB_CD_BP ( so that should be fine for him per his requirement)
|
my concern with DISTINCT was, irrespective of having distinct or not query result not going to change, but it will impact on performance with having distnict
Yes lets see if TS replies |
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3049 Location: NYC,USA
|
|
|
|
Quote: |
my concern with DISTINCT was, irrespective of having distinct or not query result not going to change, but it will impact on performance with having distnict |
Why will it not change the result? If I remove the DISTINCT then TS would be getting unwanted duplicate rows for same SIMB_CD_BP.
But the whole point is unless TS use the cursor or say 'FETCH FIRST ROW ONLY' to the query ,DQLCODE -811 would certainly be persist. |
|
Back to top |
|
|
chandan.inst
Active User
Joined: 03 Nov 2005 Posts: 275 Location: Mumbai
|
|
|
|
Hi Rohit,
Let me try to explain my view with distinct for below qurey
Code: |
WHERE SIMB_CD_BP IN
(SELECT DISTINCT SIMB_CD_BP FROM USI.TSSIMB) |
Let say table has following data for SIMB_CD_BP
Code: |
SIMB_CD_BP
1
1
2
2
3
3
3 |
Then for above data below both WHERE clause will return the same set of rows
Code: |
WHERE SIMB_CD_BP IN
(1,2,3)
WHERE SIMB_CD_BP IN
(1,2,3)
|
Kindly correct me if I missing anything here as I don't have system to check right now.
Regards,
Chandan |
|
Back to top |
|
|
chandan.inst
Active User
Joined: 03 Nov 2005 Posts: 275 Location: Mumbai
|
|
Back to top |
|
|
GuyC
Senior Member
Joined: 11 Aug 2009 Posts: 1281 Location: Belgium
|
|
|
|
did anyone notice this :
Code: |
SELECT * FROM mytable WHERE mycolumn IN
(SELECT mycolumn FROM mytable) |
I'm pretty sure this will always be true |
|
Back to top |
|
|
|