Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jamie
Aged Yak Warrior
542 Posts |
Posted - 2007-11-28 : 11:58:11
|
| hi, can someone explain to me why these 2 queries produce different counts ?select distinct loan.loanid,person.personid, depositdate, datedue, datediff(dd, depositdate, datedue) as datedifference,issettled, isdefault, isblacklist from loaninner join person on person.personid=loan.personidinner join payment on loan.loanid=payment.loanidwhere depositdate is not nullAND isblacklist=1AND loan.loanid IN (select max(loan.loanid) from loan group by personid)AND payment.paymentid IN(Select Max(paymentid) from payment group by loanid)AND issettled=1ORDER BY isdefault, issettledSELECT DISTINCT person.personId, MAX(loan.loanId) AS loanid, MAX(payment.paymentId) AS paymentid, MAX(loan.depositDate) AS depositdate, MAX(payment.dateDue) AS datedue, DATEDIFF(dd, MAX(loan.depositDate), MAX(payment.dateDue)) AS datedifference,max(convert(int,issettled)), max(convert(int,isdefault)),max(convert(int,isblacklist))FROM loan INNER JOIN person ON person.personId = loan.personId INNER JOIN payment ON loan.loanId = payment.loanIdWHERE (loan.depositDate IS NOT NULL) AND (person.isBlacklist = 1) AND (loan.isSettled = 1)GROUP BY person.personIdshouldn't they basically be the same ?the first query produces alot less.. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-11-28 : 12:06:30
|
| The queries look completely different to me.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
jamie
Aged Yak Warrior
542 Posts |
Posted - 2007-11-28 : 12:09:42
|
| hi Tara,IF I say : AND loan.loanid IN (select max(loan.loanid) from loan group by personid)isn't that saying the biggest loanid for each personid ? |
 |
|
|
anonymous1
Posting Yak Master
185 Posts |
Posted - 2007-11-28 : 12:12:16
|
| the first returns records only when the last loan is a settled loan where AND loan.loanid IN (select max(loan.loanid) from loan group by personid) where AND issettled=1the second returns the last loan record of all the settled loans select MAX(loan.loanId) AS loanid, where AND (loan.isSettled = 1) |
 |
|
|
jamie
Aged Yak Warrior
542 Posts |
Posted - 2007-11-28 : 12:15:07
|
| cheers anon ! |
 |
|
|
jamie
Aged Yak Warrior
542 Posts |
Posted - 2007-11-28 : 12:20:13
|
| query 1 should be :AND loan.loanid IN (select max(loan.loanid) from loan where issettled=1 group by personid) |
 |
|
|
|
|
|
|
|