SQL 2005 - The column was specified multiple times - sql

I am getting the following error when trying to run this query in SQL 2005:
SELECT tb.*
FROM (
SELECT *
FROM vCodesWithPEs INNER JOIN vDeriveAvailabilityFromPE
ON vCodesWithPEs.PROD_PERM = vDeriveAvailabilityFromPE.PEID
INNER JOIN PE_PDP ON vCodesWithPEs.PROD_PERM = PE_PDP.PEID
) AS tb;
Error: The column 'PEID' was specified multiple times for 'tb'.
I am new to SQL.

The problem, as mentioned, is that you are selecting PEID from two tables, the solution is to specify which PEID do you want, for example
SELECT tb.*
FROM (
SELECT tb1.PEID,tb2.col1,tb2.col2,tb3.col3 --, and so on
FROM vCodesWithPEs as tb1 INNER JOIN vDeriveAvailabilityFromPE as tb2
ON tb1.PROD_PERM = tb2.PEID
INNER JOIN PE_PDP tb3 ON tb1.PROD_PERM = tb3.PEID
) AS tb;
That aside, as Chris Lively cleverly points out in a comment the outer SELECT is totally superfluous. The following is totally equivalent to the first.
SELECT tb1.PEID,tb2.col1,tb2.col2,tb3.col3 --, and so on
FROM vCodesWithPEs as tb1 INNER JOIN vDeriveAvailabilityFromPE as tb2
ON tb1.PROD_PERM = tb2.PEID
INNER JOIN PE_PDP tb3 ON tb1.PROD_PERM = tb3.PEID
or even
SELECT *
FROM vCodesWithPEs as tb1 INNER JOIN vDeriveAvailabilityFromPE as tb2
ON tb1.PROD_PERM = tb2.PEID
INNER JOIN PE_PDP tb3 ON tb1.PROD_PERM = tb3.PEID
but please avoid using SELECT * whenever possible. It may work while you are doing interactive queries to save typing, but in production code never use it.

Looks like you have the column PEID in both tables: vDeriveAvailabilityFromPE and PE_PDP. The SELECT statement tries to select both, and gives an error about duplicate column name.

You're joining three tables, and looking at all columns in the output (*).
It looks like the tables have a common column name PEID, which you're going to have to alias as something else.
Solution: don't use * in the subquery, but explicitly select each column you wish to see, aliasing any column name that appears more than once.

Instead of using * to identify collecting all of the fields, rewrite your query to explicitly name the columns you want. That way there will be no confusion.

just give new alias name for the column that repeats,it worked for me.....

Related

SQL - Can I use a CASE Statement to left join the same table based on different criteria?

I have a stored proc that is using a LEFT JOIN to select a key from it. This proc is used by two different applications that have different criteria for when this key should be selected.
I'm getting a syntax error on this. The first statement should be used as a LEFT JOIN if the row was created 60 days in advance based on the #IsCheckDateRequired field.
The correlation name 't' is specified multiple times in a FROM clause
If the #IsCheckDateRequiredField is not necessary, I just want to left join the table on the keys.
SELECT
t.PrimaryKey
FROM dbo.WTable w
LEFT JOIN dbo.Table t
on t.PrimaryKey = w.PrimaryKey
and datediff(day, t.CreatedDate, GETUTCDATE()) > 60
and #IsCheckDateRequired = 1
LEFT JOIN dbo.Table t
on t.PrimaryKey = w.PrimaryKey
Is this possible to do in the same proc? My SELECT statement above is attempting to select t.Primary key and is complaining because I've left joined it on the same name twice.
I'm kind of new to SQL as well and am still learning best practices, so should this even be what I'm trying to do or am I way off base here?
Any advice would help for sure!
Thanks in advance!
Thanks to the comments on helping my with my approach.
It's not allowed to do the same alias on the left join. So instead of having the logic be in the LEFT JOIN Statements, I was able to put it in my select.
SELECT
CASE #IsAutoRenewalRun
when 1 then t.PrimaryKey
when 0 then t2.PrimaryKey
END
as PrimaryKey
FROM dbo.WTable w
LEFT JOIN dbo.Table t
on t.PrimaryKey = w.PrimaryKey
and datediff(day, t.CreatedDate, GETUTCDATE()) > 60
and #IsCheckDateRequired = 1
LEFT JOIN dbo.Table t2
on t2.PrimaryKey = w.PrimaryKey

SQLite - Join using Wildcards

Strange one... Im attempting to join two tables together using a wildcard. This is usually a straight forward task, yet for some reason the query returns nothing. However, when I comment the table im trying to join on and enable to the same clause within the WHERE clause I get results.
Any thoughts?
SELECT DISTINCT S.column_3
FROM TABLE1 O
INNER JOIN TABLE2 S ON S.column_1 = O.org_id
--Use of the join clause below returns nothing.
INNER JOIN TABLE2 V ON S.column_3 LIKE '%'+V.ven_name+'%'
WHERE
O.org_id = 4
--The clause returns expected values
AND S.column_3 LIKE 'DONINGTON%';
-- Column_3 = Donington Park GP
-- Ven_Name = Donington

The Inner join in my sql code is not working and how do i select only particular colums from the second table?

SELECT "KNDNR","HZDAT"
FROM "INFM_RBINPJDT_CSC1"."V_REPL_CE10010_C3" AS A
WHERE SUBSTR("HZDAT",1,4) = EXTRACT(YEAR FROM SYSDATE)
INNER JOIN "INFM_RBINPJDT_CSC1"."V_REPL_KNA1_C3" ON
"INFM_RBINPJDT_CSC1"."V_REPL_CE10010_C3"."KNDNR" = "INFM_RBINPJDT_CSC1"."V_REPL_KNA1_C3"."KUNNR"
This is my code. Here until the inner join the code is working. The inner join statement is not working. Its showing error as "Command not ended properly".
I am using this to import tables into Power BI.
And i also want to select only few columns from the 2nd table as well.
schema name:"INFM_RBINPJDT_CSC1"
table1 name:"V_REPL_CE10010_C3"
table2 name:"V_REPL_KNA1_C3"
column name:"KNDNR","KNDNR"
Here's how you should construct your query. Join first before doing any Where conditions.
Then use your table alias to join the columns B.* of your 2nd table
SELECT A."KNDNR",A."HZDAT", B."KNDNR"
FROM "INFM_RBINPJDT_CSC1"."V_REPL_CE10010_C3" AS A
INNER JOIN "INFM_RBINPJDT_CSC1"."V_REPL_KNA1_C3" AS B ON B."KNDNR" = A."KUNNR"
WHERE SUBSTR(A."HZDAT",1,4) = EXTRACT(YEAR FROM SYSDATE)
Try moving your joins before your WHERE clause and using your table alias in the ON condition:
SELECT "KNDNR","HZDAT"
FROM "INFM_RBINPJDT_CSC1"."V_REPL_CE10010_C3" AS A
INNER JOIN "INFM_RBINPJDT_CSC1"."V_REPL_KNA1_C3" AS B ON A."KNDNR" = B."KUNNR"
WHERE SUBSTR("HZDAT",1,4) = EXTRACT(YEAR FROM SYSDATE)
Your query is messed up in multiple ways. Obviously, the clauses are in the incorrect order. You should also:
use meaningful table aliases
eschew comparisons between strings and numbers
qualify the column names
So:
SELECT rc."KNDNR", "HZDAT"
FROM "INFM_RBINPJDT_CSC1"."V_REPL_CE10010_C3" rc INNER JOIN
"INFM_RBINPJDT_CSC1"."V_REPL_KNA1_C3" rk
ON rc."KNDNR" = rk."KUNNR"
WHERE SUBSTR("HZDAT", 1, 4) = TO_CHAR(SYSDATE, 'YYYY');
You should also qualify HZDAT, but I don't know what table/view that comes from.

SQL - Table Join To Compare NULL Values Where Join is NULL

I've been asked to basically write a report that displays data in two different databases and be able to see in either database if something is missing.
IE, the invoice number may exist in database1, but not in database2 and vice versa.
I've got the following query below but it only returns all the data from the second table, with NULL values for the first. I'd like to set it up to return the NULL Values in both, but I think the problem is because my join is on the values that can be NULL, so it won't return the values that exist in the first table and not the second.
Can someone step me through how to resolve an issue like this?
As far as I'm aware, I don't necessarily have any other tables to join unless I try to join more tables from each database.
Query:
Select TC.PO_Number, TC.Invoice_Date, TC.Invoice_, H.RefPoNum, H.InvoiceNum
From Table1 TC
RIGHT JOIN [SERVERNAME].[DBNAME].[TABLE2] H ON (TC.Invoice_ = H.InvoiceNum)
Where TC.Invoice_Date Between '2018-10-31' AND '2018-10-31'
AND H.Company Like 'COMPANY'
You can do what you want with a full join. Filtering is tricky with a full join, so I recommend subqueries:
select tc.PO_Number, tc.Invoice_Date, tc.Invoice_, h.RefPoNum, h.InvoiceNum
From (select tc.*
from Table1 tc
where tc.Invoice_Date Between '2018-10-31' AND '2018-10-31'
) tc full join
(select h.*
from [SERVERNAME].[DBNAME].[TABLE2] h
where h.Company Like 'COMPANY'
) h
on TC.Invoice_ = H.InvoiceNum;
Just make sure that the column you are comparing has the same data type and you can safely use this query below:
Server01 NOT IN Server02
select t1.InvoiceNumber from server01.dbo.Invoice t1
except
select t2.InvoiceNumber from server02.dbo.Invoice t2
Server02 NOT IN Server01
select t1.InvoiceNumber from server02.dbo.Invoice t1
except
select t2.InvoiceNumber from server01.dbo.Invoice t2
P.S.
While this may not be the exact query you are looking for, but this template may help.

join three table in oracle 11g

i have three table to join in select query .. this query not working
select policy_master.POLICY_REFER ,policy_master.CLIENT_NAME ,policy_master.ADRESS ,policy_master.POLICY_CLASS ,policy_master.POLICY_PRODUCT ,policy_master.EXECUTIVE_NAME ,policy_master.COMM_DATE ,
policy_master.EXPIRY_DATE ,policy_master.RENEWAL_DATE ,policy_master.GROSS ,policy_master.FED ,policy_master.FIF ,policy_master.STAMP_DUTY ,policy_master.PERMIUM ,policy_master.DESCRIPTION,
POLICY_INSURER_DETAIL.INSURER_NAME,POLICY_INSURER_DETAIL.POLICY_NUMBER,POLICY_INSURER_DETAIL.P_SHARE,POLICY_INSURER_DETAIL.G_PREMIUM,POLICY_INSURER_DETAIL.BROKER_P,POLICY_INSURER_DETAIL.LEVY,
POLICY_INSURER_DETAIL.LEVY,POLICY_SUBAGENT_DETAIL.SUBAGENT_NAME,POLICY_SUBAGENT_DETAIL.BUSSINES_SHARE,POLICY_SUBAGENT_DETAIL.COMM_P,POLICY_SUBAGENT_DETAIL.COMM_VALUE
from POLICY_MASTER INNER JOIN POLICY_INSURER_DETAIL
on policy_master.policy_refer = POLICY_INSURER_DETAIL.POLICY_REFER and
policy_master.policy_refer = POLICY_SUBAGENT_DETAIL.POLICY_REFER;
Please tell me what i should do
To simplify the answer I've removed all explicit columns and replaced them with select *.
You have only joined two tables. You are refering to policy_subagent_detail table inside a join to policy_insurer_detail (but you're not joining the subagent details table). You should join this table and specify joining conditions in order to be able to retrieve columns from it (as you did in your column list near select keyword).
I've also added table aliases to make your code shorter.
select *
from POLICY_MASTER pm
inner join POLICY_INSURER_DETAIL pid on
pm.policy_refer = pid.POLICY_REFER
inner join POLICY_SUBAGENT_DETAIL psd on -- added join
pm.policy_refer = psd.POLICY_REFER
do inner join of the third table required you missed iton the from clause . thats it .OR you can use where clause like
from table1 a,table2 b,table3 c
where a.colname= b.colname and
b.colname=c.colname.