SQL INNER JOIN duplicate columns - sql

I am trying to return some columns from 2 tables which share an ID column using the following browser database query system, which reads from the tables shown on this webpage. I believe the way to do this is by using INNER JOIN (e.g. see this guide).
SELECT sami_dr2.DR2Sample.CATID,
sami_dr2.DR2Sample.Mstar,
sami_dr2.StellarKinematics.PA_STELKIN
FROM sami_dr2.DR2Sample INNER JOIN sami_dr2.StellarKinematics
ON sami_dr2.DR2Sample.CATID = sami_dr2.StellarKinematics.CATID;
However, when I run this query I get the error message:
sql: Duplicate columns are not supported. Try using an alias for those columns within the SELECT clause e.g., SELECT t1.CATAID, t2.CATAID becomes SELECT t1.CATAID as t1_CATAID, t2.CATAID as t2_CATAID
But as far as I'm aware the whole point of using the INNER JOIN is to remove duplications as I'm not returning sami_dr2.StellarKinematics.CATID in my output table, only sami_dr2.DR2Sample.CATID.
I've also found that using SELECT sami_dr2.DR2Sample.CATID as ID_1, sami_dr2.StellarKinematics.CATID as ID_2 in the selction doesn't fix the problem either.
Any help on this would be greatly appreciated!

Related

Does Excel as something similar to SQL "Except"?

I am able to perform Union of two tables in MS Query of excel but its throwing error for "Except".
Can someone please let me know is there anyway to use "Except" of two tables in Excel.
SELECT * FROM [Book1query] tbl2
EXCEPT
SELECT * FROM [Merge1] tbl1
EXCEPT returns only rows, which are not available in the second SELECT statement. In other words, a join type of left anti.
In Power Query you can do that with a Merge of two tables, using the Join Kind option for Left Anti.
For a detailed description of all the Join Kind options, visit https://radacad.com/choose-the-right-merge-join-type-in-power-bi

How to use Except clause in Bigquery?

I am trying to use the existing Except clause in Bigquery. Please find my query below
select * EXCEPT (b.hosp_id, b.person_id,c.hosp_id) from
person a
inner join hospital b
on a.hosp_id= b.hosp_id
inner join reading c
on a.hosp_id= c.hosp_id
As you can see I am using 3 tables. All the 3 tables have the hosp_id column, so I would like to remove duplicate columns which are b.hosp_id and c.hosp_id. Simlarly, I would like to remove b.person_id column as well.
When I execute the above query, I get the syntax error as shown below
Syntax error: Expected ")" or "," but got "." at [9:19]
Please note that all the columns that I am using in Except clause is present in the tables used. Additional info is all the tables used are temp tables created using with clause. When I do the same manually by selecting column of interest, it works fine. But I have several columns and can't do this manually.
Can you help? I am trying to learn Bigquery. Your inputs would help
I use the EXCEPT on a per-table basis:
select p.* EXCEPT (hosp_id, person_id),
h.*,
r.* EXCEPT (hosp_id)
from person p inner join
hospital h
on p.hosp_id = h.hosp_id inner join
reading r
on p.hosp_id = r.hosp_id;
Note that this also uses meaningful abbreviations for table aliases, which makes the query much simpler to understand.
In your case, I don't think you need EXCEPT at all if you use the USING clause.
Try this instead:
select * EXCEPT (person_id) from
person a
inner join hospital b
using (hosp_id)
inner join reading c
using (hosp_id)
You can only put column names (not paths) in the EXCEPT list, and you can simply avoid projecting the duplicate columns with USING instead of ON.

Insert Into where not exists from specific category

I have a table that contains several repair categories, and items that are associated with each repair category. I am trying to insert all the standard items from a specific repair category that don't already exist into a Details table.
TblEstimateDetails is a join table for an Estimate Table and StandardItem Table. And TblCategoryItems is a join table for the Repair Categories and their respective Standard Items.
For example in the attached image, Left side are all the Standard Items in a Repair Category, and Right side are all the Standard Items that are already in the EstimateDetails table.
Standard Items All vs Already Included
I need to be able to insert the 6 missing GUIDS from the left, and into the table on the right, and only for a specific estimate GID.
This is being used in an Access VBA script, which I will translate into the appropriate code once I get the sql syntax correct.
Thank you!
INSERT INTO TblEstimateDetails(estimate_GID, standard_item_GID)
SELECT
'55DEEE29-7B79-4830-909C-E59E831F4297' AS estimate_GID
, standard_item_GID
FROM TblCategoryItems
WHERE repair_category_GID = '32A8AE6D-A512-4868-8E1A-EF0357AB100E'
AND NOT EXISTS
(SELECT standard_item_GID
FROM TblEstimateDetails
WHERE estimate_GID = '55DEEE29-7B79-4830-909C-E59E831F4297');
Some things to try: 1) simplify to a select query to see if it selects the right records, 2) use a NOT IN statement instead of NOT EXISTS. There's no reason NOT EXISTS shouldn't work, I'd just try a different way if it isn't working.
SELECT '55DEEE29-7B79-4830-909C-E59E831F4297' AS estimate_GID,
standard_item_GID
FROM TblCategoryItems
WHERE repair_category_GID = '32A8AE6D-A512-4868-8E1A-EF0357AB100E'
AND standard_item_GID NOT IN
(SELECT standard_item_GID FROM TblEstimateDetails
WHERE estimate_GID = '55DEEE29-7B79-4830-909C-E59E831F4297');
Got it figured out. Access needs the subquery to be correlated to main query to work. So I set the WHERE clause in the subquery to equal the matching column in the main query. And I had to join the Estimates table so that it picked only the items in a specific estimate.
SELECT
'06A2E0A9-9AE5-4073-A856-1CCE6D9C48BB' AS estimate_GID
, CI.standard_item_GID
FROM TblCategoryItems CI
INNER JOIN TblEstimates E ON CI.repair_category_GID=E.repair_category_GID
WHERE E.repair_category_GID = '15238097-305E-4456-B86F-3787C9B8219B'
AND NOT EXISTS
(SELECT ED.standard_item_GID
FROM TblEstimateDetails ED
WHERE E.estimate_GID=ED.estimate_GID
);

Getting duplicates even with Select Distinct

I'm trying to write a query in which I should get the company information; however, I should be getting only 2 record and I'm getting 6 records.
Below is my query.
SELECT distinct a.FOLIO
,a.MAIN_ADDRESS1
,a.MAIN_ADDRESS2
,b.COMPANY_NAME
,b.FIRST_NAME
,b.LAST_NAME
,a.OPEN
,a.CLOSE
,c.CC
,c.CNAME
FROM vw_CODE_CASE AS a
INNER JOIN vw_CODE_CASE_VIOLATOR_CONTACTS AS b ON b.CMCODECASEID=a.CMCODECASEID
INNER JOIN vw_CODE_CASE_WORK_FLOW AS c ON c.CMCODECASEID=a.CMCODECASEID
Is it possible that is because of the amount of inner joins?
Thank you for your help.
Without sample data it would be difficult to identify that due to which column the distinct data are not coming from your query.
But you can do one thing to identify the cause, You can try remove one by one column and check the data from your query. At the point when you get your expected data, the last removed column will be the cause behind your problem.
Hope this helps.

Duplicate results on inner join

I've written the below query but I'm getting multiple duplicate rows in the results, please can anyone see where I'm going wrong?
use Customers
select customer_details.Customer_ID,
customer_details.customer_name,
metering_point_details.MPAN_ID,
Agents.DA_DC_Charge
from Customer_Details
left join Metering_Point_Details
on customer_details.customer_id = Metering_Point_Details.Customer_ID
left join agents
on customer_details.Customer_ID = agents.customer_id
order by customer_id
It doesn't really matter, but you're not using an INNER JOIN. Regardless, your unexpected rows indicate that your JOIN criteria is not specific enough to return your expected output. You can use SELECT DISTINCT if your results are fully duplicative, and if you'd like to see why you're getting those duplicates you can just use SELECT * to see the full detail between the multiple rows that are returned using your JOIN criteria, which should help you either make your criteria more specific or show you that you've got duplicated records in one of the tables you're using in your JOIN.
With sample data we can dissect the problem more, but odds are you won't need it once you see why the rows are duplicated.