Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SUB-SELECT static data not filtered in joins [CORE1808] #2236

Closed
firebird-automations opened this issue Mar 28, 2008 · 11 comments
Closed

SUB-SELECT static data not filtered in joins [CORE1808] #2236

firebird-automations opened this issue Mar 28, 2008 · 11 comments

Comments

@firebird-automations
Copy link
Collaborator

Submitted by: Tim Kelly (m00bh000)

Duplicates CORE1246

-----------

CREATE TABLE TABLE_A (
ID INTEGER);

CREATE TABLE TABLE_B (
ID INTEGER);

INSERT INTO TABLE_B VALUES (2);
INSERT INTO TABLE_B VALUES (4);
INSERT INTO TABLE_B VALUES (6);

INSERT INTO TABLE_A VALUES (1);
INSERT INTO TABLE_A VALUES (2);
INSERT INTO TABLE_A VALUES (3);

----------

SELECT TABLE_A.ID, COALESCE(TABLE_B_VIEW.DID_JOIN, 'NOT JOINED') AS DID_JOIN
FROM
TABLE_A
LEFT JOIN
(SELECT ID, 'JOINED' AS DID_JOIN FROM TABLE_B) TABLE_B_VIEW ON TABLE_B_VIEW.ID = TABLE_A.ID

Returns:

ID DID_JOIN
-------------------------------------
1 JOINED
2 JOINED
3 JOINED

When it should return:

ID DID_JOIN
-------------------------------------
1 NOT JOINED
2 JOINED
3 NOT JOINED

Unless I am mistaken the above query should return a row for every item in TABLE_A with DID_JOIN returned as 'NOT_JOINED' for every item in TABLE_A which has no corresponding ID in TABLE_B and 'JOINED' for every entry in TABLE_A that does have a corresponding ID in TABLE_B.

But when you run the query all data is returned as 'JOINED', it appears that the static text 'JOINED' slips through the left-join.

I have found a work around, by including concating data with the static text and then running substring on it!

>>

SELECT TABLE_A.ID, COALESCE(TABLE_B_VIEW.DID_JOIN, 'NOT JOINED') AS DID_JOIN
FROM
TABLE_A
LEFT JOIN
(SELECT ID, SUBSTRING('JOINED' || ID FROM 1 FOR 6) AS DID_JOIN FROM TABLE_B) TABLE_B_VIEW ON TABLE_B_VIEW.ID = TABLE_A.ID

@firebird-automations
Copy link
Collaborator Author

Modified by: Tim Kelly (m00bh000)

description: SELECT TABLE_A.ID, COALESCE(TABLE_B_VIEW.DID_JOIN, 'NOT JOINED') AS DID_JOIN
FROM
TABLE_A
LEFT JOIN
(SELECT ID, 'JOINED' AS DID_JOIN FROM TABLE_B) TABLE_B_VIEW ON TABLE_B_VIEW.ID = TABLE_A.ID

Unless I am mistaken the above query should return a row for every item in TABLE_A with DID_JOIN returned as 'NOT_JOINED' for every item in TABLE_A which has a corresponding entry in TABLE_B and 'JOINED' for every entry in TABLE_A that does have a corresponding entry in TABLE_B.

But when you run the query all data is returned as 'JOINED', it appears that the static text 'JOINED' slips through the left-join.

Doing this kinda fixes it (but then you get 'JOINED' || ID, when really you just want 'JOINED'):

SELECT TABLE_A.ID, COALESCE(TABLE_B_VIEW.DID_JOIN, 'NOT JOINED') AS DID_JOIN
FROM
TABLE_A
LEFT JOIN
(SELECT ID, 'JOINED' || ID AS DID_JOIN FROM TABLE_B) TABLE_B_VIEW ON TABLE_B_VIEW.ID = TABLE_A.ID

=>

SELECT TABLE_A.ID, COALESCE(TABLE_B_VIEW.DID_JOIN, 'NOT JOINED') AS DID_JOIN
FROM
TABLE_A
LEFT JOIN
(SELECT ID, 'JOINED' AS DID_JOIN FROM TABLE_B) TABLE_B_VIEW ON TABLE_B_VIEW.ID = TABLE_A.ID

Unless I am mistaken the above query should return a row for every item in TABLE_A with DID_JOIN returned as 'NOT_JOINED' for every item in TABLE_A which has no corresponding ID in TABLE_B and 'JOINED' for every entry in TABLE_A that does have a corresponding ID in TABLE_B.

But when you run the query all data is returned as 'JOINED', it appears that the static text 'JOINED' slips through the left-join.

Doing this kinda fixes it (but then you get 'JOINED' || ID, when really you just want 'JOINED'):

SELECT TABLE_A.ID, COALESCE(TABLE_B_VIEW.DID_JOIN, 'NOT JOINED') AS DID_JOIN
FROM
TABLE_A
LEFT JOIN
(SELECT ID, 'JOINED' || ID AS DID_JOIN FROM TABLE_B) TABLE_B_VIEW ON TABLE_B_VIEW.ID = TABLE_A.ID

@firebird-automations
Copy link
Collaborator Author

Modified by: Tim Kelly (m00bh000)

description: SELECT TABLE_A.ID, COALESCE(TABLE_B_VIEW.DID_JOIN, 'NOT JOINED') AS DID_JOIN
FROM
TABLE_A
LEFT JOIN
(SELECT ID, 'JOINED' AS DID_JOIN FROM TABLE_B) TABLE_B_VIEW ON TABLE_B_VIEW.ID = TABLE_A.ID

Unless I am mistaken the above query should return a row for every item in TABLE_A with DID_JOIN returned as 'NOT_JOINED' for every item in TABLE_A which has no corresponding ID in TABLE_B and 'JOINED' for every entry in TABLE_A that does have a corresponding ID in TABLE_B.

But when you run the query all data is returned as 'JOINED', it appears that the static text 'JOINED' slips through the left-join.

Doing this kinda fixes it (but then you get 'JOINED' || ID, when really you just want 'JOINED'):

SELECT TABLE_A.ID, COALESCE(TABLE_B_VIEW.DID_JOIN, 'NOT JOINED') AS DID_JOIN
FROM
TABLE_A
LEFT JOIN
(SELECT ID, 'JOINED' || ID AS DID_JOIN FROM TABLE_B) TABLE_B_VIEW ON TABLE_B_VIEW.ID = TABLE_A.ID

=>

SELECT TABLE_A.ID, COALESCE(TABLE_B_VIEW.DID_JOIN, 'NOT JOINED') AS DID_JOIN
FROM
TABLE_A
LEFT JOIN
(SELECT ID, 'JOINED' AS DID_JOIN FROM TABLE_B) TABLE_B_VIEW ON TABLE_B_VIEW.ID = TABLE_A.ID

Unless I am mistaken the above query should return a row for every item in TABLE_A with DID_JOIN returned as 'NOT_JOINED' for every item in TABLE_A which has no corresponding ID in TABLE_B and 'JOINED' for every entry in TABLE_A that does have a corresponding ID in TABLE_B.

But when you run the query all data is returned as 'JOINED', it appears that the static text 'JOINED' slips through the left-join.

I have found a work around, by including concating data with the static text and then running substring on it!

>>

SELECT TABLE_A.ID, COALESCE(TABLE_B_VIEW.DID_JOIN, 'NOT JOINED') AS DID_JOIN
FROM
TABLE_A
LEFT JOIN
(SELECT ID, SUBSTRING('JOINED' || ID FROM 1 FOR 6) AS DID_JOIN FROM TABLE_B) TABLE_B_VIEW ON TABLE_B_VIEW.ID = TABLE_A.ID

@firebird-automations
Copy link
Collaborator Author

Commented by: @asfernandes

This seems to be the same problem of CORE1246.

CORE1246

@firebird-automations
Copy link
Collaborator Author

Modified by: Tim Kelly (m00bh000)

description: SELECT TABLE_A.ID, COALESCE(TABLE_B_VIEW.DID_JOIN, 'NOT JOINED') AS DID_JOIN
FROM
TABLE_A
LEFT JOIN
(SELECT ID, 'JOINED' AS DID_JOIN FROM TABLE_B) TABLE_B_VIEW ON TABLE_B_VIEW.ID = TABLE_A.ID

Unless I am mistaken the above query should return a row for every item in TABLE_A with DID_JOIN returned as 'NOT_JOINED' for every item in TABLE_A which has no corresponding ID in TABLE_B and 'JOINED' for every entry in TABLE_A that does have a corresponding ID in TABLE_B.

But when you run the query all data is returned as 'JOINED', it appears that the static text 'JOINED' slips through the left-join.

I have found a work around, by including concating data with the static text and then running substring on it!

>>

SELECT TABLE_A.ID, COALESCE(TABLE_B_VIEW.DID_JOIN, 'NOT JOINED') AS DID_JOIN
FROM
TABLE_A
LEFT JOIN
(SELECT ID, SUBSTRING('JOINED' || ID FROM 1 FOR 6) AS DID_JOIN FROM TABLE_B) TABLE_B_VIEW ON TABLE_B_VIEW.ID = TABLE_A.ID

=>

-----------

CREATE TABLE TABLE_A (
ID INTEGER DEFAULT 0);

CREATE TABLE TABLE_B (
ID INTEGER DEFAULT 0);

INSERT INTO TABLE_B VALUES (2);
INSERT INTO TABLE_B VALUES (4);
INSERT INTO TABLE_B VALUES (6);

INSERT INTO TABLE_A VALUES (1);
INSERT INTO TABLE_A VALUES (2);
INSERT INTO TABLE_A VALUES (3);

----------

SELECT TABLE_A.ID, COALESCE(TABLE_B_VIEW.DID_JOIN, 'NOT JOINED') AS DID_JOIN
FROM
TABLE_A
LEFT JOIN
(SELECT ID, 'JOINED' AS DID_JOIN FROM TABLE_B) TABLE_B_VIEW ON TABLE_B_VIEW.ID = TABLE_A.ID

Returns:

ID DID_JOIN
-------------------------------------
1 JOINED
2 JOINED
3 JOINED

When it should return:

ID DID_JOIN
-------------------------------------
1 NOT JOINED
2 JOINED
3 NOT JOINED

Unless I am mistaken the above query should return a row for every item in TABLE_A with DID_JOIN returned as 'NOT_JOINED' for every item in TABLE_A which has no corresponding ID in TABLE_B and 'JOINED' for every entry in TABLE_A that does have a corresponding ID in TABLE_B.

But when you run the query all data is returned as 'JOINED', it appears that the static text 'JOINED' slips through the left-join.

I have found a work around, by including concating data with the static text and then running substring on it!

>>

SELECT TABLE_A.ID, COALESCE(TABLE_B_VIEW.DID_JOIN, 'NOT JOINED') AS DID_JOIN
FROM
TABLE_A
LEFT JOIN
(SELECT ID, SUBSTRING('JOINED' || ID FROM 1 FOR 6) AS DID_JOIN FROM TABLE_B) TABLE_B_VIEW ON TABLE_B_VIEW.ID = TABLE_A.ID

@firebird-automations
Copy link
Collaborator Author

Modified by: Tim Kelly (m00bh000)

description: -----------

CREATE TABLE TABLE_A (
ID INTEGER DEFAULT 0);

CREATE TABLE TABLE_B (
ID INTEGER DEFAULT 0);

INSERT INTO TABLE_B VALUES (2);
INSERT INTO TABLE_B VALUES (4);
INSERT INTO TABLE_B VALUES (6);

INSERT INTO TABLE_A VALUES (1);
INSERT INTO TABLE_A VALUES (2);
INSERT INTO TABLE_A VALUES (3);

----------

SELECT TABLE_A.ID, COALESCE(TABLE_B_VIEW.DID_JOIN, 'NOT JOINED') AS DID_JOIN
FROM
TABLE_A
LEFT JOIN
(SELECT ID, 'JOINED' AS DID_JOIN FROM TABLE_B) TABLE_B_VIEW ON TABLE_B_VIEW.ID = TABLE_A.ID

Returns:

ID DID_JOIN
-------------------------------------
1 JOINED
2 JOINED
3 JOINED

When it should return:

ID DID_JOIN
-------------------------------------
1 NOT JOINED
2 JOINED
3 NOT JOINED

Unless I am mistaken the above query should return a row for every item in TABLE_A with DID_JOIN returned as 'NOT_JOINED' for every item in TABLE_A which has no corresponding ID in TABLE_B and 'JOINED' for every entry in TABLE_A that does have a corresponding ID in TABLE_B.

But when you run the query all data is returned as 'JOINED', it appears that the static text 'JOINED' slips through the left-join.

I have found a work around, by including concating data with the static text and then running substring on it!

>>

SELECT TABLE_A.ID, COALESCE(TABLE_B_VIEW.DID_JOIN, 'NOT JOINED') AS DID_JOIN
FROM
TABLE_A
LEFT JOIN
(SELECT ID, SUBSTRING('JOINED' || ID FROM 1 FOR 6) AS DID_JOIN FROM TABLE_B) TABLE_B_VIEW ON TABLE_B_VIEW.ID = TABLE_A.ID

=>

-----------

CREATE TABLE TABLE_A (
ID INTEGER);

CREATE TABLE TABLE_B (
ID INTEGER);

INSERT INTO TABLE_B VALUES (2);
INSERT INTO TABLE_B VALUES (4);
INSERT INTO TABLE_B VALUES (6);

INSERT INTO TABLE_A VALUES (1);
INSERT INTO TABLE_A VALUES (2);
INSERT INTO TABLE_A VALUES (3);

----------

SELECT TABLE_A.ID, COALESCE(TABLE_B_VIEW.DID_JOIN, 'NOT JOINED') AS DID_JOIN
FROM
TABLE_A
LEFT JOIN
(SELECT ID, 'JOINED' AS DID_JOIN FROM TABLE_B) TABLE_B_VIEW ON TABLE_B_VIEW.ID = TABLE_A.ID

Returns:

ID DID_JOIN
-------------------------------------
1 JOINED
2 JOINED
3 JOINED

When it should return:

ID DID_JOIN
-------------------------------------
1 NOT JOINED
2 JOINED
3 NOT JOINED

Unless I am mistaken the above query should return a row for every item in TABLE_A with DID_JOIN returned as 'NOT_JOINED' for every item in TABLE_A which has no corresponding ID in TABLE_B and 'JOINED' for every entry in TABLE_A that does have a corresponding ID in TABLE_B.

But when you run the query all data is returned as 'JOINED', it appears that the static text 'JOINED' slips through the left-join.

I have found a work around, by including concating data with the static text and then running substring on it!

>>

SELECT TABLE_A.ID, COALESCE(TABLE_B_VIEW.DID_JOIN, 'NOT JOINED') AS DID_JOIN
FROM
TABLE_A
LEFT JOIN
(SELECT ID, SUBSTRING('JOINED' || ID FROM 1 FOR 6) AS DID_JOIN FROM TABLE_B) TABLE_B_VIEW ON TABLE_B_VIEW.ID = TABLE_A.ID

@firebird-automations
Copy link
Collaborator Author

Commented by: Tim Kelly (m00bh000)

Any1 fancy taking on this one?

@firebird-automations
Copy link
Collaborator Author

Modified by: @asfernandes

Link: This issue duplicates CORE1246 [ CORE1246 ]

@firebird-automations
Copy link
Collaborator Author

Commented by: @asfernandes

Duplicates CORE1246.

@firebird-automations
Copy link
Collaborator Author

Modified by: @asfernandes

status: Open [ 1 ] => Resolved [ 5 ]

resolution: Duplicate [ 3 ]

@firebird-automations
Copy link
Collaborator Author

Modified by: @pcisar

status: Resolved [ 5 ] => Closed [ 6 ]

@firebird-automations
Copy link
Collaborator Author

Modified by: @pavel-zotov

QA Status: No test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant