I want the entire query to run for each value returned by the sub-query in where clause. I am unable to figure out what i am doing wrong here. Please help?
SELECT a.*, b.*, c.*
FROM table1 a, table2 b, table3 c
WHERE a.val1 = ( select val1 from table1 )
AND a.val2 = b.val3
AND a.val4 = c.val5;
in instead of =
SELECT a.*, b.*, c.*
FROM table1 a, table2 b, table3 c
WHERE a.val1 in ( select val1 from table1 )
AND a.val2 = b.val3
AND a.val4 = c.val5;
Related
i have my table containing 4 Columns (id, val1, val2, val3).
Does anyone knows how to select rows where val3 is the same where val1 is different.
for example
row1: (id1, user1, matheos, cvn)
row2: (id2, user2, matheos, cvn)
row3: (id3, user3, Claudia, bnps)
then i return the row1 and row2.
Your explanation is not entirely clear, but the following query will find matching rows according to the criteria you specified:
select a.*, b.*
from my_table a
join my_table b on b.val3 = a.val3
and b.val2 <> a.val2
and b.id < a.id
In order to produce the rows separately, you can also do:
select *
from my_table a
where exists (
select null from my_table b where b.val3 = a.val3 and b.val2 <> a.val2
)
Based on your explanation, you can try this:
select distinct t1.* from mytable t1
JOIN mytable t2 where t1.val3 = t2.val3
and t1.val1 != t2.val1;
Demo: SQL Fiddle
I am working on a personal project.
I have the following data
tbl A
id, blah, blah
tbl B
id, id_2
tbl C
id_2, Result
I have check for the following, if id in tbl A exists in tbl B AND if Pass = 'A'.
I can write a query to check for the keys in A exists in B.
Something like,
SELECT * FROM tblA as a
WHERE NOT EXISTS (
SELECT * FROM tblB as b
WHERE a.id = b.id)
And this works fine.
I can also write something with an INNER JOIN
SELECT *
FROM tabC
INNER JOIN tblB ON tblC.id2 = tblB.id2
INNER JOIN tblA ON tblB.id = tblA.id
WHERE Result = 'A' GROUP BY id
But I can't seem to figure out how to combine both these queries together.
you can try like below
SELECT c.*
FROM tabC c
INNER JOIN tblB b ON c.id2 = b.id2
INNER JOIN tblA a ON b.id = a.id
WHERE Result = 'A'
and NOT EXISTS (
SELECT 1 FROM tblB as b1
WHERE a.id = b1.id)
Please find the below provided query which I'm able to implement in Netezza where in case there is no join on 'id' then it looks for the join on the basis of 'name'. if any of the criteria gets fulfilled then Left outer join is performed.
Select * from BigqueryTest.colors a
LEFT JOIN
BigqueryTest.color2 b
ON a.id = b.id
OR a.name = b.name ;
This functionality doesn't seem to be supported in Bigquery. I mean to say that I can provide multiple joining conditions with the help of 'AND' operator but can't use 'OR' operator which can allow me moving ahead with Join operation even if a single condition gets satisfied out of many. Any leads would be appreciated.
... is there any way around to make the code run with 'OR' condition? Because in actual scenario there are more than 100 columns ...
Below is another option - as a direction for you to explore.
#standardSQL
SELECT a.id AS aid, a.name AS aname, b.id AS bid, b.name AS bname
FROM (
SELECT
a, IF(matches = 0, STRUCT<id INT64, name STRING>(NULL, NULL), b) AS b
FROM (
SELECT
a, b,
(a.id = b.id OR a.name = b.name) AS match,
COUNTIF(a.id = b.id OR a.name = b.name)
OVER(PARTITION BY a.id, a.name) AS matches,
ROW_NUMBER() OVER(PARTITION BY a.id, a.name) AS dup
FROM `BigqueryTest.colors` AS a
CROSS JOIN `BigqueryTest.color2` AS b
)
WHERE match OR (matches = 0 AND dup = 1)
)
-- ORDER BY a.id
You can test/play with it using below dummy data
#standardSQL
WITH `BigqueryTest.colors` AS (
SELECT 1 AS id, 'a' AS name UNION ALL
SELECT 2 AS id, 'b' AS name UNION ALL
SELECT 3 AS id, 'c' AS name
),
`BigqueryTest.color2` AS (
SELECT 1 AS id, 'a' AS name UNION ALL
SELECT 11 AS id, 'a' AS name UNION ALL
SELECT 2 AS id, 'b' AS name UNION ALL
SELECT 2 AS id, 'x' AS name
)
SELECT a.id AS aid, a.name AS aname, b.id AS bid, b.name AS bname
FROM (
SELECT
a, IF(matches = 0, STRUCT<id INT64, name STRING>(NULL, NULL), b) AS b
FROM (
SELECT
a, b,
(a.id = b.id OR a.name = b.name) AS match,
COUNTIF(a.id = b.id OR a.name = b.name) OVER(PARTITION BY a.id, a.name) AS matches,
ROW_NUMBER() OVER(PARTITION BY a.id, a.name) AS dup
FROM `BigqueryTest.colors` AS a
CROSS JOIN `BigqueryTest.color2` AS b
)
WHERE match OR (matches = 0 AND dup = 1)
)
ORDER BY a.id
Below is for BigQuery Standard SQL
#standardSQL
SELECT
a.id AS aid, a.name AS aname,
b.id AS bid, b.name AS bname
FROM `BigqueryTest.colors` AS a
LEFT JOIN `BigqueryTest.color2` AS b
ON a.id = b.id
UNION DISTINCT
SELECT
a.id AS aid, a.name AS aname,
b.id AS bid, b.name AS bname
FROM `BigqueryTest.colors` AS a
LEFT JOIN `BigqueryTest.color2` AS b
ON a.name = b.name
I have a oracle database and I'm trying to query data in table1 and inner join with another table2 where one of the columns(date) is equal to the most recent date and another column in table2(built) is equal to 'yes'. This query below is not picking up the where function and can't pinpoint why
SELECT id, b, c, d
FROM table1 a
INNER JOIN table2 b on b.id = a.id
WHERE b.date =(SELECT MAX(date) FROM table2) AND b.built = 'yes'
Actual query
SELECT m_tp_str, m_tp_trn, m_tp_dte, m_tp_buy, m_tp_qtyeq, m_tp_nom, m_instr,
m_tp_p, m_tp_status2
FROM HA_PRD_DM.TP_ALL_REP a INNER JOIN HA_PRD_DM.UDF_CURR_REP b
ON a.m_udf_ref2 = b.m_nb
WHERE b.m_rep_date2 = (SELECT MAX(c.m_rep_date2) FROM HA_PRD_DM.UDF_CURR_REP c)
AND b.m_purpose = 'yes'
You can do this using analytic functions:
SELECT id, b, c, d
FROM table1 a INNER JOIN
(SELECT b.*, MAX(date) OVER (PARTITION BY b.id) as max_date
FROM table2 b
WHERE built = 'yes'
) b
ON b.id = a.id AND b.max_date = b.date;
I'm trying to write select as:
SELECT * FROM table1 a
WHERE a.d > (
SELECT b.d FROM table2 b
WHERE a.id = b.id and a.Something = 1
)
BUT if the nested select does not returns any value so SELECT does not returns anything either.
Is possible to write something like:
SELECT * FROM table1 a WHERE a.d >
(SELECT * FROM
IF EXISTS (
SELECT b.d FROM table2 b WHERE a.id = b.id and a.Something = 1
)
SELECT b.d FROM table2 b WHERE a.id = b.id and a.Something = 1 )
ELSE
SELECT '0'
)
You can use COALESCE
SELECT *
FROM table1 a
WHERE a.d > COALESCE((SELECT b.d FROM table2 b WHERE a.id = b.id and a.Something = 1), '0')
COALESCE
Evaluates the arguments in order and returns the current
value of the first expression that initially does not evaluate to
NULL.
See this link for more info.