SELECT
c.CONSOL_INVOICE,
cu.name,
cu.CUST_CODE,
c.bu_name,
cLang.name
FROM CONSL_INV c
LEFT JOIN customers cu ON c.cust_code = cu.CUST_CODE,
customers_lang cLang
WHERE
upper(cLang.NAME) LIKE ?
AND upper(cLang.LANGUAGE_CD) = ?
AND c.CUST_CODE = cLang.CUST_CODE
This Query Executes Correctly In Oracle Sql Developer but it does not execute in hibernate.
It gives the following error:
"ORA-00918: column ambiguously defined"
I know it's because of multiple columns having same name, but I have done it correctly but still don't know why it's not executing in hibernate.
As the others suggested use aliases:
select c.consol_invoice
,cu.name as name1
,cu.cust_code
,c.bu_name
,clang.name as name2
from consl_inv c
left join customers cu
on c.cust_code = cu.cust_code, customers_lang clang
where upper(clang.name) like ?
and upper(clang.language_cd) = ?
and c.cust_code = clang.cust_code
As you can see I've added aliases to the selected columns cu.name and clang.name so that the query result has two different columns.
Related
I am trying to perform an inner join on two tables by date and county ID. Whenever I run the query, I get the following error:
SQL Compilation Error: syntax error line 1 at position 0 unexpected 'INNER'
The code is as follows:
SELECT
ISO3166_1,
FIPS,
DATE,
CASES
FROM
"covid-19","public"."timeseries"
WHERE
ISO3166_1 = 'US'
INNER JOIN
"db"."population_schema"."pop_table"
ON
(("covid-19","public"."timeseries".fips = "db"."population_schema"."pop_table".fips)
AND
("covid-19","public"."timeseries".date= "db"."population_schema"."pop_table".date))
For reference, the date and FIPS data types are the same. Are there any glaringly obvious issues?
The WHERE clauses go after all JOINS:
SELECT
ISO3166_1,
FIPS,
DATE,
CASES
FROM "covid-19","public"."timeseries" AS a
INNER JOIN "db"."population_schema"."pop_table" AS b
ON a.fips = b.fips
AND a.date= b.date
WHERE
ISO3166_1 = 'US'
and if you alias those two tables, your join logic can be much more readable.
I'm currently trying to draw multiple tables through several inner-join statements. However, I am continually hit with the following error:
ORA-00904: "PART"."ITEM_CLASS": invalid identifier
I've visited several other pages here but that problem did not coincide with mine.
Here is the SQL query:
SELECT Slsrep_Number, AVG(Commission_Rate) AS AVG_Rate, MAX(Total_Commission) as MAX_Comission
FROM Sales_Rep
WHERE Sales_Rep.Slsrep_Number = Customer.Slrsrep_Number
AND Customer.C_Number = Orders.C_Number
AND Orders.Order_Number = Order_Line.Order_Number
AND Order_Line.Part_Number = Part.Part_Number
AND Part.Item_Class = 'SG';
Here are the tables being used (screenshots):
The pictures are listed in the order that I am referencing them in the SQL query.
You need to reference the tables in the FROM clause. You can't just reference them.
You should also use table aliases and proper, explicit, standard JOIN syntax.
So:
SELECT sr.Slsrep_Number,
AVG(?.Commission_Rate) AS AVG_Rate,
MAX(?Total_Commission) as MAX_Comission
FROM Sales_Rep sr JOIN
Customer c
ON sr.Slsrep_Number = c.Slrsrep_Number JOIN
Orders o
ON c.C_Number = o.C_Number JOIN
Order_Line ol
ON o.Order_Number = ol.Order_Number
Part p
ON ol.Part_Number = p.Part_Number
WHERE p.Item_Class = 'SG';
The ? is for the alias for the column with the commission.
The query now "looks" right. However, I don't think it is particularly useful. If that is the case, ask another question and provide sample data, desired results, and an explanation of what you want to accomplish.
Customer,Orders,Order_Line and Part Tables are missing in the from clause.Try This.
SELECT Slsrep_Number,
AVG(Commission_Rate) AS AVG_Rate,
MAX(Total_Commission) as MAX_Comission
FROM Sales_Rep,Customer,Orders,Order_Line,Part
WHERE Sales_Rep.Slsrep_Number = Customer.Slrsrep_Number
AND Customer.C_Number = Orders.C_Number
AND Orders.Order_Number = Order_Line.Order_Number
AND Order_Line.Part_Number = Part.Part_Number
AND Part.Item_Class = 'SG';
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')
I have 2 sql queries one of them work but the other gives error. Following query works well
select /*ordered*/ coupon_address.coupon,merchant_address.id
from merchant_address,
coupon_address,
customers c
WHERE merchant_address.id = coupon_address.merchant_address
and c.CUSTOMER_ID = 'temp1'
AND sdo_within_distance(c.cust_geo_location,merchant_address.store_geo_location,'distance = 1 unit=MILE') = 'TRUE';
But following query doesn't work and gives an error
select /*ordered*/ coupon_address.coupon,merchant_address.id
from coupon_address,
customers c
JOIN merchant_address ON merchant_address.id=coupon_address.merchant_address
WHERE c.CUSTOMER_ID = 'temp1'
AND sdo_within_distance (c.cust_geo_location,merchant_address.store_geo_location, 'distance = 1 unit=MILE') = 'TRUE';
Error is
ERROR at line 1: ORA-00904: "COUPON_ADDRESS"."MERCHANT_ADDRESS": invalid identifier
Don't ever mix explicit and implicit join syntax together! They will always lead to confusion and errors :
select /*ordered*/ coupon_address.coupon,merchant_address.id
FROM coupon_address
JOIN merchant_address
ON merchant_address.id=coupon_address.merchant_address
JOIN customers c ON c.CUSTOMER_ID = 'temp1'
WHERE sdo_within_distance (c.cust_geo_location,
merchant_address.store_geo_location,
'distance = 1 unit=MILE') = 'TRUE';
The reason it didn't work is the order that the parser evaluates the query. Probably the unknown column's table wasn't evaluated yet.
The JOIN has a higher precedence than the , in the FROM clause. ON is part of the JOIN so it only sees that which begin joined, including earlier joins.
So:
select /*ordered*/ coupon_address.coupon,merchant_address.id
from coupon_address,
customers c
JOIN merchant_address
ON merchant_address.id=coupon_address.merchant_address
is in essence:
select /*ordered*/ coupon_address.coupon,merchant_address.id
from coupon_address,
(customers c
JOIN merchant_address
ON merchant_address.id=coupon_address.merchant_address)
and coupon_address is not yet in scope.
As others have said, best to stick to one style of joins, either SQL-92 join keywords or the earlier commas in the from clause and join criteria in the where clause. I prefer the explicit join syntax.
Sagi's answer is correct. The reason your version doesn't work is because of the scoping rules around commas in the FROM clause. Oh, did I mention: Never use commas in the FROM clause. Always use explicit JOIN syntax.
Your FROM clause is evaluated as:
from coupon_address,
(customers c JOIN
merchant_address
ON merchant_address.id = coupon_address.merchant_address
)
I think this makes it obvious why you are getting the error.
I am having problems writing a query to summarize the number trips from a OD matrix table. I am still new to using databases other than MS Access, so please forgive my inexperience. SMGZ is the table with the number of zones and matrix_od is the matrix table with all the OD pairs(4109*4109) from zone to zone with the total number of trip types. I am not sure this is the best way to write this query. SO what i need to do is summarize 4 types of trips(LHDT,MHDT,HHDT,HDT_Tota) by the OD. Help is much appreciated.
SELECT
smgz.smg_zone AS "O_ID",
smgz.x AS "O_X",
smgz.y AS "O_Y",
smgz1.smg_zone AS "D_ID",
smgz1.x AS "D_X",
smgz1.y AS "D_Y",
SUM(matrix_od."LHDT") AS "LHDT_Tot",
SUM(matrix_od."MHDT") AS "MHDT_Tot",
SUM(matrix_od."HHDT") AS "HHDT_Tot",
SUM(matrix_od."TOT_HDT") AS "HDT_Tot"
FROM
public.smgz,
public.matrix_od,
public.smgz smgz1
GROUP BY
"O_ID","O_X","O_Y","D_ID","D_X","D_Y"
INNER JOIN
smgz on matrix_od.O_ID = smgz.ID
INNER JOIN
smgz1 on matrix_od.D_ID = smgz1.ID;
ERROR: syntax error at or near "INNER"
LINE 18: INNER JOIN
^
********** Error **********
ERROR: syntax error at or near "INNER"
SQL state: 42601
Character: 414
As stated in comments the two main errors are that the group by clause is in the wrong place, it should be after the joins, and that you are mixing implicit joins (multiple tables in the from clause) with explicit joins (using the join keyword). The fix is to change this part:
FROM
public.smgz,
public.matrix_od,
public.smgz smgz1
GROUP BY
"O_ID","O_X","O_Y","D_ID","D_X","D_Y"
INNER JOIN
smgz on matrix_od.O_ID = smgz.ID
INNER JOIN
smgz1 on matrix_od.D_ID = smgz1.ID;
to this:
FROM
public.matrix_od
INNER JOIN
public.smgz on matrix_od.O_ID = smgz.ID
INNER JOIN
public.smgz smgz1 on matrix_od.D_ID = smgz1.ID
GROUP BY
"O_ID","O_X","O_Y","D_ID","D_X","D_Y";
That did the trick! Thank you #a_horse_with_no_name & #Lamak.
SELECT
smgz.smg_zone AS "O_ID",
smgz.x AS "O_X",
smgz.y AS "O_Y",
smgz1.smg_zone AS "O_ID",
smgz1.x AS "D_X",
smgz1.y AS "D_Y",
SUM(matrix_od."LHDT") AS "LHDT_Tot",
SUM(matrix_od."MHDT") AS "MHDT_Tot",
SUM(matrix_od."HHDT") AS "HHDT_Tot",
SUM(matrix_od."TOT_HDT") AS "HDT_Tot"
FROM
public.matrix_od
INNER JOIN
smgz on matrix_od."O_ID" = smgz."id"
INNER JOIN
public.smgz smgz1 on matrix_od."D_ID" = smgz1."id"
GROUP BY
smgz.smg_zone,smgz.x,smgz.y,smgz1.smg_zone,smgz1.x,smgz1.y;