Syntax error in SAP Hana when changing to a variable input [%0] SAP B1 v9.3 using Query Manager - hana

--Here is the query. when i make T0."ItemCode" = 'HN 210' in the query (all places) it -----works fine. When I change the where segment to Where T0."ItemCode" = [%0] is when I get --the error
Select a.*,b."OldPrice" from (
Select 'm' as "type", T0."ItemCode" , T2."ItemName" , T7."ListName" , T0."Price" as
"NewPrice" ,
T2."UpdateDate" , '9999999' as "code" , T5."U_NAME" , T2."U_lastpriceupdate",
T2."CreateDate"
FROM
OITM T2
Inner Join ITM1 T0 on T0."ItemCode" = T2."ItemCode"
INNER JOIN OUSR T5 on T5."INTERNAL_K" = T2."UserSign2"
Inner join OPLN T7 on T0."PriceList" = T7."ListNum"
Where T0."ItemCode" = [%0] and
T0."PriceList" IN ('5')
UNION ALL
Select 'a' as "type", T0."ItemCode" , T2."ItemName", T7."ListName" , T1."Price" as
"NewPrice",
T4."UpdateDate" , T1."LogInstanc" as "code" , T5."U_NAME" , T2."U_lastpriceupdate",
T2."CreateDate"
FROM
OITM T2
Inner Join ITM1 T0 on T0."ItemCode" = T2."ItemCode"
inner join AIT1 T1 on T0."ItemCode" = T1."ItemCode" and T0."PriceList" =
T1."PriceList"
INNER JOIN AITM T4 on T4."ItemCode" = T1."ItemCode" and T1."LogInstanc" =
T4."LogInstanc"
INNER JOIN OUSR T5 on T5."INTERNAL_K" = T4."UserSign2"
Inner join OPLN T7 on T0."PriceList" = T7."ListNum"
Where T0."ItemCode" = [%0] and
T0."PriceList" IN ('5')
order by "type" desc, "code" desc) as a
LEFT JOIN
(Select 'a' as "type", T0."ItemCode" , T2."ItemName", T7."ListName" , T1."Price" as
"OldPrice" ,
T4."UpdateDate" , '9999999' as "code" , T5."U_NAME" , T2."U_lastpriceupdate",
T2."CreateDate"
FROM
OITM T2
Inner Join ITM1 T0 on T0."ItemCode" = T2."ItemCode"
inner join AIT1 T1 on T0."ItemCode" = T1."ItemCode" and T0."PriceList" =
T1."PriceList"
INNER JOIN AITM T4 on T4."ItemCode" = T1."ItemCode" and T1."LogInstanc" =
T4."LogInstanc"
INNER JOIN OUSR T5 on T5."INTERNAL_K" = T4."UserSign2"
Inner join OPLN T7 on T0."PriceList" = T7."ListNum"
Where T0."ItemCode" = [%0] and
T0."PriceList" IN ('5') and T1."LogInstanc" IN (
Select max(T1."LogInstanc") as "t"
FROM
OITM T2
Inner Join ITM1 T0 on T0."ItemCode" = T2."ItemCode"
inner join AIT1 T1 on T0."ItemCode" = T1."ItemCode" and T0."PriceList" =
T1."PriceList"
INNER JOIN AITM T4 on T4."ItemCode" = T1."ItemCode" and T1."LogInstanc" =
T4."LogInstanc"
INNER JOIN OUSR T5 on T5."INTERNAL_K" = T4."UserSign2"
Inner join OPLN T7 on T0."PriceList" = T7."ListNum"
Where T0."ItemCode" = [%0] and
T0."PriceList" IN ('5')
GROUP BY T0."ItemCode" )
UNION ALL
Select 'a' as "type", T0."ItemCode" , T2."ItemName", T7."ListName" , T1."Price" as
"OldPrice" ,
T4."UpdateDate" , (T1."LogInstanc")+1 as "code" , T5."U_NAME" ,
T2."U_lastpriceupdate", T2."CreateDate"
FROM
OITM T2
Inner Join ITM1 T0 on T0."ItemCode" = T2."ItemCode"
inner join AIT1 T1 on T0."ItemCode" = T1."ItemCode" and T0."PriceList" =
T1."PriceList"
INNER JOIN AITM T4 on T4."ItemCode" = T1."ItemCode" and T1."LogInstanc" =
T4."LogInstanc"
INNER JOIN OUSR T5 on T5."INTERNAL_K" = T4."UserSign2"
Inner join OPLN T7 on T0."PriceList" = T7."ListNum"
Where T0."ItemCode" = [%0] and
T0."PriceList" IN ('5')
order by "type" desc, "code" desc
) as b
on a."ItemCode" = b."ItemCode" and b."code" = a."code"
Where a."NewPrice" <> b."OldPrice"
order by a."type" desc, a."code" desc
--I have noticed in other places where a variable in a nested query may not work. As may --be the case here. Any solutions on how to get this to accept a variable item code? i did find this link related to setting up a declare to name the variable. But I do not have the syntax correct to get it to work.
https://blogs.sap.com/2016/04/01/working-with-sql-query-generator-variables/

This is working for me, I had to comment out the 2 inner Order By as we are using microsoft Sql Server and the Syntax may be different for HANA.
User Parameter can give some very strange errors but the top 4 lines will link it to an SAP table (Item Master in this case) and should solve the problem.
/* SELECT FROM [dbo].[OITM] T0 */
DECLARE #Product AS nvarchar(20)
/* WHERE */
SET #Product = /* T0.ItemCode */ '[%0]'
Select a.*,b."OldPrice" from (
Select 'm' as "type", T0."ItemCode" , T2."ItemName" , T7."ListName" , T0."Price" as
"NewPrice" ,
T2."UpdateDate" , '9999999' as "code" , T5."U_NAME" ,
T2."CreateDate"
FROM
OITM T2
Inner Join ITM1 T0 on T0."ItemCode" = T2."ItemCode"
INNER JOIN OUSR T5 on T5."INTERNAL_K" = T2."UserSign2"
Inner join OPLN T7 on T0."PriceList" = T7."ListNum"
Where T0."ItemCode" = #Product and
T0."PriceList" IN ('5')
UNION ALL
Select 'a' as "type", T0."ItemCode" , T2."ItemName", T7."ListName" , T1."Price" as
"NewPrice",
T4."UpdateDate" , T1."LogInstanc" as "code" , T5."U_NAME" ,
T2."CreateDate"
FROM
OITM T2
Inner Join ITM1 T0 on T0."ItemCode" = T2."ItemCode"
inner join AIT1 T1 on T0."ItemCode" = T1."ItemCode" and T0."PriceList" =
T1."PriceList"
INNER JOIN AITM T4 on T4."ItemCode" = T1."ItemCode" and T1."LogInstanc" =
T4."LogInstanc"
INNER JOIN OUSR T5 on T5."INTERNAL_K" = T4."UserSign2"
Inner join OPLN T7 on T0."PriceList" = T7."ListNum"
Where T0."ItemCode" = #Product and
T0."PriceList" IN ('5')
--order by "type" desc, "code" desc
) as a
LEFT JOIN
(Select 'a' as "type", T0."ItemCode" , T2."ItemName", T7."ListName" , T1."Price" as
"OldPrice" ,
T4."UpdateDate" , '9999999' as "code" , T5."U_NAME" ,
T2."CreateDate"
FROM
OITM T2
Inner Join ITM1 T0 on T0."ItemCode" = T2."ItemCode"
inner join AIT1 T1 on T0."ItemCode" = T1."ItemCode" and T0."PriceList" =
T1."PriceList"
INNER JOIN AITM T4 on T4."ItemCode" = T1."ItemCode" and T1."LogInstanc" =
T4."LogInstanc"
INNER JOIN OUSR T5 on T5."INTERNAL_K" = T4."UserSign2"
Inner join OPLN T7 on T0."PriceList" = T7."ListNum"
Where T0."ItemCode" = #Product and
T0."PriceList" IN ('5') and T1."LogInstanc" IN (
Select max(T1."LogInstanc") as "t"
FROM
OITM T2
Inner Join ITM1 T0 on T0."ItemCode" = T2."ItemCode"
inner join AIT1 T1 on T0."ItemCode" = T1."ItemCode" and T0."PriceList" =
T1."PriceList"
INNER JOIN AITM T4 on T4."ItemCode" = T1."ItemCode" and T1."LogInstanc" =
T4."LogInstanc"
INNER JOIN OUSR T5 on T5."INTERNAL_K" = T4."UserSign2"
Inner join OPLN T7 on T0."PriceList" = T7."ListNum"
Where T0."ItemCode" = #Product and
T0."PriceList" IN ('5')
GROUP BY T0."ItemCode" )
UNION ALL
Select 'a' as "type", T0."ItemCode" , T2."ItemName", T7."ListName" , T1."Price" as
"OldPrice" ,
T4."UpdateDate" , (T1."LogInstanc")+1 as "code" , T5."U_NAME" ,
T2."CreateDate"
FROM
OITM T2
Inner Join ITM1 T0 on T0."ItemCode" = T2."ItemCode"
inner join AIT1 T1 on T0."ItemCode" = T1."ItemCode" and T0."PriceList" =
T1."PriceList"
INNER JOIN AITM T4 on T4."ItemCode" = T1."ItemCode" and T1."LogInstanc" =
T4."LogInstanc"
INNER JOIN OUSR T5 on T5."INTERNAL_K" = T4."UserSign2"
Inner join OPLN T7 on T0."PriceList" = T7."ListNum"
Where T0."ItemCode" = #Product and
T0."PriceList" IN ('5')
--order by "type" desc , "code" desc
) as b
on a."ItemCode" = b."ItemCode" and b."code" = a."code"
Where a."NewPrice" <> b."OldPrice"
order by a."type" desc, a."code" desc

Related

How to link Purchase Order and AP Invoice?

I'm trying to write a query that returns AP Invoice number(s) by keying a Purchase Order number in SAP B1.
PO --> Goods Receipt --> AP Invoice
SELECT DISTINCT
T0.DocNum AS PO,
T4.DocNum AS 'AP Inv'
FROM OPOR T0
LEFT OUTER JOIN PDN1 T1 ON T0.DocEntry = T1.BaseEntry
LEFT OUTER JOIN OPDN T2 ON T1.DocEntry = T2.DocEntry
LEFT OUTER JOIN PCH1 T3 ON T2.DocEntry = T3.BaseEntry
AND T1.LineNum = T3.BaseLine
AND T1.ItemCode = T3.ItemCode
LEFT OUTER JOIN OPCH T4 ON T3.DocEntry = T4.DocEntry
WHERE T0.DocNum = '[%0]'
PO --> Reserved AP Invoice --> Goods Receipt
SELECT DISTINCT
T2.DocNum AS PO,
T0.DocNum AS 'AP Inv'
FROM OPCH T0
INNER JOIN PCH1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN OPOR T2 ON T2.DocEntry = T1.BaseEntry
WHERE T1.BaseType = '22'
AND T2.DocNum = '[%0]'
Is there a way to get AP Invoice number(s) in a query?

Sub Query return more than one value

I have create a table in which data will be inserted.
For data inserting, Have created a query that will insert the information from another table.
If the output is single value, It's updating in the new table.
But If it's multiple value, I am getting an error " Subquery return more than one value"
How to input multiple values using query in table?
Declare #BATCHNO as Nvarchar(10)
Declare #PRODNO as Nvarchar(10)
Declare #ISSUENO AS Nvarchar(10)
set #BATCHNO = (select T2.BatchNum from OWOR T0 INNER JOIN table_P T1 ON T0.DocEntry = T1.BaseEntry
INNER JOIN Table_I T2 ON T1.DocEntry = T2.BaseEntry AND T1.ObjType = T2.BaseType
WHERE T0.DocEntry = '14')
Set #PRODNO = (select T1.BaseRef from OWOR T0 INNER JOIN table_P T1 ON T0.DocEntry = T1.BaseEntry
INNER JOIN Table_I T2 ON T1.DocEntry = T2.BaseEntry AND T1.ObjType = T2.BaseType
WHERE T0.DocEntry = '14' )
Set #ISSUENO = (select T1.DocEntry AS 'ISSUE NUMBER' from OWOR T0 INNER JOIN table_P T1 ON T0.DocEntry = T1.BaseEntry
INNER JOIN Table_I T2 ON T1.DocEntry = T2.BaseEntry AND T1.ObjType = T2.BaseType
WHERE T0.DocEntry = '14')
Insert Into BATCHDETAIL (BATCHNO,PRODNO,ISSUENO) Values(#BATCHNO,#PRODNO,#ISSUENO)
[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. FMS execution failed on field 'U_EA_LICENCE' with query name 'FMS_BATCHNO_UPDATE'
It looks like you are trying to insert multiple rows based on existing data. In which case you should use INSERT .. SELECT rather than INSERT...VALUES:
INSERT BatchDetail (BatchNo, ProdNo, IssueNo)
SELECT T2.BatchNum, T1.BaseRef, T1.DocEntry
FROM OWOR T0
INNER JOIN table_P T1
ON T0.DocEntry = T1.BaseEntry
INNER JOIN Table_I T2
ON T1.DocEntry = T2.BaseEntry
AND T1.ObjType = T2.BaseType
WHERE T0.DocEntry = '14';
If you did only want to insert one record, based on multiple records, you can use TOP 1, and provide an ORDER BY to ensure you get repeatable results:
INSERT BatchDetail (BatchNo, ProdNo, IssueNo)
SELECT TOP 1 T2.BatchNum, T1.BaseRef, T1.DocEntry
FROM OWOR T0
INNER JOIN table_P T1
ON T0.DocEntry = T1.BaseEntry
INNER JOIN Table_I T2
ON T1.DocEntry = T2.BaseEntry
AND T1.ObjType = T2.BaseType
WHERE T0.DocEntry = '14'
ORDER BY t2.BatchNum, T1.BaseRef, T1.DocEntry;
Finally, if you really do want 3 scalar variables, then you can still use SELECT and TOP 1, you just need to use the SELECT for variable assignment:
DECLARE #BATCHNO AS NVARCHAR(10)
#PRODNO AS NVARCHAR(10)
#ISSUENO AS NVARCHAR(10);
SELECT TOP 1
#BATCHNO = T2.BatchNum,
#PRODNO = T1.BaseRef,
#ISSUENO = T1.DocEntry
FROM OWOR T0
INNER JOIN table_P T1
ON T0.DocEntry = T1.BaseEntry
INNER JOIN Table_I T2
ON T1.DocEntry = T2.BaseEntry
AND T1.ObjType = T2.BaseType
WHERE T0.DocEntry = '14'
ORDER BY t2.BatchNum, T1.BaseRef, T1.DocEntry;
ADENDUMEM
I would very much recommend staying away from any of the approaches that use 3 queries with TOP 1 and no order by. For example, if you had a table:
A B C
----------
1 3 2
2 1 3
3 2 1
You might expect this to assign #A, #B, and #C values that correspond to a single record (e.g. 1, 3, 2 or 2, 1, 3)
SET #A = (SELECT TOP 1 A FROM #T);
SET #B = (SELECT TOP 1 B FROM #T);
SET #C = (SELECT TOP 1 C FROM #T);
However, you are executing 3 different queries, and depending on what indexes are on the table(s) you could get 3 different query plans, and 3 different records used for each assignment.
This is fairly easily demonstrated:
-- CREATE TABLE AND FILL IT
IF OBJECT_ID(N'tempdb..#T', 'U') IS NOT NULL DROP TABLE #T;
CREATE TABLE #T (A INT NOT NULL PRIMARY KEY, B INT NOT NULL, C INT NOT NULL);
CREATE NONCLUSTERED INDEX IX_T_B ON #T (B);
CREATE NONCLUSTERED INDEX IX_T_C ON #T (C);
INSERT #T (A, B, C)
VALUES (1, 3, 2), (2, 1, 3), (3, 2, 1);
DECLARE #A INT, #B INT, #C INT;
SET #A = (SELECT TOP 1 A FROM #T);
SET #B = (SELECT TOP 1 B FROM #T);
SET #C = (SELECT TOP 1 C FROM #T);
SELECT A = #A, B = #B, C = #C;
For me this returns:
A B C
----------
3 1 1
Which does not correspond to any record in the original data. This might be absolutely fine for your needs, but it is definitely something to be aware of.
Example on db<>fiddle
You need to fix your subqueries so they return the exact single value to be inserted.
For example, if you don't really care and can insert whatever value that fulfills those conditions, then simply add a TOP 1 to your subqueries, so they will return only the first value available.
Declare #BATCHNO as Nvarchar(10)
Declare #PRODNO as Nvarchar(10)
Declare #ISSUENO AS Nvarchar(10)
set #BATCHNO = (select TOP 1 T2.BatchNum from OWOR T0 INNER JOIN table_P T1 ON T0.DocEntry = T1.BaseEntry
INNER JOIN Table_I T2 ON T1.DocEntry = T2.BaseEntry AND T1.ObjType = T2.BaseType
WHERE T0.DocEntry = '14')
Set #PRODNO = (select TOP 1 T1.BaseRef from OWOR T0 INNER JOIN table_P T1 ON T0.DocEntry = T1.BaseEntry
INNER JOIN Table_I T2 ON T1.DocEntry = T2.BaseEntry AND T1.ObjType = T2.BaseType
WHERE T0.DocEntry = '14' )
Set #ISSUENO = (select TOP 1 T1.DocEntry AS 'ISSUE NUMBER' from OWOR T0 INNER JOIN table_P T1 ON T0.DocEntry = T1.BaseEntry
INNER JOIN Table_I T2 ON T1.DocEntry = T2.BaseEntry AND T1.ObjType = T2.BaseType
WHERE T0.DocEntry = '14')
Insert Into BATCHDETAIL (BATCHNO,PRODNO,ISSUENO) Values(#BATCHNO,#PRODNO,#ISSUENO)
By the way, you can simplify your script assigning the three variables on a single query :
Declare #BATCHNO as Nvarchar(10)
Declare #PRODNO as Nvarchar(10)
Declare #ISSUENO AS Nvarchar(10)
select TOP 1 #BATCHNO = T2.BatchNum,
#PRODNO = T1.BaseRef,
#ISSUENO = T1.DocEntry
from OWOR T0
INNER JOIN table_P T1 ON T0.DocEntry = T1.BaseEntry
INNER JOIN Table_I T2 ON T1.DocEntry = T2.BaseEntry AND T1.ObjType = T2.BaseType
where T0.DocEntry = '14'
Insert Into BATCHDETAIL (BATCHNO,PRODNO,ISSUENO) Values(#BATCHNO,#PRODNO,#ISSUENO)
Finally, if you want to insert all the values that fulfill your condition, then instead of taking just the first one with TOP 1, you can insert all of them feeding the insert with a select :
Insert Into BATCHDETAIL (BATCHNO,PRODNO,ISSUENO)
select T2.BatchNum, T1.BaseRef, T1.DocEntry
from OWOR T0
INNER JOIN table_P T1 ON T0.DocEntry = T1.BaseEntry
INNER JOIN Table_I T2 ON T1.DocEntry = T2.BaseEntry AND T1.ObjType = T2.BaseType
where T0.DocEntry = '14'
use this.
Declare #BATCHNO as Nvarchar(10)
Declare #PRODNO as Nvarchar(10)
Declare #ISSUENO AS Nvarchar(10)
set #BATCHNO = (select TOP 1 T2.BatchNum from OWOR T0 INNER JOIN table_P T1 ON T0.DocEntry = T1.BaseEntry
INNER JOIN Table_I T2 ON T1.DocEntry = T2.BaseEntry AND T1.ObjType = T2.BaseType
WHERE T0.DocEntry = '14')
Set #PRODNO = (select TOP 1 T1.BaseRef from OWOR T0 INNER JOIN table_P T1 ON T0.DocEntry = T1.BaseEntry
INNER JOIN Table_I T2 ON T1.DocEntry = T2.BaseEntry AND T1.ObjType = T2.BaseType
WHERE T0.DocEntry = '14' )
Set #ISSUENO = (select TOP 1 T1.DocEntry AS 'ISSUE NUMBER' from OWOR T0 INNER JOIN table_P T1 ON T0.DocEntry = T1.BaseEntry
INNER JOIN Table_I T2 ON T1.DocEntry = T2.BaseEntry AND T1.ObjType = T2.BaseType
WHERE T0.DocEntry = '14')
Insert Into BATCHDETAIL (BATCHNO,PRODNO,ISSUENO) Values(#BATCHNO,#PRODNO,#ISSUENO)

SQL Using two queries with JOIN

I am fairly new to SQL. I was trying to use two queries and join them together to get the result I want:
select
A.[Article:Link]
,A.[ArtNo]
,A.[GenericArticle:Link]
,A.[Vehicle:Link]
,A.[LinkingTargetNo]
,Q1.[Name]
FROM [HellaPim].[dbo].[TecDoc.ArticleLinkage.Linkages] A WITH (NOLOCK)
LEFT OUTER JOIN
(SELECT
base.[TecDocNo],
base.[VehicleType],
base.[TypeNo],
T3.[Manufacturer:Link],
T24.[Name],
T1.[Designation],
T1.[LongDesignation],
T2.[DescriptionNo],
base.[Model:Link],
T4.[Designation],
base.[SortNo],
base.[ConstructionYearFrom],
base.[ConstructionYearTo],
base.[KW],
base.[PS],
base.[CcmTaxation],
base.[CcmTechnical],
base.[CapacityLitres],
base.[NumCylinders],
base.[NumDoors],
base.[FuelTankCapacity],
base.[MainVoltage],
base.[ABS],
base.[ASR],
base.[EngineType:Link],
T6.[Description <4>],
base.[FuelMixtureFormation:Link],
T8.[Description <4>],
base.[DriveType:Link],
T10.[Description <4>],
base.[BrakeType:Link],
T12.[Description <4>],
base.[BrakeSystem:Link],
T14.[Description <4>],
base.[NumValves],
base.[FuelType:Link],
T16.[Description <4>],
base.[CatalystConverterType:Link],
T18.[Description <4>],
base.[TransmissionType:Link],
T20.[Description <4>],
base.[BodyType:Link],
T22.[Description <4>]
FROM [HellaPim].[dbo].[TecDoc.LinkingTargets.Types] base WITH (NOLOCK)
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.LinkingTargets.Models] T3 WITH (NOLOCK)
ON T3.[ModelNo] = [base].[Model:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.Manufacturers] T23 WITH (NOLOCK)
ON T23.[ManufacturerNo] = [T3].[Manufacturer:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.Manufacturers <TecDoc.GeneralData.Countries>] T24 WITH (NOLOCK)
ON T24.[:Id] = [T23].[:Id]
AND T24.[:TecDoc.GeneralData.Countries_Id] = 'A98E18B0-1401-4104-9EF9-827497E6407F'
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.LinkingTargets.Types <TecDoc.GeneralData.Countries>] T1 WITH (NOLOCK)
ON T1.[:Id] = [base].[:Id]
AND T1.[:TecDoc.GeneralData.Countries_Id] = 'D3ECF38D-A5D3-4A46-BED1-24853E25DFC8'
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.CouLngDescriptions] T2 WITH (NOLOCK)
ON T2.[DescriptionNo] = [base].[Description:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.LinkingTargets.Models <TecDoc.GeneralData.Countries>] T4 WITH (NOLOCK)
ON T4.[:Id] = [T3].[:Id]
AND T4.[:TecDoc.GeneralData.Countries_Id] = 'D3ECF38D-A5D3-4A46-BED1-24853E25DFC8'
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T5 WITH (NOLOCK)
ON T5.[KeyValueNo] = [base].[EngineType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T6 WITH (NOLOCK)
ON T6.[DescriptionNo] = [T5].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T7 WITH (NOLOCK)
ON T7.[KeyValueNo] = [base].[FuelMixtureFormation:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T8 WITH (NOLOCK)
ON T8.[DescriptionNo] = [T7].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T9 WITH (NOLOCK)
ON T9.[KeyValueNo] = [base].[DriveType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T10 WITH (NOLOCK)
ON T10.[DescriptionNo] = [T9].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T11 WITH (NOLOCK)
ON T11.[KeyValueNo] = [base].[BrakeType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T12 WITH (NOLOCK)
ON T12.[DescriptionNo] = [T11].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T13 WITH (NOLOCK)
ON T13.[KeyValueNo] = [base].[BrakeSystem:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T14 WITH (NOLOCK)
ON T14.[DescriptionNo] = [T13].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T15 WITH (NOLOCK)
ON T15.[KeyValueNo] = [base].[FuelType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T16 WITH (NOLOCK)
ON T16.[DescriptionNo] = [T15].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T17 WITH (NOLOCK)
ON T17.[KeyValueNo] = [base].[CatalystConverterType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T18 WITH (NOLOCK)
ON T18.[DescriptionNo] = [T17].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T19 WITH (NOLOCK)
ON T19.[KeyValueNo] = [base].[TransmissionType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T20 WITH (NOLOCK)
ON T20.[DescriptionNo] = [T19].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T21 WITH (NOLOCK)
ON T21.[KeyValueNo] = [base].[BodyType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T22 WITH (NOLOCK)
ON T22.[DescriptionNo] = [T21].[Name:Link]
) as Q1 on A.[LinkingTargetNo] = Q1.[TypeNo]
My error is
Msg 8156, Level 16, State 1, Line 107
The column 'Designation' was specified multiple times for 'Q1'.
The two queries individual work perfectly fine. The problem is when I join them together. As you can see I pipe in [Designation] two times but once from T1 and once from T4.
What am I missing?
use alias for designation
select
A.[Article:Link]
,A.[ArtNo]
,A.[GenericArticle:Link]
,A.[Vehicle:Link]
,A.[LinkingTargetNo]
,Q1.[Name]
FROM [HellaPim].[dbo].[TecDoc.ArticleLinkage.Linkages] A WITH (NOLOCK)
LEFT OUTER JOIN
(SELECT
base.[TecDocNo],
base.[VehicleType],
base.[TypeNo],
T3.[Manufacturer:Link],
T24.[Name],
T1.[Designation] as DesignationT1 ,
T1.[LongDesignation],
T2.[DescriptionNo],
base.[Model:Link],
T4.[Designation],
base.[SortNo],
base.[ConstructionYearFrom],
base.[ConstructionYearTo],
base.[KW],
base.[PS],
base.[CcmTaxation],
base.[CcmTechnical],
base.[CapacityLitres],
base.[NumCylinders],
base.[NumDoors],
base.[FuelTankCapacity],
base.[MainVoltage],
base.[ABS],
base.[ASR],
base.[EngineType:Link],
T6.[Description <4>],
base.[FuelMixtureFormation:Link],
T8.[Description <4>],
base.[DriveType:Link],
T10.[Description <4>],
base.[BrakeType:Link],
T12.[Description <4>],
base.[BrakeSystem:Link],
T14.[Description <4>],
base.[NumValves],
base.[FuelType:Link],
T16.[Description <4>],
base.[CatalystConverterType:Link],
T18.[Description <4>],
base.[TransmissionType:Link],
T20.[Description <4>],
base.[BodyType:Link],
T22.[Description <4>]
FROM [HellaPim].[dbo].[TecDoc.LinkingTargets.Types] base WITH (NOLOCK)
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.LinkingTargets.Models] T3 WITH (NOLOCK)
ON T3.[ModelNo] = [base].[Model:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.Manufacturers] T23 WITH (NOLOCK)
ON T23.[ManufacturerNo] = [T3].[Manufacturer:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.Manufacturers <TecDoc.GeneralData.Countries>] T24 WITH (NOLOCK)
ON T24.[:Id] = [T23].[:Id]
AND T24.[:TecDoc.GeneralData.Countries_Id] = 'A98E18B0-1401-4104-9EF9-827497E6407F'
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.LinkingTargets.Types <TecDoc.GeneralData.Countries>] T1 WITH (NOLOCK)
ON T1.[:Id] = [base].[:Id]
AND T1.[:TecDoc.GeneralData.Countries_Id] = 'D3ECF38D-A5D3-4A46-BED1-24853E25DFC8'
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.CouLngDescriptions] T2 WITH (NOLOCK)
ON T2.[DescriptionNo] = [base].[Description:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.LinkingTargets.Models <TecDoc.GeneralData.Countries>] T4 WITH (NOLOCK)
ON T4.[:Id] = [T3].[:Id]
AND T4.[:TecDoc.GeneralData.Countries_Id] = 'D3ECF38D-A5D3-4A46-BED1-24853E25DFC8'
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T5 WITH (NOLOCK)
ON T5.[KeyValueNo] = [base].[EngineType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T6 WITH (NOLOCK)
ON T6.[DescriptionNo] = [T5].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T7 WITH (NOLOCK)
ON T7.[KeyValueNo] = [base].[FuelMixtureFormation:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T8 WITH (NOLOCK)
ON T8.[DescriptionNo] = [T7].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T9 WITH (NOLOCK)
ON T9.[KeyValueNo] = [base].[DriveType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T10 WITH (NOLOCK)
ON T10.[DescriptionNo] = [T9].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T11 WITH (NOLOCK)
ON T11.[KeyValueNo] = [base].[BrakeType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T12 WITH (NOLOCK)
ON T12.[DescriptionNo] = [T11].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T13 WITH (NOLOCK)
ON T13.[KeyValueNo] = [base].[BrakeSystem:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T14 WITH (NOLOCK)
ON T14.[DescriptionNo] = [T13].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T15 WITH (NOLOCK)
ON T15.[KeyValueNo] = [base].[FuelType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T16 WITH (NOLOCK)
ON T16.[DescriptionNo] = [T15].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T17 WITH (NOLOCK)
ON T17.[KeyValueNo] = [base].[CatalystConverterType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T18 WITH (NOLOCK)
ON T18.[DescriptionNo] = [T17].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T19 WITH (NOLOCK)
ON T19.[KeyValueNo] = [base].[TransmissionType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T20 WITH (NOLOCK)
ON T20.[DescriptionNo] = [T19].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T21 WITH (NOLOCK)
ON T21.[KeyValueNo] = [base].[BodyType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T22 WITH (NOLOCK)
ON T22.[DescriptionNo] = [T21].[Name:Link]
) as Q1 on A.[LinkingTargetNo] = Q1.[TypeNo]
You can try below - you've two designation column one is T1.[Designation] and another one is T4.[Designation] - one have to be renamed
select
A.[Article:Link]
,A.[ArtNo]
,A.[GenericArticle:Link]
,A.[Vehicle:Link]
,A.[LinkingTargetNo]
,Q1.[Name]
FROM [HellaPim].[dbo].[TecDoc.ArticleLinkage.Linkages] A WITH (NOLOCK)
LEFT OUTER JOIN
(SELECT
base.[TecDocNo],
base.[VehicleType],
base.[TypeNo],
T3.[Manufacturer:Link],
T24.[Name],
T1.[Designation] as designation1,
T1.[LongDesignation],
T2.[DescriptionNo],
base.[Model:Link],
T4.[Designation],
base.[SortNo],
base.[ConstructionYearFrom],
base.[ConstructionYearTo],
base.[KW],
base.[PS],
base.[CcmTaxation],
base.[CcmTechnical],
base.[CapacityLitres],
base.[NumCylinders],
base.[NumDoors],
base.[FuelTankCapacity],
base.[MainVoltage],
base.[ABS],
base.[ASR],
base.[EngineType:Link],
T6.[Description <4>],
base.[FuelMixtureFormation:Link],
T8.[Description <4>],
base.[DriveType:Link],
T10.[Description <4>],
base.[BrakeType:Link],
T12.[Description <4>],
base.[BrakeSystem:Link],
T14.[Description <4>],
base.[NumValves],
base.[FuelType:Link],
T16.[Description <4>],
base.[CatalystConverterType:Link],
T18.[Description <4>],
base.[TransmissionType:Link],
T20.[Description <4>],
base.[BodyType:Link],
T22.[Description <4>]
FROM [HellaPim].[dbo].[TecDoc.LinkingTargets.Types] base WITH (NOLOCK)
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.LinkingTargets.Models] T3 WITH (NOLOCK)
ON T3.[ModelNo] = [base].[Model:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.Manufacturers] T23 WITH (NOLOCK)
ON T23.[ManufacturerNo] = [T3].[Manufacturer:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.Manufacturers <TecDoc.GeneralData.Countries>] T24 WITH (NOLOCK)
ON T24.[:Id] = [T23].[:Id]
AND T24.[:TecDoc.GeneralData.Countries_Id] = 'A98E18B0-1401-4104-9EF9-827497E6407F'
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.LinkingTargets.Types <TecDoc.GeneralData.Countries>] T1 WITH (NOLOCK)
ON T1.[:Id] = [base].[:Id]
AND T1.[:TecDoc.GeneralData.Countries_Id] = 'D3ECF38D-A5D3-4A46-BED1-24853E25DFC8'
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.CouLngDescriptions] T2 WITH (NOLOCK)
ON T2.[DescriptionNo] = [base].[Description:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.LinkingTargets.Models <TecDoc.GeneralData.Countries>] T4 WITH (NOLOCK)
ON T4.[:Id] = [T3].[:Id]
AND T4.[:TecDoc.GeneralData.Countries_Id] = 'D3ECF38D-A5D3-4A46-BED1-24853E25DFC8'
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T5 WITH (NOLOCK)
ON T5.[KeyValueNo] = [base].[EngineType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T6 WITH (NOLOCK)
ON T6.[DescriptionNo] = [T5].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T7 WITH (NOLOCK)
ON T7.[KeyValueNo] = [base].[FuelMixtureFormation:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T8 WITH (NOLOCK)
ON T8.[DescriptionNo] = [T7].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T9 WITH (NOLOCK)
ON T9.[KeyValueNo] = [base].[DriveType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T10 WITH (NOLOCK)
ON T10.[DescriptionNo] = [T9].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T11 WITH (NOLOCK)
ON T11.[KeyValueNo] = [base].[BrakeType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T12 WITH (NOLOCK)
ON T12.[DescriptionNo] = [T11].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T13 WITH (NOLOCK)
ON T13.[KeyValueNo] = [base].[BrakeSystem:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T14 WITH (NOLOCK)
ON T14.[DescriptionNo] = [T13].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T15 WITH (NOLOCK)
ON T15.[KeyValueNo] = [base].[FuelType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T16 WITH (NOLOCK)
ON T16.[DescriptionNo] = [T15].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T17 WITH (NOLOCK)
ON T17.[KeyValueNo] = [base].[CatalystConverterType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T18 WITH (NOLOCK)
ON T18.[DescriptionNo] = [T17].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T19 WITH (NOLOCK)
ON T19.[KeyValueNo] = [base].[TransmissionType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T20 WITH (NOLOCK)
ON T20.[DescriptionNo] = [T19].[Name:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.KeyValues] T21 WITH (NOLOCK)
ON T21.[KeyValueNo] = [base].[BodyType:Link]
LEFT OUTER JOIN [HellaPim].[dbo].[TecDoc.GeneralData.LngDescriptions] T22 WITH (NOLOCK)
ON T22.[DescriptionNo] = [T21].[Name:Link]
) as Q1 on A.[LinkingTargetNo] = Q1.[TypeNo]
Your SELECT query has T1.Designate and T4.Designate and that might be the problem. Maybe try adding an alias to one of them, eg. T4.Designate AS Designate4.
Please set aliases for every duplicate column in the list
like this
T4.[Designation] As T4Designation,
T6.[Description <4>] As T6Description,
and so on.

Cumulative total based off of two columns

So I am looking to get an extra column into my query that calculates a cumulative balance based off of the credit/debit columns.
My query looks like this so far:
SELECT T1.[RefDate], T1.[TransId], T1.[BaseRef], T1.[LineMemo], T3.[Dscription], T1.[Debit], T1.[Credit], T4.[AcctName]
FROM OJDT T0
right JOIN JDT1 T1 ON T0.TransId = T1.TransId
left JOIN OPCH T2 ON T0.TransId = T2.TransId
left JOIN PCH1 T3 ON T3.DocEntry = T2.DocEntry
left JOIN OACT T4 ON T1.Account = T4.AcctCode
WHERE T4.[AcctCode] = [%0] AND
T1.[RefDate] BETWEEN [%1] AND [%2]
You could use windowed SUM:
SELECT T1.[RefDate], T1.[TransId], T1.[BaseRef], T1.[LineMemo], T3.[Dscription],
T1.[Debit], T1.[Credit], T4.[AcctName],
SUM(ISNULL(Debit,0)+ISNULL(Credit,0)) OVER(PARTITION BY ... ORDER BY refDate)
FROM OJDT T0
right JOIN JDT1 T1 ON T0.TransId = T1.TransId
left JOIN OPCH T2 ON T0.TransId = T2.TransId
left JOIN PCH1 T3 ON T3.DocEntry = T2.DocEntry
left JOIN OACT T4 ON T1.Account = T4.AcctCode
WHERE T4.[AcctCode] = [%0] AND
T1.[RefDate] BETWEEN [%1] AND [%2];
If Debit column contains positive values you need to prefix it with -.
Documentation around OVER: https://msdn.microsoft.com/en-us/library/ms189461(v=SQL.110).aspx
You'll probably find example C to be the most useful though it looks like you don't want to PARTITION, you want to use RANGE.
you'll want something like this for your calculated column:
SUM(ISNULL(Debit,0)+ISNULL(Credit,0))
OVER(RANGE UNBOUNDED PRECEDING AND CURRENT ROW) AS RunningTotal
This will result in a query something like:
SELECT T1.[RefDate], T1.[TransId], T1.[BaseRef], T1.[LineMemo], T3.[Dscription], T1.[Debit], T1.[Credit], T4.[AcctName],
SUM(ISNULL(Debit,0)+ISNULL(Credit,0))
OVER(RANGE UNBOUNDED PRECEDING AND CURRENT ROW) AS RunningTotal
FROM OJDT T0
right JOIN JDT1 T1 ON T0.TransId = T1.TransId
left JOIN OPCH T2 ON T0.TransId = T2.TransId
left JOIN PCH1 T3 ON T3.DocEntry = T2.DocEntry
left JOIN OACT T4 ON T1.Account = T4.AcctCode
WHERE T4.[AcctCode] = [%0] AND
T1.[RefDate] BETWEEN [%1] AND [%2]

trying to add a selection to the following query

I nee to add a user selected date (DocDate) to the following query
SELECT DocNum, CardCode, CardName FROM ORDR WHERE DOCENTRY NOT IN(
select DISTINCT(T0.DOCENTRY)
from ORDR T0 JOIN RDR1 T1 ON T0.DOCENTRY = T1.DOCENTRY
LEFT JOIN OITM T2 ON T1.ITEMCODE = T2.ITEMCODE
LEFT JOIN OITB T3 ON T2.ItmsGrpCod = T3.ItmsGrpCod
WHERE T3.ItmsGrpNam = 'Carriage Out'
AND T0.DocType = 'I'
)
AND DOCTYPE = 'I'
Improved query - not yet an answer though.
SELECT DocNum, CardCode, CardName
FROM ORDR
WHERE DOCENTRY NOT IN
(
select T0.DOCENTRY
from ORDR T0
JOIN RDR1 T1 ON T0.DOCENTRY = T1.DOCENTRY
LEFT JOIN OITM T2 ON T1.ITEMCODE = T2.ITEMCODE
LEFT JOIN OITB T3 ON T2.ItmsGrpCod = T3.ItmsGrpCod
AND T3.ItmsGrpNam = 'Carriage Out'
WHERE T0.DocType = 'I'
)
AND DOCTYPE = 'I'
where YEAR(t0.DocDate) = YEAR(#date) where #date is your sent input, you can either check for specific date, same year or month.