simple subquery not working DB2 - sql

hey guys this is a very simple sql query that is not giving me the correct result.
subquery:
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
this subquery successfully returns a correct value 627809
simple query:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (627809)
this query executes properly and returns 4 rows.(4 addresses for a member)
but if I try to combine these queries in 1 query as follows:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)
then the query returns 0 rows. why is this happening?
Thanks

Your query looks OK, the only I can think is maybe you mistake the value on the result.
can you try this:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT 627809
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)
and this
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT 627809
FROM NEODB2ADMIN.ORDERS
)

Since your Order (presumably) can only carry a single Member_ID -- can you please try your full query without the "IN", rather try an equal join as follows:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID = (
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)

Related

error incorporating a select within a IFNULL in MariaDB

I'm creating a view in MariaDB and i'm having trouble making it work for a couple of fields. Currently this is working:
( SELECT DISTINCT IFNULL(grades.`grade`,'No Grade')
FROM `table` grades
WHERE userinfo.`id` = grades.`id`
AND grades.`Item Name` = 'SOMEINFO'
) 'SOMENAME',
But i need to add a select where the 'No grade' is, in the following form
( SELECT DISTINCT IFNULL( grades.`grade`,
SELECT IF( EXISTS
( SELECT *
FROM `another_table`
WHERE userid = 365
AND courseid = 2
), 'Enrolled', 'Not enrolled'
)
)
FROM `table` grades
WHERE userinfo.`id` = grades.`id`
AND grades.`Item Name` = 'SOMEINFO'
) 'SOMENAME',
i know that
SELECT IF( EXISTS( SELECT *
FROM `another_table`
WHERE userid = 365
AND courseid = 2
),
'Enrolled', 'Not enrolled'
)
is working too, but now the whole thing it's giving me an error, so any suggestions would be greatly appreciated
Thanks
This looks like a subquery:
(SELECT DISTINCT IFNULL(grades.`grade`,
SELECT IF( EXISTS (SELECT *
FROM `another_table`
WHERE userid = 365 AND courseid = 2
), 'Enrolled', 'Not enrolled'
)
)
FROM `table` grades
WHERE userinfo.`id` = grades.`id` AND
grades.`Item Name` = 'SOMEINFO'
) as SOMENAME,
You are using a subquery that returns two columns in a position where a scalar subquery is expected. A scalar subquery returns one column in at most one row.
Unfortunately, there is no easy way to do what you want in MySQL, because of the restrictions on views. I would advise you to rewrite the logic so the exists is handled using a left join in the from clause.

select specific records using IN

I need to select records that has ID = 10,23,30 so I wrote this SQL
Select * from mytable where position(id in '10,23,30') > 0
But the problem I get additional records where ID = 1 and 2
Any ideas how to select only what I need ?
No need for position, just do IN:
Select * from mytable
where id in (10,23,30)
Use IN operator:
Select * from mytable where id in (10,23,30)

With clause subquery availablility for the entire SQL statement in Oracle

I am trying to write a SQL statement which reuses the subquery of the With clause multiple times in Oracle.
With mySubQ as (
...
)
Select Something
From SomeTable,
(
Select *
From mySubQ
where mySubQ.Something >= 0
) newSubQ
where mySubQ.Something = SomeTable.Something
This gives me error - ORA-32034 unsupported use of WITH clause
What am I missing?
You need to join with mySubQ, not just define it.
WITH mySubQ AS (...)
SELECT Something
FROM SomeTable
JOIN mySubQ ON mySubQ.Something = SomeTable.Something
WHERE mySubQ.Something >= 0
If you put the query of mySubQ in a subquery, you can't reference mySubQ in the WHERE clause of the main query. Each level of query can only access tables in its own FROM and JOIN clauses, not those of subqueries.
Here is the error: where mySubQ.Something = SomeTable.Something.
The bottom query selects from SomeTable and from the subquery with alias newSubQ,
so mySubQ.Something is not known in this context.
If something is a real column name, not only a "placeholder in the pseudocode", then there is also another error here: Select Something - the column is ambiguous, because both sometable and the subquery have this column.
Try this query:
With mySubQ as (
SELECT * FROM sometable
)
Select newSubQ.Something
From SomeTable,
(
Select *
From mySubQ
where mySubQ.Something >= 0
) newSubQ
where newSubQ.Something = SomeTable.Something
;
Demo --> http://www.sqlfiddle.com/#!4/88855/12
This demo contains also another example of using WITH clause:
WITH mySubQ AS (
SELECT *
FROM sometable
),
mySubQ_1 AS (
SELECT *
FROM mySubQ
WHERE somethingelse = 1
),
mySubQ_2 AS (
SELECT *
FROM mySubQ
WHERE something between 2 AND 5
)
SELECT *
FROM sometable s, mySubQ_1 m1,
(
SELECT * FROM mySubQ_2
WHERE something < 10
) m2
WHERE s.something = m1.something
AND m1.somethingelse = m2.somethingelse

jpql INTERSECT without INTERSECT

i have four queries that return intergers.
select listOfIntegers from [something]...
(edit: the results are ROWS)
and need a way to do
select ...
intersect
select ...
intersect
select ...
intersect
select ...
but in jpql there is no intersect as such.
so, is there a way to mimic the behavior using some other jpql to get the same result?
(for those that are unsure about intersect) basically i need to get all the values that appear in ALL the selects...
result from select 1: 1,2,3,4
result from select 2: 1,2,5,6
result from select 3: 1,2,7,8
result from select 4: 1,2,9,0
so the result i want with intersect: 1,2
thnx a lot
p.s. there is no chance to use ANYHTING OTHER THAN JPQL :( no native queries, etc...
Can you use something like this?:
select s1.result
from select_1 as s1
where exists (
select *
from select_2 as s2
where s2.result = s1.result
)
and exists (
select *
from select_3 as s3
where s3.result = s1.result
)
and exists (
select *
from select_4 as s4
where s4.result = s1.result
);

Bubbling Up Columns in Sql

Pardon the convoluted example, but I believe there is something fundamental about sql I am missing and I'm not sure what it is. I have this crazy query...
SELECT *
FROM (
SELECT *
FROM (
SELECT #t1 := #t1 +1 AS leaderboard_entry_youngness_rank, 1 - #t1 /100 AS
leaderboard_entry_youngness_based_on_expiry, leaderboard_entry . * ,
NOW( ) - leaderboard_entry_timestamp AS leaderboard_entry_age_in_some_units,
TO_DAYS( NOW( ) ) - TO_DAYS( leaderboard_entry_timestamp )
AS leaderboard_entry_age_in_days
FROM leaderboard_entry) AS inner_temp
NATURAL JOIN leaderboard
NATURAL JOIN user
WHERE (
leaderboard_load_key = 'sk-en-adjectives-1'
OR leaderboard_load_key = '-sk-en-adjectives-1'
)
AND leaderboard_quiz_mode = '0'
ORDER BY leaderboard_entry_age_in_some_units ASC , leaderboard_entry_timestamp ASC
LIMIT 0 , 100
) AS outer_temp
ORDER BY leaderboard_entry_elapsed_time_ms ASC , leaderboard_entry_timestamp ASC
LIMIT 0 , 50
I added the second nested SELECT statement because the user_name in the user table was not being returned in the outermost query. But now the leaderboard_entry_youngness_based_on_expiry field, which is being generated based on a row index ratio, is not working correctly.
If I remove the second nested SELECT statement, the leaderboard_entry_youngness_based_on_expiry works as expected, but the user_name column is not returned.
How can I satisfy both? Why is this happening?
Thanks!
This stems from the following question:
Add a numbered list column to a returned MySQL query
In your inner SELECT statement, you do not have user.user_name, that's why username is not returned. Remove the outer query, do it like earlier but with user.user_name like this:
....
SELECT #t1 := #t1 +1 AS leaderboard_entry_youngness_rank, 1 - #t1 /100 AS
leaderboard_entry_youngness_based_on_expiry, leaderboard_entry . * ,
NOW( ) - leaderboard_entry_timestamp AS leaderboard_entry_age_in_some_units,
TO_DAYS( NOW( ) ) - TO_DAYS( leaderboard_entry_timestamp )
AS leaderboard_entry_age_in_days, user.user_name
....
Try putting a ORDER BY in the inner most query, since there currently is no ORDER BY clause, its wrong to say that "is not working correctly".
Check if you take away the outer SELECT * FROM..., see if there are duplicate user_name columns.
BTW, Since you are not using the row index columns in your query, why not just put this logic in the application itself? it will be more reliable doing so.