Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Server bug (Union in subquery)

Status
Not open for further replies.

JRT76

Programmer
Joined
Mar 4, 2010
Messages
2
Location
GB
I have a query where the result doesn't make sense. Please review the query below. If I change Line #4 from demandnumber >= 28500 to 28554 the query result increases from 0 to 30. This is not mathematically possible?

The query also perfoms correctly if I remove the 2nd part of the union in the sub query.



declare @CL int

select @CL = codeid from codes where codetyp = 'BTSTATUS' and coderef = 'CL'

select count(*) from paymentdemands where
demandnumber >= 28550 and
owntyp = 'ME' and ownid in

(select ownid from feedetails fd
join codes fpt on fd.paymenttermsid = fpt.codeid and fpt.coderef = 'DFSA'
left join bankaccounts ba on fd.sourcebankaccid = ba.bankaccid
left join banks b on ba.bankid = b.bankid and b.sortcode = '124267'
left join banktransactions bt on ba.bankaccid = bt.bankaccid and bt.statusid = @CL
where owntyp = 'ME' and bt.banktxnid is null

union

select ownid from feedetails fd
join codes fpt on fd.paymenttermsid = fpt.codeid
where owntyp = 'ME' and fpt.coderef = 'DFSA' and fd.sourcebankaccID is null
)
 
Forgot to mention that the query works fine if i use a temporary table to store the results from the subquery and use a subquery to reference the temporary table values.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top