TSQL select all columns from join with same names - tsql

If I have a query
select * into #tmp1 from dbo.t1 inner join dbo.t2 on t1.Sender_Id=t2.Id
I get an error
Column names in each table must be unique. Column name 'Id' in table '#tmp1' is specified more than once.
How can I do the same thing without resorting to
select t1.Id as t1id, t1.col2. ... t1.col30, t2.Id as t2id, ... t2.col40 as t2col40 from ...
notation.
I just need to quickly and manualy examine several tables, so I'd like to have a quick way of examining joins.

If you have to persist the result via select * into #tmp or want to create a view, every field name has to be unique and you will have to uses aliases for fields with identical names.
A simple query does not need unique names.

Yes, columns name must unique in select statement, in your case, you have for example two (02) id, one from tbl1, and the other one from tbl2, on esolution is list them as:
Select t1.id, t2.id
From tbl1 as t1 Inner Join tbl2 as t2 on t1.id = t2.id
Hope this help.

Or, if you have columns with same name in both tables, use this:
Select t1.*, t2.* From tbl1 as t1 Inner join tbl2 as t2 On t1.id = t2.id
Regards

Related

“Not exists” across multiple joins

I am learning sql (postgres) and id like to find values that do not exist.
I have a table, table 1 with ids and i want to find those ids that are not in table 4.
I have to join between 3 tables as table 1 holds id and table 4 contact_id (not the same number)
The tables 2,3 need to be joined as that connects the ids.
So how do i do that with “not exists”?
Select t1.id, table4.contact_id
From table1 t1
Join table2 using(id)
Join table3 using(id)
Join table4 using(contact_id)
Where not exists (
Select 1
From table4
Where table4.contact_id=t1.id
);
It returns no values, but should
No error msg…
I have thinking error i assume
Your query probably returns no values because you join table4 on contact_id and then you exclude in the WHERE clause the rows which come from this join.
To find values that don't exist, you can usually use LEFT JOIN or RIGHT JOIN or FULL OUTER JOIN and then filter the rows with NULL values in the WHERE clause.
Try this :
SELECT t1.id
FROM table1 t1
LEFT JOIN table2 t2 using(id)
LEFT JOIN table3 t3 using(id)
LEFT JOIN table4 t4 using(contact_id)
WHERE t4.contact_id IS NULL

Is there a way to alias a query of the type "table.column" inside a function?

I created a CTE, the second part of the CTE contains a select with a st_contains. In this one, i've two columns of diferent tables with the same name. I want to alias one of this 'table.column' combination becouse when I do the selection at the end of the CTE outputs an error; column reference " " is ambiguous.
with
table1 as (
...
),
table2 as (
select*
from table3, table4
where st_contains (table3.atribute1, table4.atribute1)
)
select
table1.atribute1
table2.atribute1 #here i need somethig like table2.table3.atribute1
from table1
join table2 on table1.atribute2=table2.atribute2
;
I hope i explined the problem well.
Thanks!
Alias the table3 and table4 columns in your table2 CTE to resolve the ambiguity.
with
table1 as (
...
),
table2 as (
select table3.attribute1 table3atrr1, table4.attribute1 table4attr1
from table3, table4
where st_contains (table3.atribute1, table4.atribute1)
)
select
table1.atribute1
table2.table3atrr1 -- use the aliased column name
from table1
join table2 on table1.atribute2=table2.atribute2
;

comparing two tables using row id in sqlite

I want to get the data from second table where the row id is equal to first table?Can any one help me with the syntax or code ?
There are a lot of different ways to do that.
You can use join:
Select t2.*
from tab2 t2
join tab1 t1 on t1.id = t2.id
also you can use in operator
Select *
from tab2 t2
where t2.id in ( select t1.id
from table1 t1
)
or you can use exists operator
Select *
from tab2 t2
where exists
(
selec 1
from table1 t1
where t2.id = t1.id
)
As your question is tagged with iPhone IOS so this may be the query string
NSString *query = [NSString stringWithFormat:#"SELECT * FROM secondTable WHERE rowid = %d",yourRowID];
//where yourRowID is your first table id
But if you're talking about Database syntax then see this link table joining .

How to use "as" to set alias for joined tables in oracle 10

I wrote this, and it is wrong syntax, help me fix it, I want 'T' to be an alias of the result of the two inner joins.
select T.id
from table1
inner join table2 on table1.x = table2.y
inner join table3 on table3.z = table1.w as T;
You cannot use aliases to name the "entire" join, you can, however, put aliases on individual tables of the join:
select t1.id
from table1 t1
inner join table2 t2 on t1.x = t2.y
inner join table3 t3 on t3.z = t1.w
In the projection, you will have to use the alias of the table, which defines the id column you are going to select.
You can't directly name the result of a join. One option is to use a subquery:
select T.id
from (
select *
from table1
inner join table2 on table1.x = table2.y
inner join table3 on table3.z = table1.w
) T
Another option is subquery factoring:
with T as (
select *
from table1
inner join table2 on table1.x = table2.y
inner join table3 on table3.z = table1.w
)
select T.id
from T

How to write subquery in Criteria

I have a SQL like this:
Select tbl.id, tbl.name
From
(select table1.id, table1.name
from table1
inner join table2 on table1.id = table2.id
order by table2.priority
) tbl
group by table1.id
order by table1.name
What I'm trying to achieve is to first sort (order by table2.priority), and then get the record with table1.id, name with highest priority.
Note, MAX(table2.priority) doesn't work here, because table1 to table2 is one to many, and for one table1 record, table2 can have N records with the highest priority = 1, where another table1 record with highest priority = 3.
If you only need one record from the result, and they are in order such that the record you need is at the end (or beginning) of the sort, simply limit the results to one. i.e:
SELECT tbl.id, tbl.name
FROM (
SELECT table1.id, table1.name
FROM table1
INNER JOIN table2 ON table1.id = table2.id
ORDER BY table2.priority
) tbl
GROUP BY table1.id
ORDER BY table1.name
LIMIT 1;
Note that depending on the order, you could specify ASC or DESC to ensure the correct record is the one you retrieve.