Reputation: 113
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 |
Upvotes: 0
Views: 82