Help with SQL INNER JOIN statement - sql

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'

Related

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

Inner Join and Left Join on 5 tables in Access using SQL

I am attempting to access data from the following tables:
OrgPlanYear
ProjOrgPlnYrJunction
DC
DCMaxEEContribLevel
DCNonDiscretionaryContribLevel
Basically, I need to inner join OrgPlanYear + DC and ProjOrgPlnYrJunction then I need to Left Join the remaining tables (tables 4 and 5) due to the fact the tables 1-3 have all the rows I need and only some have data in tables 4-5. I need several variables from each table. I also need the WHERE function to be across all fields (meaning I want all this data for a select group where projectID=919).
Please help!
I have tried many things with errors including attempting to use the Design Query side (i.e. JOIN function issues, badly formatted FROM function, etc.)! Here is an example of one excluding all variables I need:
SELECT
ProjOrgPlnYrJunction.fkeyProjectID, OrgPlanYear.OrgName, DC.PlanCode, DCNonDiscretionaryContribLevel.Age,DCNonDiscretionaryContribLevel.Service
FROM
(((OrgPlanYear INNER JOIN DC ON OrgPlanYear.OrgPlanYearID = DC.fkeyOrgPlanYearID) INNER JOIN ProjOrgPlnYrJunction ON OrgPlanYear.OrgPlanYearID = ProjOrgPlnYrJunction.fkeyOrgPlanYearID)
LEFT JOIN
(SELECT DCNonDiscretionaryContribLevel.Age AS Age, DCNonDiscretionaryContribLevel.Service AS Service FROM DCNonDiscretionaryContribLevel WHERE ProjOrgPlnYrJunction.fkeyProjectID)=919)
LEFT JOIN (
SELECT DCMaxEEContribLevel.EEContribRoth FROM EEContribRoth WHERE ProjOrgPlnYrJunction.fkeyProjectID)=919)
ORDER BY OrgPlanYear.OrgName;
Main issues with your query:
Missing ON clauses for each LEFT JOIN.
Referencing other table columns in SELECT and WHERE of a different subquery (e.g., FROM DCNonDiscretionaryContribLevel WHERE ProjOrgPlnYrJunction.fkeyProjectID).
Unmatched parentheses around subqueries and joins per Access SQL requirements.
See below adjusted SQL that now uses short table aliases. Be sure to adjust SELECT and ON clauses with appropriate columns.
SELECT p.fkeyProjectID, o.OrgName, DC.PlanCode, dcn.Age, dcn.Service, e.EEContribRoth
FROM (((OrgPlanYear o
INNER JOIN DC
ON o.OrgPlanYearID = DC.fkeyOrgPlanYearID)
INNER JOIN ProjOrgPlnYrJunction p
ON o.OrgPlanYearID = p.fkeyOrgPlanYearID)
LEFT JOIN
(SELECT Age AS Age, Service AS Service
FROM DCNonDiscretionaryContribLevel
WHERE fkeyProjectID = 919) AS dcn
ON dcn.fkeyProjectID = p.fkeyOrgPlanYearID)
LEFT JOIN
(SELECT EEContribRoth
FROM EEContribRoth
WHERE fkeyProjectID = 919) AS e
ON e.fkeyProjectID = p.fkeyProjectID
ORDER BY o.OrgName;

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.

SQL Query for Inner Join

There Is three table Master, Regular and Customer.
I'm saving ControlId in customer master for both Master and Regular. I want to get Profile from Master from the Customer Record.
By using below Query. I'm able to get MasterID from regular But I want Profile.
Query
select * from customer where refId='R000003'
(select ControlId from regular where LicenseId='R000003')
Result
Master Table
Regular Table
My Query Is..
SELECT Customer.CustomerId, Regular.LicenseId, Regular.ControlId,
Master.FullName, Master.profile
FROM Customer INNER JOIN
Regular ON Customer.RefId = Regular.LicenseId INNER JOIN
Master ON Regular.ControlId = Master.MasterId
WHERE (Customer.RefId = 'R000003')
But Its showing Regural's only I want Masters record also...
Is this what you mean? I'm not sure..
select regular.ControlId, master.profile
from regular r inner join master m ON (r.controlId = m.masterId)
where regular.LicenseId='R000003'
Posting an image of your data is not helpful. No one is going to type this in.
Paste the sample.
I am going to guess that RefId and LicenseId refer to each other. I think this is the query you want:
select c.*, m.profile
from customer c join
regular r
on c.refId = r.LicenseId join
master m
on r.controlId = m.MasterId;
I would advise you to fix your tables. Join keys in different tables should have similar names, so you know they line up. In fact, I almost always name my join keys as "Id", so this query would look more like like:
select c.*, m.profile
from customer c join
regular r
on c.CustomerId = r.CustomerId join
master m
on r.MasterId = m.MasterId;

Joining several tables and views

SELECT dbo.Monitor_Request.WorkDesc
, dbo.Monitor_Request.Request_ID
, dbo.Monitor_Request.Due_Dt
, dbo.Monitor_Request.Attempts
, dbo.Monitor_Request.Status_Ind
, dbo.Monitor_Request.Create_Dt
, dbo.Monitor_Request.Monitor_ID
, dbo.Monitor_Request.ByCustomer_ID
, dbo.Monitor_Request.ByCompanyID
, dbo.CompanyShim.Company_Name
, dbo.PostalAddressShim.HouseName
, dbo.PostalAddressShim.Street
, dbo.PostalAddressShim.Town
, dbo.PostalAddressShim.City
, dbo.PostalAddressShim.County
, dbo.PostalAddressShim.Postcode
FROM dbo.PostalAddressShim
RIGHT OUTER JOIN dbo.CompanyShim ON dbo.PostalAddressShim.Address_ID = dbo.CompanyShim.Company_Address_ID
RIGHT OUTER JOIN dbo.CUSTOMER ON dbo.PostalAddressShim.Address_ID = dbo.CUSTOMER.Address_ID
RIGHT OUTER JOIN dbo.Monitor_Request ON dbo.CUSTOMER.Customer_ID = dbo.Monitor_Request.ByCustomer_ID
AND dbo.CompanyShim.Company_ID = dbo.Monitor_Request.ByCompanyID
I have created a view to display customer details with their addresses. A customer can be an individual or a company. They are stored in in different tables. For individuals the details are stored in a table called Customer and the company details are stored in the Company table. I am trying to create a view of all the customers in the database to display their addresses. When I join the tables separately I am getting the address details but if I am joining both the tables(Customer and Company) at once I am not getting null values for address details columns.
How can I get all the address details of the customers (Individual or company) from the database
I rarely found a need to use RIGHT OUTER JOIN and every time I encounter it, it forces me to think from right to left complicating things.
If I didn't goof up to badly, following joins should be equivalent to what you have written
SQL Statement
dbo.Monitor_Request mr
LEFT OUTER JOIN dbo.Customer c ON c.Customer_ID = mr.ByCustomerID
LEFT OUTER JOIN dbo.CompanyShim cs ON cs.Company_ID = mr.ByCompanyID
LEFT OUTER JOIN dbo.PostalAddressShim pas ON pas.Address_ID = c.Address_ID
AND pas.Address_ID = cs.Company_Address_ID
Now this is something I can read and theorize about. What's immediate obvious from this statement is the AND clause in joining the adresses with customers and companies effectively negating each other and returning no adresses at all.
My guess is you should simply
replace your RIGHT JOINS with LEFT JOINS
use OR instead of AND
SQL Statement
dbo.Monitor_Request mr
LEFT OUTER JOIN dbo.Customer c ON c.Customer_ID = mr.ByCustomerID
LEFT OUTER JOIN dbo.CompanyShim cs ON cs.Company_ID = mr.ByCompanyID
LEFT OUTER JOIN dbo.PostalAddressShim pas ON pas.Address_ID = c.Address_ID
OR pas.Address_ID = cs.Company_Address_ID
At first glance, try changing your RIGHT OUTER JOINs to LEFT OUTER JOINs.