SQL Join between two tables excluding some fields - sql

I have two tables Customer and Beneficiary, the relation between them is ManyToMany,
the generated table customers_beneficiaries contains the Id of Beneficiary and the Id of Customer
i want to get the list of customers with a given beneficiary_id
SELECT * from customer c
Full OUTER JOIN customers_beneficiaries cb
ON c.id= cb.customer_id
WHERE cb.beneficiary_id=8;
But the result iam getting contains the two fields of customers_beneficiaries table (customer_id && beneficiary_id)
How can i exclude them from the result
Thank you .

Try this:(In case you can change id column name in customer table to customer_id)
SELECT c.* from customer c
Full OUTER JOIN customers_beneficiaries cb
USING(customer_id)
WHERE cb.beneficiary_id=8;
USING Clause is like ON Clause which takes list of columns on which joining of table has to be done but those columns have to exist in both tables. The columns used in join operation appears only once in output.

Related

SQLite - How to select records from one table that are not in another table

I have a database with 3 tables; tblCustomers, tblBookings, tblFlights.
I want to find the customer's last name (LName), from the Customers table where the customers do not appear in the bookings table. It should return just three names, but it returns the names 10 times each. There are 10 records in the bookings table, so I think the command is returning the correct names, but not once...
I have tried:
SELECT tblCustomers.LName
FROM tblCustomers, tblBookings
WHERE tblCustomers.CustID
NOT IN (SELECT CustID FROM tblBookings)
How do I return just one instance of the name, not the name repeated 10 times?
You are doing a CROSS JOIN of the 2 tables.
Use only NOT IN:
SELECT LName
FROM tblCustomers
WHERE CustID NOT IN (SELECT CustID FROM tblBookings)
The (implicit) cross join on The bookings table in the outer query makes no sense - and it multiplies the customer rows.
Also, I would recommend not exists for filtering instead of not in: it usually performs better - with the right index in place, and it is null-safe:
SELECT c.LName
FROM tblCustomers c
WHERE NOT EXISTS (SELECT 1 FROM tblBookings b WHERE b.CustID = c.CustID)
For performance, make sure to have an index on tblBookings(CustID) - if you have a proper foreign key declared, it should already be there.

SQL Copy data in joined tables

I have created a query in SQL to join 3 tables :
1st table contains contact data,
2nd table contains link data,
3rd table contains sales info.
The 2nd table is like an index table to join the sales data (3rd table) to the contact data (1st table).
I have managed to join the 3 tables together with the following which only lists out contacts that have sales info associated:-
USE wce_site
SELECT
c.CONTACT,c.POSTALCODE,c.UNIQUEID,l.LEntityID,l.LETableName,l.LUniqueID,s.Area,s.POSTCODE
FROM
dbo.wce_contact AS c
INNER JOIN dbo.wce_linkto AS l
ON c.UNIQUEID=l.LEntityID
INNER JOIN dbo.wce_sales AS s
ON s.UNIQUEID=l.LUniqueID
Now I have the desired results I don't know what to do next to copy c.POSTALCODE to s.POSTCODE which are in 2 different tables.
In SQL Server, you can use join with update, so I think the following does what you want:
UPDATE s
SET POSTCODE = c.POSTCODE
FROM dbo.wce_contact AS c INNER JOIN
dbo.wce_linkto AS l
ON c.UNIQUEID = l.LEntityID INNER JOIN
dbo.wce_sales AS s
ON s.UNIQUEID = l.LUniqueID;

SQL: Join 2 tables and return multiple rows from second table based on key of first table

I have one table 'Customers', with a key of customerid.
There is another table PaymentTotals which also has a customerid column. This table stores amounts paid by a customer (PaymentAmount) in a given week (weeknumber field). This implies that in the PaymentTotals table there may be several rows for any one customerid, the difference being the weeknumber for any of these rows.
I am trying to build a query in MSSQL that joins the two tables and will return for a given customerid the PaymentAmount for each different weeknumber.
It is not clear to me how to build this query. Any advice? Thanks.
SELECT *
FROM Customers C INNER JOIN PaymentTotals PT
ON C.customerid = PT.customerid
If you have multiple Payments made by one customer in a given week and want to get total by week you could do something like ....
SELECT C.customerid
,PT.WeekNumber
,SUM(PT.Payment_Column) AS TotalPayment
FROM Customers C INNER JOIN PaymentTotals PT
ON C.customerid = PT.customerid
GROUP BY C.customerid, PT.WeekNumber

Suggestion needed for this Hive Query

I have a select statement with 5 ID columns. I need to lookup and select the corresponding customer names from a Customer master table that stores Ids/names and come up with a Customer report. The tables columns are as below:
origCustomerID,Tier1PartnerID,Tier2PartnerID,DistributorId,EndCustomerID,productId,OrderTotal,OrderDate
The first 5 columns are ID columns that match CustID column in the Customers table. Note that NOT all of these columns will contain a value for a given record at all times, i.e. they could be null at times. Given the current constraints in hiveQL, I can only think of the following way, but this takes up a lot of time and is not the best possible way. Could you please suggest any improvements to this?
Select origCustomerID,a.name,Tier1PartnerID,b.name,Tier2PartnerID,
c.name,DistributorId,d.name,EndCustomerID,e.name,productId,OrderTotal,OrderDate
From Orders O
LEFT OUTER JOIN customers a on o.origCustomerID = a.custid
LEFT OUTER JOIN customers b on o.Tier1PartnerID = a.custid
LEFT OUTER JOIN customers c on o.Tier2PartnerID = a.custid
LEFT OUTER JOIN customers d on o.DistributorId = a.custid
LEFT OUTER JOIN customers e on o.EndCustomerID = a.custid
If the id values are always either customer ids or NULL (i.e. in the case they are not NULL you are sure they are customer ids and not something else) and each record in the Orders table matches at most one customer (i.e. every record has at most one id in those five columns; or possible the same id several times), you could perhaps use COALESCE in your matching expression.
I can't test this at the moment, but this should join the records using the first non-NULL id from the Orders table.
SELECT [stuff]
FROM Orders O
LEFT OUTER JOIN customers a
ON COALESCE(o.origCustomerID,
o.Tier1PartnerID,
o.Tier2PartnerID,
o.DistributorId,
o.EndCustomerID) = a.custid
Hope that helps.

MySQL Join statement

I know how to get a single record from multiple tables using Join. How do you get multiple records?
E.G
Table: categories
id
name
description
Table: some_table
id
name
content
category_id
How would I extend the basic query below to pull all records from within all categories?
SELECT c.id, c.name as category_name FROM categories AS c
The exact join depends on your needs, but the following will show all data from category and some_table where there is at least one row in some_table that matches the value in category. Empty categories will not be shown; you can use a LEFT JOIN instead if you want to show empty category records with NULL values for the records that would otherwise come from the some_table table.
SELECT *
FROM categories c
INNER JOIN some_table st ON (c.id = st.category_id);