join three table in oracle 11g - sql

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.

Related

SQL column added twice during INNER JOIN

I am trying to join two tables from a database; energyImport and sunAlt.
energyImport has three columns: timestamp_id, energy, duration.
sunAlt has two columns: timestamp_id, altitude
I am doing an inner join on these two tables using the SQL:
SELECT *
FROM energyImport
INNER JOIN sunAz ON sunAz.timestamp_id = energyImport.timestamp_id;
The output from this is:
timestamp_id,duration,energy,timestamp_id,altitude
1601769600,1800,81310,1601769600,0.0
1601771400,1800,78915,1601771400,0.0
1601773200,1800,78305,1601773200,0.0
The problem is that the timestamp_id column is repeated. How can I join these columns and only include the first timestamp_id?
Replace the * with
energyImport.timestamp_id,energyImport.duration, energyImport.energy,
sunAz.altitude
Either you specify the columns that you want in the results:
SELECT e.timestamp_id, e.duration, e.energy, s.altitude
FROM energyImport e INNER JOIN sunAz s
ON s.timestamp_id = e.timestamp_id;
Or, use NATURAL instead of INNER join, so that the join is based on the columns(s) with the same names of the 2 tables (if this fits your requirement), because NATURAL join returns only 1 of each pair of these columns:
SELECT *
FROM energyImport NATURAL JOIN sunAz;
See the demo.

Select and join returning duplicate data

I have some tables that can be accessed here and I would like to get a new table with EntryId from Entry table and ProtocolNumber from JudicialOrder table. For that I'm using this query:
SELECT DISTINCT ET.EntryId, JOA.ProtocolNumber FROM Entry AS ET
LEFT JOIN JudicialOrderAccount AS JOT ON JOT.AccountId = ET.OwnerAccountId
INNER JOIN JudicialOrder AS JOA ON JOA.JudicialOrderId = JOT.JudicialOrderId;
But the ProtocolNumber is duplicated, what could be wrong with my query?
As Kurt said only the combination of ET.EntryId, JOA.ProtocolNumber is unique. You will recognize it if you add an order by.
SELECT DISTINCT ET.EntryId, JOA.ProtocolNumber FROM Entry AS ET
LEFT JOIN JudicialOrderAccount AS JOT ON JOT.AccountId = ET.OwnerAccountId
INNER JOIN JudicialOrder AS JOA ON JOA.JudicialOrderId = JOT.JudicialOrderId
ORDER BY ET.EntryId, JOA.ProtocolNumber;
If you would really like to have unique protocol number you would need to group by ProtocolNumber and wrap EntryId in some string_agg function (depends on your database).
FYI: Your LEFT JOIN - INNER JOIN combination ends up being two INNER JOINs, see

Get some columns from table A and some from table B

I have two columns i need to show all the records in column [PR] and some columns from column [EM]. The below SQL statement does not return all the records from column [PR].
SELECT
[PR].[WBS1], [EM].[FirstName], [EM].[LastName], [EM].[EMail]
FROM
[VisionDemo].[dbo].[PR]
JOIN
[VisionDemo].[dbo].[EM] ON [VisionDemo].[dbo].[PR].[Principal] = [VisionDemo].[dbo].[EM].[Employee]
How do I do this ?
Use a LEFT JOIN:
SELECT [PR].[WBS1],[EM].[FirstName],[EM].[LastName], [EM].[EMail]
FROM [VisionDemo].[dbo].[PR]
LEFT JOIN [VisionDemo].[dbo].[EM]
ON [VisionDemo].[dbo].[PR].[Principal] = [VisionDemo].[dbo].[EM].[Employee]
Use LEFT JOIN. This is fundamental stuff, go check out the FAQ
SELECT [PR].[WBS1],[EM].[FirstName],[EM].[LastName], [EM].[EMail]
FROM [VisionDemo].[dbo].[PR]
LEFT JOIN [VisionDemo].[dbo].[EM] --Use a left join
ON [VisionDemo].[dbo].[PR].[Principal] = [VisionDemo].[dbo].[EM].[Employee]

Help with SQL INNER JOIN statement

I have 2 tables , one showing me customer addresses and one other table showing all the order data. i would like to query the two tables using a JOIN so that i can get a result set shwoing me all the email addresses for customers that have not ordered in the last year.
so far i have this , but my inner join is not working, if you may help:
SELECT SHH.CUST_NO,ADR.EMAIL
FROM SALES_HISTORY_HEADER SHH,ADDRESS ADR
INNER JOIN ADR ON
SHH.CUST_NO = ADR.CUST_NO
GROUP BY SHH.CUST_NO
HAVING Max(SHH.INVOICE_DATE) < '20100728'
You were mixing join styles. If you're going to use explicit joins (and you should) then you specify the second table on the JOIN rather than listing all the tables in the FROM clause.
SELECT SHH.CUST_NO,ADR.EMAIL
FROM SALES_HISTORY_HEADER SHH
INNER JOIN ADDRESS ADR
ON SHH.CUST_NO = ADR.CUST_NO
GROUP BY SHH.CUST_NO, ADR.EMAIL
HAVING Max(SHH.INVOICE_DATE) < '20100728'

SQL 2005 - The column was specified multiple times

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.....