SQL - how to rename column being used on inner join - sql

I have created a view in Microsoft SQL Server Management Studio, and I now want to join it to the correct spatial information.
I have created the query below
select * from v_postal_address_view pa
INNER JOIN SPATIAL_INFO sp ON sp.ECAD_ID = pa.BUILDING_ID
This runs fine as the ECAD_ID and BUILDING_ID are linked. However, I now want to save my results as a new view but cannot because the v_postal_address_view contains ECAD_ID already, therefore forbidding the SPATIAL_INFO tables ECAD_ID to be joined as the name is not unique.
How can I rename the ECAD_ID column in spatial_info during the inner join to the view??

CREATE VIEW RESOLVE_AMBIGUOUS_NAMES AS
SELECT T1.id AS id_1,
T2.id AS id_2
FROM TABLE_1 T1
INNER
JOIN TABLE_2 T2
ON T1.fk = T2.pk

Related

Create View in Athena but "column name specified more than once"

I have created a SELECT query that joins three tables in AWS Athena - the query itself works...
Select t1.*, t2.*, t3.*
from "analytics_poc"."stg_orderitem" as t1
INNER Join "analytics_poc"."stg_orderitemtag" as t2
ON t1.orderitemid=t2.orderitemid
LEFT Join "analytics_poc"."stg_tag" as t3
ON t3.tagid=t2.tagid
However, when I try to create a VIEW from this query I get this error...
CREATE OR REPLACE VIEW "CMS_orderitem_tags"
AS
Select t1.*, t2.*, t3.*
from "analytics_poc"."stg_orderitem" as t1
INNER Join "analytics_poc"."stg_orderitemtag" as t2
ON t1.orderitemid=t2.orderitemid
LEFT Join "analytics_poc"."stg_tag" as t3
ON t3.tagid=t2.tagid
line 1:1: Column name 'orderitemid' specified more than once.
Does anyone know why this is the case?
It would appear that a column called orderitemid exists in more than one of those tables.
The SELECT command doesn't mind having multiple output columns with the same name, but CREATE VIEW doesn't allow it.
You could try USING instead, which removes the 'join' column.
Change this line:
INNER Join "analytics_poc"."stg_orderitemtag" as t2 ON t1.orderitemid=t2.orderitemid
into:
INNER Join "analytics_poc"."stg_orderitemtag" as t2 ON USING(orderitemid)

Appending and Mapping of two tables in SQL

I have two tables in the database, I want to merge and map the table 2
into table 1
by writing SQL query.
I don't know whether to apply joins or what? and then how to append the data.
note: Customer id is the common key in both tables.
A simple INNER JOIN would solve your problem --
SELECT T1.[Branch Code]
,T1.[CustomerID]
,T2.[Customer Name]
,T1.[Advised Amount]
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.CustomerID = T2.CustomerID
Use Inner join..... U can write column name in place of *
Select * from Table1 as a
inner join Table2 as b on a.CustomerId = b.CustomerId

Conditional Join if Exists

I need to join two tables together based on a three-column key stack. The problem is sometimes one of the key columns is translated and mapped differently in another table. I will attempt to example my issue using code:
select t1.TQ
from table1 t1
left join table2 t2 on t1.comp_cd = t2.comp_cd and t1.plcy_frm = t2.plcy_frm
and t1.val_cd = t2.val_cd
The columns "comp_cd" and "plcy_frm" are fine, however the problem is with val_cd. Sometimes the val_cd in table2 does not map correctly to table1 and must go through a third table, table3. Table3 structure is below:
Val_Cd Mapped_Val_Cd
A123 A564
So -> I need to join on Mapped_Val_Cd value when it exists in Table3, but join on Val_Cd from Table2 when Val_Cd does not exist in Table3.
I hope this makes sense - I have tried Case when exists syntax but cannot get that to work.
Assuming there are no duplicates in table3, you can left join it in and then choose the value that you want in the on clause:
select t1.TQ
from table1 t1 left join
table3 t3
on t1.val_cd = t3.val_cd
table2 t2
on t1.comp_cd = t2.comp_cd and
t1.plcy_frm = t2.plcy_frm and
t1.val_cd = coalesce(t3.Mapped_Val_Cd, t2.val_cd);

parameters for view in SQL server

In SQL Server 2005, I have a complex view that needs to specify additional conditions on certain relations. These conditions are not known at the time of view creation. Here's a greatly simplified version.
SELECT fields FROM table1
LEFT JOIN table2 ON ((table1.pid = table2.fid) AND (table2.condition1 = #runtimecondition));
LEFT JOIN table3 ON ....
LEFT JOIN table4 ON ....
LEFT JOIN table5 ON ....
Dynamic SQL to directly access the tables and do the joins is not an option for me because of 3rd party constraints (this is an integration and they want to have a single point of access for me code, preferably a view -- rather than grant access to a variety of tables). Can this be done with a view? Do I have to use a stored procedure? Is this a problem that could be addressed by a Table Valued Function?
You can use Inline Table-Valued function
CREATE FUNCTION dbo.Condition
(
#condition1 int,
#condition2 int,
)
RETURNS TABLE
AS
RETURN (
SELECT *
FROM table1 t
LEFT JOIN table2 t2 ON t.pid = t2.fid
AND t2.condition1 = ISNULL(#condition1, t2.condition1)
LEFT JOIN table3 t3 ON t.pid = t3.fid
AND t3.condition1 = ISNULL(#condition2, t3.condition1)
LEFT JOIN table4 ON ....
LEFT JOIN table5 ON ....
)
Demo on SQLFiddle
For improving performance use this indexes:
CREATE INDEX x ON dbo.table1(pid)
CREATE INDEX x ON dbo.table2(condition1) INCLUDE(fid, pid)
CREATE INDEX x ON dbo.table3(condition1) INCLUDE(fid, pid)
Plan Diagram(on example of the three tables)
You can expose field(s) of interest to list of produced fields:
CREATE VIEW myview AS
SELECT fields,
table2.condition1 AS condition1
FROM table1
LEFT JOIN table2 ON (table1.pid = table2.fid);
LEFT JOIN table3 ON ....
LEFT JOIN table4 ON ....
LEFT JOIN table5 ON ....
This lets VIEW users impose conditions when they use your view, something like this:
SELECT * FROM myview
WHERE condition1 = #runtimecondition
This one could be less efficient than the function table (but I don't think it will).
View defined as :
SELECT
fields
, table2.fid AS t2fid
, table2.condition1 AS t2condition1
FROM table1
LEFT JOIN table2 ON (table1.pid = table2.fid)
LEFT JOIN table3 ON ....
LEFT JOIN table4 ON ....
LEFT JOIN table5 ON ....
View called like:
SELECTT
fields
FROM dbo.MyView
WHERE (
t2fid IS NULL
OR
t2condition1 = #runtimecondition
)

sql access inner join of 4 tables

I am new to sql and I am trying to join 4 tables together but just cant get the hang of it.
I am trying to do this with an inner join but I always get an syntax error with access.
SELECT *
from kdst,aufpos
inner join( artst inner join vert on kdst.vertreter = vert.vertnr)
on aufpos.artnr = artst.artnr;
This is my code but it does not work. I dont know what to do anymore, I hope someone can help me.
Build using the query design window
Then switch to sql view
Select *
From table1 t1
Inner join table2 t2 on t1.id = t2.fkid
Inner join table3 t3 on t1.id = t3.fkid
...
This is if you want to join multiple tables to the same parent table (table1). Fkid is the column of the foreign key field that refers to the primary key of the parent table.