0

I have a query that yields inconsistent results. I receive a different number of rows almost every time I run the query. I receive no errors. I have tried running the query from all of the servers in the query. I also tried running the query from a fourth unrelated server that is linked to both servers in the query. I tried running each of the CTEs on its own and always get consistent results. Only the last part of the query yields inconsistent results.

Does anyone know what could be causing these inconsistent results? Thanks!

 with customerOrderMatches as (
  select
 SAPOX.docentry
,SAPO.cardcode as 'O CC'
,SAPCMS.CardCode+'-'+SAPCMS.Address as 'OrigShipToID'
,SAPCMB.CardCode+'-'+SAPCMB.Address as 'OrigCustID'
from [Server1].[BowDB].[dbo].ORDR SAPO
join [Server1].[BowDB].[dbo].RDR12 SAPOX
on SAPO.docentry=SAPOX.docentry
left outer join [Server1].[BowDB].[dbo].CRD1 SAPCMS --ship to match
on SAPCMS.cardcode=SAPO.cardcode
and SAPCMS.Address=SAPO.Shiptocode
and SAPCMS.AdresType='S'
and SAPCMS.Street=SAPOX.StreetS
left outer join [Server1].[BowDB].[dbo].CRD1 SAPCMB --bill to match
on SAPCMB.cardcode=SAPO.cardcode
and SAPCMB.Address=SAPO.PayToCode
and SAPCMB.AdresType='B'
and SAPCMB.Street=SAPOX.StreetB
where 
SAPO.cardcode NOT IN (
'1001', '1002', '1003'
)
AND SAPO.canceled = 'N'
),

customerRank as (
SELECT 
    rtrim(C.custid) AS 'custid'
    ,COUNT(SLShipper.ShipperID) as 'totalShippers'
    ,Row_number() OVER (ORDER BY COUNT(SLShipper.ShipperID) DESC) AS 'customerRank'
FROM [MLSQL12].[SLapplication15].dbo.customer C
left outer join [MLSQL12].[SLapplication15].dbo.SOShipHeader SLShipper 
    on SLShipper.custid=C.custid
GROUP BY C.CustID
),

customerShipToRank as (
SELECT 
rtrim(SOA.CustID) AS 'custid'
,rtrim(SOA.ShiptoID) as 'shiptoid'
,COUNT(SLShipper.ShipperID) as 'totalShippers'
,cast(Row_number() OVER(Partition by SOA.custid ORDER BY COUNT(SLShipper.ShipperID) DESC) as int) AS 'ShipToRank'
,customerRank
FROM [MLSQL12].[SLapplication15].dbo.soaddress SOA
left outer join [MLSQL12].[SLapplication15].dbo.SOShipHeader SLShipper
on SLShipper.CustID=SOA.CustId
and SLShipper.ShiptoID=SOA.shiptoid
join customerRank CR
on CR.custid=SOA.CustID
GROUP BY 
SOA.CustID
,SOA.ShiptoID
,customerRank
),

combinedData as (
select
COM.Docentry
,CXR.*
,CSTR.*
from customerOrderMatches COM
join MLSQL15.HistoricalData.Hist.CustomerXRef CXR
on CXR.OrigShipToID=COM.OrigShipToID collate SQL_Latin1_General_CP850_CI_AS
and CXR.OrigCustID=COM.OrigCustID collate SQL_Latin1_General_CP850_CI_AS
left outer join customerShipToRank CSTR
on CSTR.shiptoid =CXR.BKShiptoId 
and CSTR.custid =CXR.BKCustId 
)



select 
*
from combinedData CD
where CONCAT(customerRank,ShipToRank) in (
select MIN(CONCAT(customerRank,ShipToRank))
from combinedData
group by docentry)
order by docentry

Other random facts about the situation: -I realize that there are probably inefficiencies in my query that could be optimized. However, this should not result in inconsistent results. -One database is an SAP DB. -One DB is a Microsoft Dynamics SL DB. -One DB is our own DB we created for acquisition data.

Update (12/12/2022) One of the columns returned is a basic primary key of an order in an order table. I ran the query six times and got the following results:

Query Run Number Total Rows Lowest DocEntry Highest DocEntry
1 14509 9 31412
2 14509 9 31412
3 5455 105 31408
4 5448 108 31411
5 14509 9 31412
6 5181 105 31411
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • 3
    Your question as it is right now is practically impossible for someone to help. You don't show any data example to demonstrate what inconsistencies you are talking about. There is no reference to any of your tables and database structure. There are 5 different queries in your code without any of these context, are we suppose to guess what is wrong or are you expecting that someone might have the exact same queries you do on their SAP setup? That's very, very, very improbable. – Jorge Campos Dec 12 '22 at 22:29
  • 2
    Please read this post and edit your question accordingly: https://meta.stackoverflow.com/questions/271055/tips-for-asking-a-good-structured-query-language-sql-question – Jorge Campos Dec 12 '22 at 22:30
  • Hi Jorge, thank you for this comment. While I read through the post I will mention that the data is inconsistent every time. I didn't think that example data would be helpful. The data within any given row is accurate if it is returned. However, the number of rows returned is not reliable. I suspect something is silently failing but am not sure where to check for that possibility. I realize this post is a bit of a Hail Mary, but I am confident I will figure it out. Stay tuned! Thank you :) – Brendan Lesinski Dec 12 '22 at 23:37
  • 2
    You seem to use MS SQL Server, but you don't say which "SAP DB" it is: another MS SQL Server or SAP HANA? – Sandra Rossi Dec 13 '22 at 13:35
  • 1
    Doing cross-server joins can be very expensive. As someone else suggested, try and limit your data to what you need first, possibly by doing a SELECT cols INTO #SomeTempTable first and then performing your rank and join operations. It's been 84 years since I've done such cross server joins directly without first limiting the data as, at least back then, I found SQL Server would ship all the data prior to performing the join. – Alan Samet Dec 13 '22 at 18:49

0 Answers0