SQL compilation error: syntax error line 6 at position 75 unexpected 'on' - snowflake-cloud-data-platform

I am working on a long query that has multiple joins, and I am getting this error numerous times, can someone please assist? I went through every single solution online, but it doesn't seem to be my case. Below is an example where I am getting this "SQL compilation error: syntax error line 6 at position 75 unexpected 'on'"
select
clmt.CLAIMANT_NATURAL_KEY,
dlexp.LOSS_EXPOSURE_NATURAL_KEY,
min(rtncall.RETURNED_CALL_DATE_TIME_KEY) MIN_RETURN_CALL_DATE_TIME_KEY
from
PNC_PSA_DB.KEDW_KSG_LOSS_CDC.D_CLAIMANT clmt
left join
PNC_PSA_DB.KEDW_KSG_LOSS_CDC.D_CLAIM_CONTACT_RETURN_CALL rtncall on clmt.CLAIM_CONTACT_KEY = rtncall.CLAIM_CONTACT_KEY 
left join
PNC_PSA_DB.KEDW_KSG_LOSS_CDC.D_LOSS_EXPOSURE dlexp on clmt.CLAIMANT_NATURAL_KEY = dlexp.CLAIMANT_NATURAL_KEY 
where
rtncall.RETURNED_CALL_STATUS_CODE = 'CONTACTED'
and dlexp.CURRENT_RECORD_IND = 'Y' 
group by 
clmt.CLAIMANT_NATURAL_KEY, dlexp.LOSS_EXPOSURE_NATURAL_KEY
Non-formatted code here:
select
clmt.CLAIMANT_NATURAL_KEY
,dlexp.LOSS_EXPOSURE_NATURAL_KEY
,min(rtncall.RETURNED_CALL_DATE_TIME_KEY) MIN_RETURN_CALL_DATE_TIME_KEY
from PNC_PSA_DB.KEDW_KSG_LOSS_CDC.D_CLAIMANT clmt
left join PNC_PSA_DB.KEDW_KSG_LOSS_CDC.D_CLAIM_CONTACT_RETURN_CALL rtncall on clmt.CLAIM_CONTACT_KEY = rtncall.CLAIM_CONTACT_KEY 
left join PNC_PSA_DB.KEDW_KSG_LOSS_CDC.D_LOSS_EXPOSURE as dlexp on clmt.CLAIMANT_NATURAL_KEY = dlexp.CLAIMANT_NATURAL_KEY 
where rtncall.RETURNED_CALL_STATUS_CODE = 'CONTACTED'
and dlexp.CURRENT_RECORD_IND = 'Y' 
group by 
clmt.CLAIMANT_NATURAL_KEY
,dlexp.LOSS_EXPOSURE_NATURAL_KEY
How to solve this?
Thanks for the help in advance.
I tried some solutions from the snowflake community but can't find why this keeps happening.

select
clmt.CLAIMANT_NATURAL_KEY
,dlexp.LOSS_EXPOSURE_NATURAL_KEY
,min(rtncall.RETURNED_CALL_DATE_TIME_KEY) MIN_RETURN_CALL_DATE_TIME_KEY
from PNC_PSA_DB.KEDW_KSG_LOSS_CDC.D_CLAIMANT clmt
left join PNC_PSA_DB.KEDW_KSG_LOSS_CDC.D_CLAIM_CONTACT_RETURN_CAL
rtncall on clmt.CLAIM_CONTACT_KEY = rtncall.CLAIM_CONTACT_KEY
left join PNC_PSA_DB.KEDW_KSG_LOSS_CDC.D_LOSS_EXPOSURE dlexp on
clmt.CLAIMANT_NATURAL_KEY = dlexp.CLAIMANT_NATURAL_KEY
where rtncall.RETURNED_CALL_STATUS_CODE = 'CONTACTED'
and dlexp.CURRENT_RECORD_IND = 'Y'
group by
clmt.CLAIMANT_NATURAL_KEY
,dlexp.LOSS_EXPOSURE_NATURAL_KEY;
This code works as is and is syntactically right.

Related

SQL query gives Incorrect syntax near ')' - use of CAST in INNER JOIN

I get the error Incorrect syntax near ')' from the following SQL query:
SELECT
"GLPOST"."ACCTID",
"GLPOST"."FISCALYR",
"GLPOST"."FISCALPERD",
"GLPOST"."SRCELEDGER",
"GLPOST"."JRNLDATE",
"GLPOST"."BATCHNBR",
"GLPOST"."ENTRYNBR",
"GLPOST"."JNLDTLDESC",
"GLPOST"."JNLDTLREF",
"GLPOST"."TRANSAMT",
"APIBC"."POSTSEQNBR",
"APIBC"."CNTBTCH"
FROM ("MHLDAT"."dbo"."GLPOST" "GLPOST"
INNER JOIN "MHLDAT"."dbo"."GLJEH" "GLJEH"
ON (("GLPOST"."DRILSRCTY"="GLJEH"."DRILSRCTY")
AND ("GLPOST"."DRILLDWNLK"="GLJEH"."DRILLDWNLK")
AND "GLPOST"."DRILAPP"="GLJEH"."DRILAPP")))
INNER JOIN "MHLDAT"."dbo"."APIBC" "APIBC"
ON "APIBC"."POSTSEQNBR" = (CAST ("SUBSTRING" (CAST ("GLPOST"."DRILLDWNLK" AS "CHAR"(18)),3,CAST ("LEFT" (CAST ("GLPOST"."DRILLDWNLK" AS "CHAR"(18)),1) AS "INT" )) AS "INT" ))
WHERE
"GLPOST"."SRCELEDGER"=N'AP' AND "GLPOST"."FISCALYR"=N'2021' AND "GLPOST"."FISCALPERD"=N'01' AND "GLJEH"."ERRBATCH"=0
Any suggestions to resolve please?
SELECT
GLPOST.ACCTID,
GLPOST.FISCALYR,
GLPOST.FISCALPERD,
GLPOST.SRCELEDGER,
GLPOST.JRNLDATE,
GLPOST.BATCHNBR,
GLPOST.ENTRYNBR,
GLPOST.JNLDTLDESC,
GLPOST.JNLDTLREF,
GLPOST.TRANSAMT,
APIBC.POSTSEQNBR,
APIBC.CNTBTCH
FROM MHLDAT.dbo.GLPOST
INNER JOIN MHLDAT.dbo.GLJEH
ON GLPOST.DRILSRCTY=GLJEH.DRILSRCTY
AND GLPOST.DRILLDWNLK=GLJEH.DRILLDWNLK
AND GLPOST.DRILAPP=GLJEH.DRILAPP
INNER JOIN MHLDAT.dbo.APIBC
ON APIBC.POSTSEQNBR = CAST(SUBSTRING(CAST(GLPOST.DRILLDWNLK AS CHAR(18)),
3,
CAST(LEFT(CAST(GLPOST.DRILLDWNLK AS CHAR(18)),
1) AS INT)
) AS INT)
WHERE
GLPOST.SRCELEDGER = N'AP'
AND GLPOST.FISCALYR = N'2021'
AND GLPOST.FISCALPERD = N'01'
AND GLJEH.ERRBATCH = 0
You are using a lot of unnecessary parenthesis and double quoting, remove them (specially on the names of the functions SUBSTRING and LEFT). Remove the aliases (they are unnecessary because you use the same name as the table).
I have aligned your query so you can visually see where starts and ends every subexpression.

Incorrect syntax near ')' in hibernate named query

I am facing an error mentioned above, while fetching a list of values in a named query.
Here is the HQL syntax for this:
select rolepermis0_.permission_name as col_0_0_ from role_permission rolepermis0_ where rolepermis0_.role_id in ()
and here is the named query syntax i am using:
#NamedQuery(name = "rolePermission.getPermissionsByRoleIds",
//query = "SELECT NEW com.bitwise.tdm.project.internal.entity.Project(p.projectId, p.projectName,p.projectDescription) FROM UserProjectEntity up LEFT JOIN up.user u LEFT JOIN up.project p WHERE up.userId = :userId")
query = "SELECT rp.permissionName FROM RolePermissionEntity rp WHERE rp.roleId IN (:roleIds)")
Can someone please point out what am i missing in this?

Multi-part identifier 1."ea.ParameterTypeID" could not be bound. 2.The multi-part identifier "ea.ParameterTypeID" could not be bound

SELECT two.WorkOrderID,
te.EquipmentID,
tm.MaterialID,
CodMaterial,
MaterialName,
EquipmentName,
GoalPriceEnergyPerUnit,
pt.ParameterTypeID
FROM T_WorkOrders two
LEFT OUTER JOIN T_Equipment te
ON te.EquipmentID = two.EquipmentID
LEFT OUTER JOIN T_Materials tm
ON tm.MaterialID = two.MaterialID
LEFT JOIN T_Parameter_Type pt
ON ea.ParameterTypeID = pt.ParameterTypeID
WHERE WorkOrderNumber = 2
AND tm.MaterialID = 417
AND te.EquipmentID = 1076
AND ea.ParameterTypeID = 4918
Just completing,what was said in comments ..
you have defined table aliases in your query like below
T_Equipment te
T_Materials tm
T_Parameter_Type pt
and finally you are referring a column with an alias that is not in any one of the above..
ea.ParameterTypeID
so refer that column using the alias ,for which the table belongs
You are not connecting your tables because you are using the wrong alias. Try looking at your tables
T_Equipment
MaterialID
T_WorkOrders
to see which one has a column that you can 'connect' to with ParameterTypeID.
I can't help you more than that as you haven't given enough information about your tables and what you really want to do. But here's a try anyway
SELECT two.WorkOrderID,
te.EquipmentID,
tm.MaterialID,
CodMaterial,
MaterialName,
EquipmentName,
GoalPriceEnergyPerUnit,
pt.ParameterTypeID
FROM T_WorkOrders two
LEFT JOIN T_Equipment te
ON te.EquipmentID = two.EquipmentID
LEFT JOIN T_Materials tm
ON tm.MaterialID = two.MaterialID
LEFT JOIN T_Parameter_Type pt
( -- this is not correct syntax
ON te.ParameterTypeID = pt.ParameterTypeID
ON tm.ParameterTypeID = pt.ParameterTypeID
ON two.ParameterTypeID = pt.ParameterTypeID
)
WHERE WorkOrderNumber = 2
AND tm.MaterialID = 417
AND te.EquipmentID = 1076
AND ea.ParameterTypeID = 4918
Code above is example of possible uses of LEFT JOIN and ON where I'm connecting different tables. Now as you can see in the comment, this will not work because the code has two ON-s extra. All I'm trying to do here is show you possible combinations that you have maybe missed.
Also, the code you have given in comments to #TheGameiswar is not correct.
SELECT two.WorkOrderID,
te.EquipmentID,
tm.MaterialID,
CodMaterial,
MaterialName,
Equipm‌​entName,
GoalPriceEne‌​rgyPerUnit,
pt.Parame‌​terTypeID
FROM T_WorkOrders two
LEFT JOIN T_Equipment te
ON te.EquipmentID = two.EquipmentID
LEFT JOIN T_Materials tm
ON tm.MaterialID = two.MaterialID
LEFT JOIN T_Parameter_Type pt
--ON is missing, maybe something like
-- pt.SomeID = two.SomeID
LEFT JOIN T_Equipment_Address ea
ON ea.ParameterTypeID=pt.ParameterTypeID
WHERE WorkOrderNumber=2
AND tm.MaterialID = 417
AND te.EquipmentID = 1076
AND pt.ParameterTypeID = 4918
It's missing ON statement as shown in the code comment. Try to fix that. Also using Microsoft SQL Server Management Studio helps a lot in writing sql code because it will warn you if you are missing something. Also, I sometimes use visual reminders for JOIN just to be sure what am I doing, often this one from codinghorror.

Why Is My Subtraction Result Incorrect?

select sum(l.coins) - sum(t.coins) as total
from luxx_getaway_2016_coins l
join thrive_rewards_redeemed t
on l.consid = t.guideid
where l.consid = 24969 and t.harvestyear = 1516
Hello all. I am attempting to grab an updated total using the query above. The problem I'm having is that the total of these sums totals out to well above what it should be. I'm unsure of what I'm doing wrong. We're using Azure SQL Database and I've used RazorSQL and SSMS 2012 to run this query with identical results. Any help is appreciated. Please feel free to ask for clarification.
A simple solution to your duplication problem:
Select
(select sum(l.coins)
from luxx_getaway_2016_coins l
where l.consid = 24969)
-
(select sum(t.coins)
from thrive_rewards_redeemed t
where t.guideid = 24969
and t.harvestyear = 1516)
For the more general case:
; With A as (select consid, sum(l.coins) as TotA
from luxx_getaway_2016_coins l
group by consid)
, B as (select guideid, sum(t.coins) as TotB
from thrive_rewards_redeemed t
where t.harvestyear = 1516
group by guideID)
Select a.consid, TotA - TotB as Total
from A
inner join B
on a.Consid = b.GuideID

Why I can't join partial tables into one in Sqlite3?

I decomposed a large table into five tables based on BCNF and I tried to join them all together to check if I lost something or not.
I used this sql statement in Sqlite3
Select
spart.Sno, spart.Sname,
cpart.Cno, CtoT.Cname,
CtoT.Tno,tpart.Tname,
scorepart.Degree
from spart, cpart, CtoT, tpart, scorepart
where
cpart.Cname = CtoT.Cname
spart.Sno = scorepart.Sno
CtoT.Tno = tpart.Tno
cpart.Cno = scorepart.Cno;
And I got Error: near "spart": syntax error
Could anybody help me out?
Try this:
Select
spart.Sno, spart.Sname,
cpart.Cno, CtoT.Cname,
CtoT.Tno,tpart.Tname,
scorepart.Degree
from spart, cpart, CtoT, tpart, scorepart
where
(cpart.Cname = CtoT.Cname)
and (spart.Sno = scorepart.Sno)
and (CtoT.Tno = tpart.Tno)
and (cpart.Cno = scorepart.Cno);
I'm not sure the parentheses are really needed. I've forgotten the syntax rules.

Resources