How to deal with ambiguous column reference in sql column name reference? - sql

I have some code:
CREATE OR REPLACE TEMPORARY VIEW cctv_join_1
AS
SELECT *
FROM cctv1_details AS A
LFET JOIN cctv3_details AS B ON (A.CDE_WR = B.CDE_WR) AND A.CDE_dist = B.CDE_dist
I then try
CREATE OR REPLACE TEMPORARY VIEW cctv_join_2
AS
(SELECT *
FROM cctv_join_1 AS A
LEFT JOIN cctv5_details AS B ON (A.CDE_WR = B.CDE_WR) AND (A.CDE_dist = B.CDE_dist))
but I get an error
Error in SQL statement: AnalysisException: Reference 'A.CDE_WR' is ambiguous, could be: A.CDE_WR, A.CDE_WR.; line 6 pos 4
in databricks. How can I deal with this?

This query:
SELECT *
FROM cctv1_details c1d LEFT JOIN
cctv3_details c3d
ON c1d.CDE_WR = c3d.CDE_WR AND c1d.CDE_dist = c3d.CDE_dist
is using SELECT *. The * is shorthand for all columns from both tables.
Obviously the combined columns from the two tables have duplicate column names; at least, CDE_WR and CDE_dist -- and there may be others. The general solution is to list all the columns out:
SELECT c1d.col1, c1d.col2, . . . c3d.colx, c3d.coly
FROM cctv1_details c1d LEFT JOIN
cctv3_details c3d
ON c1d.CDE_WR = c3d.CDE_WR AND c1d.CDE_dist = c3d.CDE_dist;
However, this is often shorted to:
SELECT c1d.*, c3d.colx, c3d.coly
FROM cctv1_details c1d LEFT JOIN
cctv3_details c3d
ON c1d.CDE_WR = c3d.CDE_WR AND c1d.CDE_dist = c3d.CDE_dist;
Note that I changed the table aliases to be reasonable abbreviations for the table names, making the query much clearer and easier to maintain.

Related

JOIN EXPRESSION NOT SUPPORTED with 3 tables

I'm working with MS Access, trying to join 3 tables together. But i'm getting message "JOIN EXPRESSION NOT SUPPORTED.".
Basically I want to join just 2 tables which are A_01HWeekEHCalendar and A00_Plant. A00_Plant requires 3 column to join; Plant_Product, Plant_Code, and Plant_Name. Plant_Product, Plant_Code is ok, but there is no column to match Plant_Product. so I have to add A_ProductGroup
I'm not sure if it can't be done in one Query or it's error from my SYNTAX. or there will be another way to do that without separate Queries.
I also add a picture of what I want and try to run and get error.
Thanks.
SELECT
A_01HWeekEHCalendar.RunNo_H,
A_01HWeekEHCalendar.Audit_Week,
A_01HWeekEHCalendar.CurrentYear,
A_01HWeekEHCalendar.CurrentWeek,
A_01HWeekEHCalendar.TTSMonth,
A_01HWeekEHCalendar.Audit_Plant,
A_01HWeekEHCalendar.Audit_plantname,
A_01HWeekEHCalendar.Audit_Qacode,
A_01HWeekEHCalendar.Audit_Qaname,
A_01HWeekEHCalendar.Audit_Product,
A00_Plant.HCA_StartDate
FROM
(A_01HWeekEHCalendar LEFT JOIN A_ProductGroup
ON A_01HWeekEHCalendar.Audit_Product = A_ProductGroup.R_Code)
LEFT JOIN A00_Plant
ON A_01HWeekEHCalendar.Audit_plantname = A00_Plant.Plant_Name
AND A_01HWeekEHCalendar.Audit_Plant = A00_Plant.Plant_Code
AND A_ProductGroup.R_Code = A00_Plant.Plant_Product;
You are lucky because you want only one column. You can work around this problem using a correlated subquery:
SELECT . . .,
(SELECT TOP (1) A00_Plant.HCA_StartDate
FROM A00_Plant
WHERE A_01HWeekEHCalendar.Audit_plantname = A00_Plant.Plant_Name
AND
A_01HWeekEHCalendar.Audit_Plant = A00_Plant.Plant_Code AND
A_ProductGroup.R_Code = A00_Plant.Plant_Product
) as HCA_StartDate
FROM A_01HWeekEHCalendar LEFT JOIN
A_ProductGroup
ON A_01HWeekEHCalendar.Audit_Product = A_ProductGroup.R_Code ;

Why am i getting Unknown column 'list_class.pk_class_id' in 'on clause' error?

I want to get student details from different tables with given academic year and class.
Query that i used is:
SELECT
list_acad_years.acad_year,
a.fk_stu_id,
tbl_stu_details.stu_fname,
tbl_stu_details.stu_sname,
a.fk_section_id,
b.fk_class_id,
list_class.class_name,
list_sections.section_code
FROM
tbl_stu_details,
list_class,
list_sections,
list_acad_years
INNER JOIN
tbl_stu_class AS a
ON
(
list_acad_years.pk_acad_year_id = a.fk_year_id
)
INNER JOIN
tbl_stu_class AS b
ON
(list_class.pk_class_id = b.fk_class_id)
WHERE
(
list_acad_years.acad_year = '2019'
) AND(list_class.class_name = '10')
it shows the following error:
#1054 - Unknown column 'list_class.pk_class_id' in 'on clause'
columns of my table are:
tbl_stu_class:
pk_stu_cls_id`, `fk_stu_id`, `fk_year_id`, `fk_class_id`, `fk_section_id`, `current_yr`
list_class:
`pk_class_id`, `class_name`, `class_code`, `fk_user_id`
list_sections:
pk_section_id`, `section_code`, `section_description`, `fk_user_id`
list_acad_years:
`pk_acad_year_id`, `acad_year`, `acad_year_code`, `fk_user_id`
tbl_stu_details:
`pk_stu_id`, `stu_id`, `username`, `stu_fname`, `stu_mname`, `stu_sname`
list_sections:
`pk_section_id`, `section_code`, `section_description`, `fk_user_id`
Why did it say unknown column when the column is present?
It would be great help if you can help me make this query better...
Thanks in advance.
Your problem is that you are using old-style joins. Period. To make matters worse, you are combining them with new style joins.
The names of the tables are not understood across commas. That limits the scope of the definitions.
You appear to know how to write JOINs correctly. So, just fix the FROM clause and your code should work.
Your Inner join will alway connect to the last table in from statement if you are using multiple FROM table.
I have just change the position of INNER JOIN
try this let me know if it works.
SELECT
list_acad_years.acad_year,
a.fk_stu_id,
tbl_stu_details.stu_fname,
tbl_stu_details.stu_sname,
a.fk_section_id,
b.fk_class_id,
list_class.class_name,
list_sections.section_code
FROM
tbl_stu_details,
list_class INNER JOIN
tbl_stu_class AS b
ON
(list_class.pk_class_id = b.fk_class_id),
list_sections,
list_acad_years
INNER JOIN
tbl_stu_class AS a
ON
(
list_acad_years.pk_acad_year_id = a.fk_year_id
)
WHERE
(
list_acad_years.acad_year = '2019'
) AND(list_class.class_name = '10')

How can I do a SQL join to get a value 4 tables farther from the value provided?

My title is probably not very clear, so I made a little schema to explain what I'm trying to achieve. The xxxx_uid labels are foreign keys linking two tables.
Goal: Retrieve a column from the grids table by giving a proj_uid value.
I'm not very good with SQL joins and I don't know how to build a single query that will achieve that.
Actually, I'm doing 3 queries to perform the operation:
1) This gives me a res_uid to work with:
select res_uid from results where results.proj_uid = VALUE order by res_uid asc limit 1"
2) This gives me a rec_uid to work with:
select rec_uid from receptor_results
inner join results on results.res_uid = receptor_results.res_uid
where receptor_results.res_uid = res_uid_VALUE order by rec_uid asc limit 1
3) Get the grid column I want from the grids table:
select grid_name from grids
inner join receptors on receptors.grid_uid = grids.grid_uid
where receptors.rec_uid = rec_uid_VALUE;
Is it possible to perform a single SQL that will give me the same results the 3 I'm actually doing ?
You're not limited to one JOIN in a query:
select grids.grid_name
from grids
inner join receptors
on receptors.grid_uid = grids.grid_uid
inner join receptor_results
on receptor_results.rec_uid = receptors.rec_uid
inner join results
on results.res_uid = receptor_results.res_uid
where results.proj_uid = VALUE;
select g.grid_name
from results r
join resceptor_results rr on r.res_uid = rr.res_uid
join receptors rec on rec.rec_uid = rr.rec_uid
join grids g on g.grid_uid = rec.grid_uid
where r.proj_uid = VALUE
a small note about names, typically in sql the table is named for a single item not the group. thus "result" not "results" and "receptor" not "receptors" etc. As you work with sql this will make sense and names like you have will seem strange. Also, one less character to type!

SQL DB2 Conditional Select

I'm working on a DB2 stored procedure and am having a little trouble getting the results I want. The problem with the following query is that it does not return rows from table A that don't pass the final where clause. I would like to receive all rows from table A that meet the first WHERE clause (WHERE A.GENRC_CD_TYPE = 'MDAA'). Then, add an email column from table B for each of those rows(WHERE (A.DESC) = B.MATL_PLNR_ID).
SELECT A.GENRC_CD,
A.DESC_30,
A.DOL,
A.DLU,
A.LU_LID,
B.EMAIL_ID_50
FROM GENRCCD A,
MPPLNR B
WHERE A.GENRC_CD_TYPE = 'MDAA'
AND (A.DESC_30) = B.MATL_PLNR_ID;
Any help is much appreciated, thanks!
Then what you need is a LEFT JOIN:
SELECT A.GENRC_CD,
A.DESC_30,
A.DOL,
A.DLU,
A.LU_LID,
B.EMAIL_ID_50
FROM GENRCCD A LEFT JOIN
MPPLNR B on A.DESC_30=B.MATL_PLNR_ID
WHERE A.GENRC_CD_TYPE = 'MDAA'

SQL - Query Select Joins through multiple tables

Here is the situation:
I search for Persons with ID (empr.empr_cb)
that has bill(s) to pay (transactions.montant)
this refers to transactions.compte_id
which is identical to comptes.id_compte
this refers to comptes.proprio.id
which is identical to empr.id_empr
that would give us the Person ID (empr.empr_cb)
I tried this, but I don't know what joins to set (cross Join?):
SELECT `empr`.`empr_cb`,`transactions`.`montant`
FROM `empr`,`comptes`,`transactions`
WHERE `transactions`.`montant` > `0`
AND `transactions`.`encaissement` = `0`
AND `transactions`.compte_id` = `comptes`.`id_compte`
AND `comptes`.`proprio_id` = `id_empr`
Any ideas how to put the joins?
This query is already using implicit INNER JOINs. It can be rewritten this way:
SELECT empr.empr_cb
, transactions.montant
FROM empr
JOIN comptes ON comptes.proprio_id = empr.id_empr
JOIN transactions ON transactions.compte_id = comptes.id_compte
WHERE transactions.encaissement = 0
AND transactions.montant > 0