SBO tables Relationship (AR INVOICE-INCOMING Payment) - sql

am making a query that bring Incoming Payments details , that include payment means , details of payment means , and then some UDFS from the related A/R invoice(s) , in addition to some UDF from an object that relate to a UDF in the AR INVOICE ,
now every time am running my query it show no result.
Am sure there is something I missing here or incorrect but so far couldn't find it .
if any one can help me with this i will be thankful
here is the query :
SELECT T1.[baseAbs] AS INVOICENO, T0.[DocDate],t0.[trsfrdate],t0.[trsfrref], T0.[CardName],T0.[Doctotal],T4.[VoucherNum] ,
T0.[Comments], T1.[DocNum] AS PAYMENTNO, T2.[Phone1],
T0.[CashSum], T0.[CreditSum], T0.[CheckSum], T0.[TrsfrSum],
T3.[DueDate] AS CHECKDATE, T3.[CheckNum] AS CHECKNO, T3.[Details] AS MAYBEBANKNAME
, t5.[U_UnitCode],t5.[U_Type],t7.[WhsName],t7.[city] ,
t8.U_FloorNo
FROM ORCT T0
inner JOIN RCT2 T1 ON T0.[DocEntry] = T1.[DocNum]
inner JOIN OINV T5 ON T5.[docnum] =T1.[BaseAbs]
INNER JOIN RCT1 T3 ON T0.[DocNum] = T3.[DocNum]
INNER JOIN RCT3 T4 ON T0.[DocNum] = T4.[DocNum]
INNER JOIN OCRD T2 ON T0.[CardCode] = T2.[CardCode]
INNER JOIN INV1 T6 ON T5.[DocEntry] = T6.[DocEntry]
INNER JOIN OWHS T7 ON T6.[WhsCode] = T7.[WhsCode]
INNER JOIN [dbo].[#AUND] T8 ON T5.[U_UnitCode] = T8.[Code]

the query is now working fine now , the problem was in inner join , it should been replaced to left join ,
here is the fixed one :
select T0.DocNum as 'Payment Number',T0.DocDate 'Payment Date',T0.CardCode,
T0.CardName 'Customer Name',T1.BankCode 'Bankcode',T3.BankName 'Bank Name', T2.Phone1 ,
T0.CreditSum,
T0.CashSum,
T0.TrsfrSum,
t0.CheckSum,
t1.CheckNum as 'Check Number',
t1.DueDate as 'check date',
t6.VoucherNum as 'Voucher Number',
t0.TrsfrRef as 'Transfer No',
t0.TrsfrDate AS 'Transfer Date',
ousr.USER_code as 'user code',
T5.DocNum, t11.U_P_BuildingName as 'Building Name',
CASE when T5.DocNum is null then 'On Account' else 'Paid For Invoice' END AS 'Payment Status',
CASE when T5.DocStatus = 'O' then 'Open' else 'Closed' END AS ' Invoice Status',
T4.SumApplied as 'Amount Paid on Invoice',T9.U_FloorNo,T5.U_UnitCode,T5.U_Type,
t0.DocTotal as 'Payment Total',t5.DocTotal as'Invoice Total' , t8.City,
t0.Comments as'Remarks'
from ORCT T0
left join rct1 T1 on T0.DocNum=T1.DocNum
left join ocrd T2 on T2.CardCode=T0.CardCode
left outer join ODSC T3 on T3.BankCode=T0.BankCode
left join RCT2 T4 on T0.DocNum = T4.DocNum
left join RCT3 T6 on T0.DocNum = T6.DocNum
left join OINV T5 on T4.DocEntry = T5.DocEntry and T5.ObjType = T4.InvType
left join oitm t11 on t5.u_unitcode = t11.ItemCode
LEFT JOIN OWHS T8 ON T11.U_P_BuildingNum = T8.WhsCode
LEFT JOIN [dbo].[#AUND] T9 ON T5.[U_UnitCode] = T9.[Code]
INNER JOIN OSLP T10 ON T5.[SlpCode] = T10.[SlpCode]
inner join ousr on ousr.USERID = t0.usersign
where
T4.InvType <> '14' and T0.[Canceled] = 'N' and t0.docnum=200001

I think there's a few issues with your query, starting with the join from ORCT to RCT2.
I've created similar queries in the past, here's one which I know works, maybe you can use it to adjust yours. For one thing, you'll definitely need to adjust a lot of those inner joins to outer joins, as a lot of the relations between Payments and it's parent business objects (like Invoices) are very loose and may not always apply.
Note that the query below is looking specifically for Invoices in the RCT2 table (this is the "lines" section of the Incoming Payment object), hence the J002.InvType = 13 condition.
SELECT *
FROM [ORCT] J001
LEFT OUTER JOIN [RCT2] J002 ON J002.DocNum = J001.DocNum AND J002.InvType = 13
LEFT OUTER JOIN [OINV] J003 ON J003.DocEntry = J002.DocEntry
LEFT OUTER JOIN [OACT] J004 ON J004.AcctCode = J001.CashAcct
LEFT OUTER JOIN [RCT1] J005 ON J005.DocNum = J001.DocNum

Related

Only include emails once in SQL query

Having a bit of a dead brain moment right now.
I have this query to give me BP Codes,Names, Contacts and Sales Person. I only want to include an existing email once in the query. If the same email exists for multiple BP's then only 1 BP should be included (lets say TOP 1).
SELECT DISTINCT
OCRD.CardCode,
OCRD.CardName,
OCPR.Name,
OCPR.E_MailL,
OSLP.SlpName
FROM OCRD
INNER JOIN OCRG ON OCRG.GroupCode = OCRD.GroupCode
INNER JOIN OCPR ON OCPR.CardCode = OCRD.CardCode
INNER JOIN OOND ON OOND.IndCode = OCRD.IndustryC
INNER JOIN OSLP ON OCRD.SlpCode = OSLP.SlpCode
WHERE OCRG.GroupName <> 'Retail' AND
OOND.IndName = 'Aged Care' and
OCRD.frozenFor = 'N' AND
OCPR.E_MailL LIKE '%#%'
ORDER BY OCRD.CardCode ASC
For the life of me I cant figure it out right now. Any help will be appreciated.
Based on this stackoverflow post, I assume this might work
SELECT DISTINCT ON (OCPR.E_MailL)
OCRD.CardCode,
OCRD.CardName,
OCPR.Name,
OCPR.E_MailL,
OSLP.SlpName
FROM OCRD
INNER JOIN OCRG ON OCRG.GroupCode = OCRD.GroupCode
INNER JOIN OCPR ON OCPR.CardCode = OCRD.CardCode
INNER JOIN OOND ON OOND.IndCode = OCRD.IndustryC
INNER JOIN OSLP ON OCRD.SlpCode = OSLP.SlpCode
WHERE OCRG.GroupName <> 'Retail' AND
OOND.IndName = 'Aged Care' and
OCRD.frozenFor = 'N' AND
OCPR.E_MailL LIKE '%#%'
ORDER BY OCRD.CardCode ASC, OCRD.E_MAIL

What is the link in SAP Business One between Sales Orders, Deliveries, and Invoices

I'm trying to join ORDR, ODLN and OINV in a SAP Business One query, but I can't seem to find a field that they share in common.
There must be some record somewhere that links one to another.
Are they linked via a separate table? Or am I missing something obvious?
I am using SAP HANA as my DB, so queries in HANA are preferred rather than MSSQL.
First, credit to Eralper for their answer, as the link contained in it helped me find the solution I was looking for. However, their solution does not include an explanation and does not quite give the result that is being looked for.
The main information for a Sales Order in SAP is stored in two tables, ORDR and RDR1. ORDR has one line for each Sales Order, while RDR1 has one line for each product row on the Sales Order.
Delivery Notes and Invoices (and basically any document in SAP) follow this pattern.
Why is this important to this question? Because the column that contains the data to link Sales Orders, Delivery Notes and Invoices is in RDR1 (or the similar variant). It's name is TrgetEntry.
As there is a row for each product on a Sales Order, we can't simply do a join, as any Sales Order that has more than one product will appear multiple times in the result. The following query uses grouping to show a table that has a line for each Sales Order, and has the needed information to link it to Delivery Notes.
SELECT T0."DocEntry" AS "SO DE", T0."DocNum" AS "Sales Order Number", T1."TrgetEntry" AS "SO TE", COUNT(T0."DocNum") AS "Rows"
FROM ORDR T0
LEFT JOIN RDR1 T1 ON T0."DocEntry" = T1."DocEntry"
GROUP BY T0."DocEntry", T0."DocNum", T1."TrgetEntry"
By just changing the table names, similar queries can be created for Delivery Notes and Invoices.
Then you can use the TrgetEntry and DocEntry to link the various results.
The final code I use to show Sales Orders, their related Deliveries and Invoices is the following:
SELECT S0."SalesOrderNumber", S1."DeliveryNumber", S2."DocNum" AS "InvoiceNumber", S0."Rows", S2."DocTotal"
FROM (SELECT T0."DocEntry" AS "SO_DE", T0."DocNum" AS "SalesOrderNumber", T1."TrgetEntry" AS "SO_TE", COUNT(T0."DocNum") AS "Rows"
FROM ORDR T0
LEFT JOIN RDR1 T1 ON T0."DocEntry" = T1."DocEntry"
GROUP BY T0."DocEntry", T0."DocNum", T1."TrgetEntry") S0
LEFT JOIN (SELECT T0."DocEntry" AS "DN_DE", T0."DocNum" AS "DeliveryNumber", T1."TrgetEntry" AS "DN_TE"
FROM ODLN T0
LEFT JOIN DLN1 T1 ON T0."DocEntry" = T1."DocEntry"
GROUP BY T0."DocEntry", T0."DocNum", T1."TrgetEntry") S1 ON S0."SO_TE" = S1."DN_DE"
LEFT JOIN OINV S2 ON S1."DN_TE" = S2."DocEntry"
Please check https://archive.sap.com/discussions/thread/1440163
There following relationship is given
SELECT Distinct(T0.DocNum ),T0.DocDate, T0.CardCode, T0.CardName, T1.ItemCode, T1.Quantity, T1.Price,T1.TotalSumSy, T0.DocTotal
FROM ORDR T0
INNER JOIN RDR1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN ODLN T2 ON T2.DocEntry = T1.TrgetEntry
INNER JOIN DLN1 T3 on T3.DocEntry = T2.Docentry
INNER JOIN OINV T4 ON T4.DocEntry = T3.TrgetEntry
INNER JOIN INV1 T5 ON T5.DocEntry = T4.DocEntry
LEFT JOIN ORDN T6 ON T6.DocEntry = T5.TrgetEntry
LEFT JOIN RDN1 T7 ON T7.DocEntry = T6.DocEntry
So following relation is also true
SELECT *
FROM ODLN T2
INNER JOIN DLN1 T3 on T3.DocEntry = T2.Docentry
INNER JOIN OINV T4 ON T4.DocEntry = T3.TrgetEntry

Dynamic Pivots for year in SQL Server

I'm trying to find a better way of reporting on some of our data within SQL Server, currently we have a report that looks at sales value in a certain country pivoted by year.
The report give me the correct data, but every year i'm having to add an extra column in for the new year. I've read a little bit about Dynamic pivot queries, but I cannot make them to work.
Here is my current query:
SELECT
isnull([TYPE],'Total') as 'Type',
sum(isnull([2010],0)) as '2010',
sum(isnull([2011],0)) as '2011',
sum(isnull([2012],0)) as '2012',
sum(isnull([2013],0)) as '2013',
sum(isnull([2014],0)) as '2014',
sum(isnull([2015],0)) as '2015',
sum(isnull([2016],0)) as '2016',
sum(isnull([2017],0)) as '2017',
sum(isnull([2018],0)) as '2018',
sum(isnull([2010],0) +isnull([2011],0) +isnull([2012],0) +isnull([2013],0) +isnull([2014],0) +isnull([2015],0) +isnull([2016],0) +isnull([2017],0) +isnull([2018],0)) as 'total'
FROM
(
SELECT
T3.Name 'Type' ,
DATEPART(YEAR,T0.DocDate) 'Year',
SUM(T1.totalsumsy) 'Quantity'
FROM
OINV T0 with (nolock) INNER JOIN
INV1 T1 with (nolock) ON T0.DocEntry = T1.DocEntry left join
INV12 t9 with (nolock) on t0.DocEntry = t9.DocEntry inner join
OCRD t7 with (nolock) on t0.CardCode = t7.CardCode INNER JOIN
[#AA_REGION] T8 ON case when isnull(t0.u_b2c,'n') = 'y' then t9.countrys else ISNULL(T7.U_COUNTRY,T7.COUNTRY) end = T8.CODE AND T8.code = 'au' INNER JOIN
OITM T2 with (nolock) ON T1.ItemCode = T2.ItemCode LEFT JOIN
[#AA_ITEMTYPES] T3 ON T2.U_CATEGORY = T3.Code LEFT JOIN
[#AA_STYLE] T4 ON T2.U_Style = T4.Code LEFT JOIN
[#AA_STYLEGROUP] T5 ON T4.U_GROUP = T5.Code LEFT JOIN
[#AA_ANIMAL] T6 ON T2.U_ANIMAL = T6.Code
WHERE
T5.Code = '010' AND
T6.CODE = '001' and
T0.DocDate >= '01.01.2010'
GROUP BY
T3.Name ,
DATEPART(YEAR,T0.DocDate)
)PS
PIVOT
(SUM(Quantity) for [Year] in ([2010],[2011],[2012],[2013],[2014],[2015],[2016], [2017], [2018]) ) as pvt
group by
[type] with rollup
order by sum(isnull([2010],0) +isnull([2011],0) +isnull([2012],0) +isnull([2013],0) +isnull([2014],0)+isnull([2015],0)+isnull([2016],0)) desc
Can anyone point me in the right direction to make the above query dynamic by year?

Conversion failed when converting the nvarchar value 'FALL' to data type int

I have a query below that I have been struggled with. I know that my evt_code column is integer but includes nvarchar value also (for example FALL2015,JP19200202 and many others including letters).
I need to include those nvarchar codes in my result set. Can you help? I believe I need to add a condition into my join in order to include those codes with nvarchar datatype but don't know how.
I apologize for such a long script. You may ignore it. The question is up here but wanted to include the query also. I am using SQL Server 2014.
select distinct
prc_code AS REGCLASS,
etp_code AS TYPE,
evt_title AS TITLE,
evt_code AS MEETING,
ea.adr_city AS CITY,
ea.adr_state AS STATE,
ea.adr_country AS COUNTRY,
evt_start_date AS DATE,
case
when net_ivd_amount_cp=0
then 'Comp'
else 'Paid'
end as 'Paid/Comp',
net_ivd_amount_cp as 'Revenue',
ev_registrant.reg_cst_key AS [COUNT],
net_ivd_amount_cp,
chp_name as 'District / National Council',
c03_region_name as 'DC Region',
cst_sort_name_dn as 'Name',
ind_first_name as 'First',
ind_last_name as 'Last',
cst_ixo_title_dn as 'Registrant Title',
cst_org_name_dn as 'Organization',
ca.adr_line1 as 'Address 1',
ca.adr_line2 as 'Address 2',
ca.adr_city as 'Registrant City',
ca.adr_state as 'Registrant State',
ca.adr_post_code as 'Registrant Zip',
ca.adr_country as 'Registrant Country',
cst_phn_number_complete_dn as 'Phone',
cst_eml_address_dn as 'Email',
case
when mem_member_type IS null
then 'Prospect'
else mem_member_type
end as 'Member/Prospect',
mem_join_date as 'Join Date',
reg_attendance_flag as 'Attended',
case
when ind_int_code='Student'
then 'Student'
else org_ogt_code
end as 'Industry',
cr.rgn_code as 'Region'
FROM ev_registrant
join co_customer on reg_cst_key=cst_key
INNER JOIN ev_event on reg_evt_key=evt_key
INNER JOIN ev_event_type ON ev_event.evt_etp_key = ev_event_type.etp_key
left outer join dbo.ac_invoice with (nolock) ON dbo.ev_registrant.reg_inv_code=dbo.ac_invoice.inv_code
LEFT OUTER JOIN dbo.vw_ac_invoice_detail WITH (nolock) ON dbo.ac_invoice.inv_key = net_inv_key
LEFT OUTER JOIN dbo.ac_payment_detail WITH (nolock) ON dbo.ac_payment_detail.pyd_ivd_key = net_ivd_key
LEFT OUTER JOIN dbo.oe_product with (nolock) on dbo.oe_product.prd_key=net_ivd_prc_prd_key
LEFT OUTER JOIN dbo.oe_product_type WITIH (nolock) on prd_ptp_key = ptp_key
LEFT OUTER JOIN dbo.oe_price WITH (nolock) on net_ivd_prc_key = prc_key
left outer join ev_event_location on evl_evt_key=evt_key and evl_primary=1
left outer join ev_location on evl_loc_key=loc_key
left outer join co_customer_x_address ecxa on loc_cxa_key=ecxa.cxa_key
left outer join co_address ea on ecxa.cxa_adr_key=ea.adr_key
left outer join client_uli_dc_region on LEFT(evt_code, 4)=c03_chp_chapter_number
left outer join co_chapter on LEFT(evt_code, 4)=chp_chapter_number
left outer join co_customer_x_address ccxa on cst_cxa_key=ccxa.cxa_key
left outer join co_address ca on ccxa.cxa_adr_key=ca.adr_key
LEFT OUTER JOIN co_country cc on ca.adr_country=cc.cty_code
LEFT OUTER JOIN co_region cr on cr.rgn_key=cc.cty_rgn_key
left outer join vw_client_uli_member_type on cst_key=mem_cst_key
left outer join co_individual_x_organization on ixo_key=cst_ixo_key
left outer join co_organization on ixo_org_cst_key=org_cst_key
left outer join co_individual on cst_key=ind_cst_key
where
((net_ivd_void_flag)=0
Or net_ivd_void_flag Is Null)
AND (reg_delete_flag=0 Or reg_delete_flag Is Null)
AND ((ev_registrant.reg_cancel_date) Is Null
and evt_start_date>='2017-07-01')
and ptp_code='event'
Ok, so now we know that the evt_code is of data type NVARCHAR.
You have two joins which are joining on that field, listed below.
left outer join client_uli_dc_region on LEFT(evt_code, 4)=c03_chp_chapter_number
left outer join co_chapter on LEFT(evt_code, 4)=chp_chapter_number
If either of those columns are of any numerical data type, you'll get the error which you have mentioned. The solution would be to CAST the fields which you join to them, to NVARCHAR

SAP B1 Link AP invoice to PO user

I am trying to generate a query that will return header information for Payment-Hold AP Invoices but also show the username that entered the originating Purchase Order.
Some of my invoices do not have a relationship history so I wouldn't expect any user details to be returned but still need the invoice header information. Others have multiple GRPO entries and are therefore returning 2-3 lines where I only need one. Below is my query.
thanks in advance.
select distinct T0."DocNum"
,T0."DocDate"
,T0."DocType"
,T0."CardCode"
,T0."CardName"
,T0."DocStatus"
,T0."GroupNum"
,T0."DocDueDate"
,T0."DocCur"
,T0."DocTotal"
,T0."DocTotalFC"
,T0."PayBlock"
,T0."PaymentRef"
--,T1."DocEntry"
--,T2."DocEntry"
--,T4."DocNum"
--,T3."ItemCode"
--,IFNULL(T4."UserSign", 0) AS "User_Code"
-- ,T4."UserSign"
,T5."U_NAME" as "Purc_User"
,IFNULL(T5."U_NAME",'') as "PURC_USER"
,'HT' as Entity
,T0."Comments"
FROM OPCH T0
left JOIN PCH1 T1 ON T1."DocEntry" = T0."DocEntry"
left JOIN PDN1 T2 on T1."BaseEntry"= T2."DocEntry" and T1."BaseLine"= T2."LineNum"
left JOIN POR1 T3 on T2."BaseEntry" = T3."DocEntry" and T2."BaseLine" = T3."LineNum"
left JOIN OPOR T4 on T4. "DocEntry" = T3."DocEntry"
left JOIN OUSR T5 on T5."USERID" = T4."UserSign"
where T0."DocStatus" = 'O'
and T0."PayBlock" = 'Y'
order by T0."DocNum"