SAP DBTech JDBC: [260]: invalid column name - SQL for HANA (Studio) and SQL SERVER (MST) produces differents results - hana

I have this query in SQL Server and it's ok ... but in Hana Studio it throws an error
SAP DBTech JDBC: [260]: invalid column name: VT.ItemCode: line 23 col 15
Can you help me? Thanks
SELECT
VT."ItemCode" AS "ItemCode", VT."PN" AS PN,
SUM(VT."Qty") AS Qty, SUM(VT."Valor") AS Valor
FROM
(
SELECT
T1."ItemCode" AS ItemCode, T0."CardCode" AS PN,
T1."Quantity" AS Qty, T1."LineTotal" AS Valor
FROM
OINV T0
INNER JOIN
INV1 T1 ON T0."DocEntry" = T1."DocEntry"
WHERE
T0."DocDate" >= ('2018-01-01') AND T0."DocDate" <= ('2018-12-31')
UNION ALL
SELECT
T1."ItemCode" AS ItemCode, T0."CardCode" AS PN,
-1 * T1."Quantity" AS Qty, -1 * T1."LineTotal" AS Valor
FROM
ORIN T0
INNER JOIN
RIN1 T1 ON T0."DocEntry" = T1."DocEntry"
LEFT JOIN
INV1 T2 ON T1."BaseType" = T2."ObjType"
AND T1."BaseEntry" = T2."DocEntry"
AND T1."BaseLine" = T2."LineNum"
WHERE
T0."DocDate" >= ('2018-01-01') AND T0."DocDate" <= ('2018-12-31')
) AS VT
GROUP BY
ItemCode, PN

Since column ItemCode has lower case characters it needs to be referenced with double quotation marks. This includes the GROUP BY clause.
It’s correct in the list of projected columns, just needs to be done the same way at the end of the statement.

Related

Query to get the line with the latest date

SELECT T0.[ItemCode], T0.[PriceFOB], T0.[PriceAtWH], T0.[DocEntry], T0.[CardCode], T2.[DocDate], T3.[CardName] FROM IPF1 T0 LEFT JOIN OIPF T2 ON T2.[DocDate] = (SELECT MAX(T4.[DocDate]) FROM OIPF T4 WHERE T0.[DocEntry]=T4.[DocEntry]) INNER JOIN OCRD T3 ON T0.[CardCode] = T3.[CardCode] WHERE T3.[GroupCode] = '105'
I am using the following query listed above.
The issue rises where I have multiple lines of the same item listed but my goal is to get the one with the LATEST DocDate.
If someone can please help and explain what it is that I am doing wrong.
I have tried Order By, Group By but keep getting multiple errors (syntax mostly)
SELECT ItemCode, PriceAtWH, DocEntry, PriceFOB, CardCode FROM ( SELECT *, ROW_NUMBER() OVER (PARTITION BY T0.[ItemCode] ORDER BY T0.[DocEntry] DESC) AS ROWNUM FROM IPF1 T0) IPF1 WHERE ROWNUM = 1
This is the other query that I was able to come close to my solution but I need it to filter for only GroupCode = '105' from OCRD matching the CardCode
The Main goal is to filter out a Master DATA which is combined with IPF1 and OCRD(Only need to pull Group Code from this) and to filter the IPF1 with the Group Code first. Then proceed to do the Max Date or Max DocEntry ID for each item.
You could use a distinct to get one row per event even if they have different dates and use the MAX on the select
SELECT distinct T0.[ItemCode],
T0.[PriceFOB],
T0.[PriceAtWH],
T0.[DocEntry],
T0.[CardCode],
T3.[CardName],
MAX(T2.[DocDate] ) DocDate
FROM IPF1 T0
LEFT JOIN OIPF T2
ON T0.[DocEntry]=T2.DocEntry]
INNER JOIN OCRD T3
ON T0.[CardCode] = T3.[CardCode]
WHERE T3.[GroupCode] = '105'
GROUP BY T0.[ItemCode], T0.[PriceFOB], T0.[PriceAtWH], T0.[DocEntry], T0.[CardCode],T3.[CardName]
WITH T AS (SELECT T10.[DocEntry], T10.[CardCode], T10.[ItemCode], T10.[PriceFOB], T10.[PriceAtWH] FROM IPF1 T10 INNER JOIN OCRD T11 ON T10.[CardCode] = T11.[CardCode] WHERE T11.[GroupCode] = '105')
SELECT T.[DocEntry], T.[CardCode], T.[ItemCode], T.[PriceFOB], T.[PriceAtWH] FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY T.[ItemCode] ORDER BY T.[DocEntry] DESC) AS ROWNUM FROM T) T WHERE ROWNUM = 1
So I ended up solving my own problem.
Thank you Julissa for trying buy your query has an error where you are missing a "[" in front of DocEntry and also the query does not do what I want it do, multiple instances of the same item for all date still show up. Thank you for trying!

Unable to select data from another joined table

I have some tables with huge data, so I try to use that data to create a kind of a report, based on some conditions using `LEFT JOIN',
SELECT SUBSIDIARY, MAIN_ACCOUNT, AMOUNT ... FROM (
...
LEFT JOIN (SELECT MAIN_ACCOUNT, ACCOUNT_NO AS SUBSIDIARY FROM TABLE1 WHERE STATUS = 'A') K
ON MAIN_ACCOUNT = K.MAIN_ACCOUNT
LEFT JOIN (SELECT SUM(AMOUNT) AS AMOUNT, ACCOUNT_NO, FROM TABLE2 WHERE ACCOUNT_NO = K.ACCOUNT_NO GROUP_BY ACCOUNT_NO) L
ON SUBSIDIARY = L.ACCOUNT_NO --this is where i got into a problem
...
);
The problem is that I cannot access the K.ACCOUNT_NO from another join I have tried using WHERE ACCOUNT_NO = SUBSIDIARY since I select in the main SELECT. Does it mean that I cannot access previously queried data from another join?
Error I get:
ORA-00904: "SUBSIDIARY": invalid identifier
if I use k.subsidiary or k.account_no
ORA-00904: "k.SUBSIDIARY": invalid identifier
ORA-00904: "k.account_no": invalid identifier
You can not access the K.ACCOUNT_NO but you can INNER JOIN with TABLE1.
WHERE ACCOUNT_NO = K.ACCOUNT_NO
is replaced
INNER JOIN TABLE1 ON TABLE2.ACCOUNT_NO = TABLE1.ACCOUNT_NO AND TABLE1.STATUS = 'A'
I hope it is helpful.
SELECT SUBSIDIARY, MAIN_ACCOUNT, AMOUNT ... FROM (
...
LEFT JOIN (SELECT MAIN_ACCOUNT, ACCOUNT_NO AS SUBSIDIARY FROM TABLE1 WHERE STATUS = 'A') K
ON MAIN_ACCOUNT = K.MAIN_ACCOUNT
--this is where i got into a problem
LEFT JOIN (SELECT SUM(AMOUNT) AS AMOUNT, ACCOUNT_NO, FROM TABLE2 INNER JOIN TABLE1 ON TABLE2.ACCOUNT_NO = TABLE1.ACCOUNT_NO AND TABLE1.STATUS = 'A' GROUP BY ACCOUNT_NO) L
ON SUBSIDIARY = L.ACCOUNT_NO
...
);

ORA-00907: missing right parenthesis 00907. 00000 - "missing right parenthesis" *Cause: *Action: Error at Line: 44 Column: 30

I am executing the following query:
SELECT *
FROM (SELECT *
FROM (SELECT DISTINCT r.llobjid AS dataid,
r.drawingid,
r.revisionid,
r.revisionnumber AS Revision_Number,
r.revisionlabel,
r.minorrevisionlabel,
r.revisiontype,
p.project AS Project,
r.revisionstatus,
r.r1i AS SignIn_Requestor,
r.r2i AS SeedFileVersion,
rt.display_type_name AS Revision_Type,
rt.can_signin,
rs.display_status_name AS Revision_Status,
a.adntypeid,
at.NAME AS ADN_Type,
a.requestby,
a.assignbyAS Assign_By_Id,
' ' AS Assign_By,
a.assigndate,
' ' AS Assign_Date
FROM adnids a
INNER JOIN crt_revision r
ON r.drawingid = a.adnid
AND Upper(a.wholeid) LIKE '4160%'
INNER JOIN adntypes at
ON a.adntypeid = at.adntypeid
AND at.orgunitid = 21
INNER JOIN crt_project p
ON r.projectid = p.projectid
AND p.ouid = 21
LEFT OUTER JOIN crt_revision_type_map rt
ON r.revisiontype = rt.revtypeid
AND rt.ouid = 21
LEFT OUTER JOIN crt_revision_status_map rs
ON r.revisionstatus = rs.revstatusid
AND rs.ouid = 21
WHERE ( r.revisionstatus = 2 )) tbl1
INNER JOIN (SELECT DISTINCT d.dataid AS LLDataID,
Cast(d.dcomment AS NVARCHAR(4000)) AS
Title,
d.NAME AS Document_Name,
d.createdate AS Created_Date,
d.modifydate AS Modified_Date,
d.subtype,
d.versionnum,
d.permid,
d.reserved,
d.ownerid
FROM crt_revision r3
INNER JOIN adnids a3
ON r3.drawingid = a3.adnid
AND Upper(a3.wholeid) LIKE '4160%'
INNER JOIN dtree d
ON r3.llobjid = d.dataid
WHERE ( r3.revisionstatus = 2 )) tbl2
ON tbl1.dataid = tbl2.lldataid
LEFT OUTER JOIN (SELECT DISTINCT l.dataid AS asm_dataid,
l.stateid,
l.intransition,
ls.NAME AS Current_State,
ls.signin,
ls.lifecycleid,
ll.NAME AS Lifecycle
FROM crt_revision r5
INNER JOIN adnids a5
ON r5.drawingid = a5.adnid
AND Upper(a5.wholeid) LIKE '4160%'
INNER JOIN lm_lifecycles l
ON l.dataid = r5.llobjid
INNER JOIN lm_def_states ls
ON l.stateid = ls.stateid
INNER JOIN lm_def_lifecycles ll
ON ls.lifecycleid =
ll.lifecycleid
WHERE ( r5.revisionstatus = 2 )) tbl4
ON tbl1.dataid = tbl4.asm_dataid
WHERE ( EXISTS (SELECT b.dataid
FROM dtreeacl b
WHERE b.dataid = Nvl(tbl2.permid, tbl2.lldataid)
AND ( rightid IN ( -2, -1, 1000, 1001 ) )
AND see > 0) )
ORDER BY modified_date DESC) tbl100
WHERE rownum <= 25
ORACLE SQL Developer throws an error as: ORA-00907: missing right
parenthesis
It looks like everything is fine but don't understand where am I getting the error of missing right parenthesis.
It is all because of Cast(d.dcomment AS NVARCHAR(4000)). NVARCHAR is not a valid data type.
Change it to Cast(d.dcomment AS NVARCHAR2(2000))
It will work
Fiddle
In the fiddle my sample is
select Cast('ddd' AS NVARCHAR2(2000)) AS dd from dual
If you change it in to
select Cast('ddd' AS NVARCHAR(2000)) AS dd from dual
You will get the error
ORA-00907: missing right parenthesis
Cast Operator in Oracle Tutorial
Data Types in Oracle

CAST to int used with LEFT on an nvarchar field not working properly?

I'm trying to create a view where I union 4 tables. One of the fields is a document number. The only way I can pull the related document number from one of the tables is by running a LEFT on the 6 left-most characters. But when I do this and then CAST to integer I still cannot query the field. It's like it's looking past my where clause and trying to convert ALL of the characters in the field.
This is what I'm doing:
SELECT dbo.OSLP.U_RepStatus AS RepStatus, dbo.OSLP.SlpCode, dbo.OPCH.CardCode, dbo.OPCH.CardName, 'IN' AS DocTypeDesc, dbo.OPCH.DocNum,
dbo.OPCH.DocEntry, dbo.OPCH.DocDate, dbo.OPCH.NumAtCard, dbo.OPCH.DocStatus, dbo.OPCH.DocTotal, dbo.OPCH.U_CommInvNo AS ARInvNo
FROM dbo.OPCH INNER JOIN
dbo.OCRD ON dbo.OPCH.CardCode = dbo.OCRD.CardCode INNER JOIN
dbo.OSLP ON dbo.OCRD.U_SalesRepNumber = dbo.OSLP.SlpCode
WHERE (dbo.OSLP.U_RepStatus IN ('Dir', 'Ind', 'Sal')) AND (dbo.OPCH.U_CommInvNo IS NOT NULL)
UNION
SELECT OSLP_1.U_RepStatus AS RepStatus, OSLP_1.SlpCode, dbo.ORPC.CardCode, dbo.ORPC.CardName, 'CM' AS DocTypeDesc, dbo.ORPC.DocNum, dbo.ORPC.DocEntry,
dbo.ORPC.DocDate, dbo.ORPC.NumAtCard, dbo.ORPC.DocStatus, - 1 * dbo.ORPC.DocTotal AS DocTotal, dbo.ORPC.U_CommInvNo AS ARInvNo
FROM dbo.ORPC INNER JOIN
dbo.OCRD AS OCRD_1 ON dbo.ORPC.CardCode = OCRD_1.CardCode INNER JOIN
dbo.OSLP AS OSLP_1 ON OCRD_1.U_SalesRepNumber = OSLP_1.SlpCode
WHERE (OSLP_1.U_RepStatus IN ('Dir', 'Ind', 'Sal')) AND (dbo.ORPC.U_CommInvNo IS NOT NULL)
UNION
SELECT T3.U_RepStatus AS RepStatus, T3.SlpCode, T0.ContraAct AS CardCode, T2.CardName, 'JE' AS DocTypeDesc, T1.TransId AS DocNum, T1.TransId AS DocEntry,
T1.RefDate AS DocDate, T0.LineMemo AS NumAtCard, T1.BtfStatus AS DocStatus, T0.Debit AS DocTotal, CAST(LEFT(T0.LineMemo, 6) AS INT) AS ARInvNo
FROM dbo.JDT1 AS T0 INNER JOIN
dbo.OJDT AS T1 ON T0.TransId = T1.TransId INNER JOIN
dbo.OCRD AS T2 ON T0.ContraAct = T2.CardCode INNER JOIN
dbo.OSLP AS T3 ON T2.U_SalesRepNumber = T3.SlpCode
WHERE (T0.ShortName = 'CompanyName') AND (T1.TransType = 30) AND (T0.Account = '_SYS00000000624') AND (T3.U_RepStatus IN ('Dir', 'Ind', 'Sal')) AND (T0.Debit > 0)
UNION
SELECT T7.U_RepStatus AS RepStatus, T7.SlpCode, T4.ContraAct AS CardCode, T6.CardName, 'JE' AS DocTypeDesc, T5.TransId AS DocNum, T5.TransId AS DocEntry,
T5.RefDate AS DocDate, T4.LineMemo AS NumAtCard, T5.BtfStatus AS DocStatus, - (1 * T4.Credit) AS DocTotal, CAST(LEFT(T4.LineMemo, 6) AS INT) AS ARInvNo
FROM dbo.JDT1 AS T4 INNER JOIN
dbo.OJDT AS T5 ON T4.TransId = T5.TransId INNER JOIN
dbo.OCRD AS T6 ON T4.ContraAct = T6.CardCode INNER JOIN
dbo.OSLP AS T7 ON T6.U_SalesRepNumber = T7.SlpCode
WHERE (T4.ShortName = 'CompanyName') AND (T5.TransType = 30) AND (T4.Account = '_SYS00000000624') AND (T7.U_RepStatus IN ('Dir', 'Ind', 'Sal')) AND (T4.Credit > 0)
If I query this view with WHERE ARInvNo = 295696 I get this error:
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value 'A/P In' to data type int.
If I take out the bottom 2 unions from my view I can query with WHERE ARInvNo = 295696 without an issue. If I run the second 2 parts of the query, nothing in my results includes 'A/P In', although that does exist in rows in JDT1 outside of the WHERE criteria I'm using. Any idea why SQL is looking past my WHERE statement and trying to LEFT and CAST every field before it returns any data?
To clarify, this returns no results:
SELECT T0.LineMemo
FROM JDT1 T0 INNER JOIN OJDT T1 ON T0.TransId = T1.TransId INNER JOIN OCRD T2 ON T0.ContraAct = T2.CardCode INNER JOIN OSLP T3 ON T2.U_SalesRepNumber = T3.SlpCode
WHERE T0.ShortName = 'CompanyName' AND T1.TransType = 30 AND T0.Account = '_SYS00000000624' AND T3.U_RepStatus IN ('Dir', 'Ind', 'Sal') AND left(T0.LineMemo,3)='A/P In'
I see it now.
It's seeing 'A/P in' in your values. It can't CAST the '/' character to INT
MSSQL cast( [varcharColumn] to int) in SELECT gets executed before WHERE clause filters out bad values
In short you will want to do this:
select (case when isnumeric(TO.LineMemo) = 1 then cast(TO.LineMemo as int) end)

SQL Subquery error near )

My subquery gives an error: Msg 102, Level 15, State 1, Line 17 Incorrect syntax near ')'.
SELECT SalesArea, Branch, Volume
from
(select
br.SalesArea as SalesArea
,br.Branch as Branch
, sum(a.Volume) as Volume
FROM dbo.vDetail a with (nolock)
LEFT JOIN
dbo.vBranch AS br WITH (nolock)
ON a.Branch = br.Branch
group by a.Volume, br.SalesArea, br.Branch)
You are missing alias for subquery try out this.
SELECT SalesArea, Branch, Volume
from
(select
br.SalesArea as SalesArea
,br.Branch as Branch
, sum(a.Volume) as Volume
FROM dbo.vDetail a with (nolock)
LEFT JOIN
dbo.vBranch AS br WITH (nolock)
ON a.Branch = br.Branch
group by a.Volume, br.SalesArea, br.Branch) as x
Every select from subquery needs an alias. Just add an "X" in the end that will become the name of the table
NOT OK:
select * from (
select * from your_table
)
OK:
select * from (
select * from your_table
) X
You need alias name for a derived table
SELECT SalesArea, Branch, Volume
from
(select
br.SalesArea as SalesArea
,br.Branch as Branch
, sum(a.Volume) as Volume
FROM dbo.vDetail a with (nolock)
LEFT JOIN
dbo.vBranch AS br WITH (nolock)
ON a.Branch = br.Branch
group by a.Volume, br.SalesArea, br.Branch) as T