I have code like this
PROC SQL;
CREATE TABLE my_table as
SELECT DISTINCT(
t1.*,
t2.*,
t3.value3 )
FROM table1 as t1
INNER JOIN table2 as t2
ON t1.value = t2.value
INNER JOIN t3.value as t3
ON t1.value2 = t3.value2
;
quit;
But SAS sees t1.* as a format. what can I do about this? Is there a way to express this without building 2 separate tables first?
Distinct
needs not the parenthes and it applies to all items listed under
Select
I suggest you to consider the use of
Coalesce
or
Coalescec ( for character)
For the duplicated variables existed at both t1 and t2. Cartisan product would return some duplicated attributes that is not preferable.
An alternative would be to separate the sql statements and it would be much clear
Proc sql;
Select distinct * from table1 as L1
Inner join
Select distinct * from table 2 as L2
On L1.value =L2.value
Inner join
...
You may have another problem with the code.
By using t1.* and t2.* you end up with two variables named value which SAS will not condone.
I'm afraid the best syntax you can use in SAS would be:
PROC SQL;
CREATE TABLE my_table as
SELECT DISTINCT(
t1.*,
t2.value2,
t3.value3 )
FROM table1 as t1
INNER JOIN table2 as t2
ON t1.value = t2.value
INNER JOIN t3.value as t3
ON t1.value2 = t3.value2
;
QUIT;
Related
Initially, I have a query like below, doing a join on 1=1. (It's simply doing a cross join, which selects all rows from the first table and all rows from the second table and shows as a cartesian product, i.e. with all possibilities.)
SELECT * FROM Table1 t1
JOIN Table2 t2 ON 1=1
Problem: Optimize this query in such a way, it will show only the records for a particular ID and if we don't have an ID or have a NULL in the ID then it will show the result same as previously(1=1). So I wrote the script below.
Declare #T2id as int;
Set #T2id = 123;
SELECT * FROM Table1 t1
JOIN Table2 t2 ON
-- left side of join on statement
CASE
WHEN #T2id Is NULL
THEN 1
ELSE
t2.Id
END
=
-- right side of join on statement
CASE
WHEN #T2id Is NULL
THEN 1
ELSE
#T2id
END
Can anyone confirm, is it good or we can have a better approach than this?
I think your way of presenting a cross-join is something I haven't seen before.
My view is it's simpler to read and understand if you just:
SELECT *
FROM Table1 t1, Table2 t2
As for the question, assuming SQL Server (you didn't tag the RDBMS, but I guess from your variable declaration) you might consider:
IF ISNULL(#T2id,1) = 1
SELECT *
FROM Table1 t1, Table2 t2;
ELSE
SELECT *
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.id = t2.id
WHERE t2.id = #T2id;
i want create a sql statemnt (in PL SQL Developer) with a join with comma seperated?
SELECT * FROM TABLE1 t1 JOIN TABLE2 t2 ON t1.tab_id, second_id = t2.tab_id, second_id;
I always get a ORA-00920 Exception. If i change it to two Rows:
t1.tab_id = t2.tab_id AND t1.second_id = t2.second_id;
Then i get rows.
Can some say me if i can use the first step with coma seperated columns?
Greetz
You need a valid condition:
SELECT *
FROM TABLE1 t1 JOIN
TABLE2 t2
ON t1.tab_id = t2.tab_id AND t1.second_id = t2.second_id;
I think Oracle will also let you do:
SELECT *
FROM TABLE1 t1 JOIN
TABLE2 t2
ON (t1.tab_id, t1.second_id) in ( (t2.tab_id, t2.second_id) );
Or even:
SELECT *
FROM TABLE1 t1 JOIN
TABLE2 t2
USING (tab_id, second_id);
This works because the JOIN keys have the same names in the two tables.
I have two tables T1 and T2.
T1 having records like A,B,C,D
T2 having records like A,B,D,E
Now out of the query should be C when we compare both tables as C is not available in T2
Please help here..
In Oracle, you can use the minus set operator:
select t1.*
from t1
minus
select t2.*
from t2;
You should be able to just use an inner join, this will return everything that both tables have in common, so:
SELECT T1.* FROM T1 INNER JOIN T2 ON T1.id = T2.id
Another way of achieving this would be:
SELECT
C1
FROM
T1
WHERE
C1 NOT IN (
SELECT
C1
FROM
T2
);
select *
from table1 t1,
table2 t2,
table3 t3
where t2.parent_id = t1.row_id
and t2.xyz is not null
and (
select count(*)
from table3
where xyz = t2.row_id
) = 0;
Will it work?
I am using the alias t2 within my subquery.
My requirement is to check is to specify condition in where clause such that there is no record present in table3 where column xyz of table3 is stored as row_id of table2.
You can use NOT EXISTS to assert that there is no row returned from the subquery. Use modern explicit join syntax instead of comma based legacy syntax. No need to join table3 outside (you were making a cross join effectively).
select *
from table1 t1
join table2 t2 on t2.parent_id = t1.row_id
where t2.xyz is not null
and not exists (
select 1
from table3
where xyz = t2.row_id
);
I'm using Oracle 10, but the best way to ask this question is with an example.
select *
from t1, t2
where t1.id = t2.id
and t1.otherID = (select max(otherID)
from t2
where id = THE ID FROM THE OUTER QUERY T1
)
I think you see where I'm trying to go with this. I need to reference t1 in the subquery to join it to the max of t2.
I need to know how to create a query like this.
"THE ID FROM THE OUTER QUERY T1" is where my confusion is.
I tried using t1.id, but did not get results.
Try the following
select t1.*, t2.*
from t1
join t2 on t1.id = t2.id
join (select id, max(otherID) as max_otherID
from t2
group by id
) a ON a.id = t1.id and a.max_otherID = t1.otherID
Using a sub-query on the join often gives better performance than using it in the where clause.