| Author |
Topic |
|
sreenu9f
Yak Posting Veteran
73 Posts |
Posted - 2008-12-11 : 16:05:09
|
| I have this unique situationI have two tables X and Y both with the data elements BillingID,Balance,PeriodEndDate for exTable XBillingID,Balance,PeriodEndDate,pr123,50,10/31/08,1123,-150,10/31/08,2456,100,10/31/08,1123,100,11/31/08,2789,100,11/31/08,1Table YBillingID,Balance,PeriodEndDate,pr123,200,10/31/08,2432,100,11/31/08,2498,200,10/31/08,1498,100,10/31/08,2The final result i need is distinct BillingID from Table X which are not there in Table Y for the same periodEndDate(for ex 10/31/07 for both table x and table Y) and then group by BillingID and having sumofthier balances in a given table >0.I tried alot but not sucessfull. Please suggest.Thanks, |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-12-11 : 17:04:48
|
| select billindID from Xwhere not exists (select 1 from Y where x.billingID = y.billingID and x.periodEnddate = y.periodenddate)group by billindID having sum(balance) > 0 |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2008-12-11 : 18:29:38
|
quote: Originally posted by hanbingl select billindID from Xwhere not exists (select 1 from Y where x.billingID = y.billingID and x.periodEnddate = y.periodenddate)group by billindID having sum(balance) > 0
This will be faster than Left join with larger resultsets. |
 |
|
|
sreenu9f
Yak Posting Veteran
73 Posts |
Posted - 2008-12-11 : 18:53:32
|
| Thanks Hanbingl & SodeepI am confused what does select 1 in where not exists (select 1 from Y.............. do? is it 1 or something else. Please let me know.THanks, |
 |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2008-12-12 : 03:38:20
|
| select 1 from Y where x.billingID = y.billingID and x.periodEnddate = y.periodenddateIf there are any records in your table for the combination of billingid and periodenddateFor ex let us say that there are 2 records in your table for the above combination Then the o/p is11It simply returns 1 thats it.Jai Krishna |
 |
|
|
sreenu9f
Yak Posting Veteran
73 Posts |
Posted - 2008-12-12 : 09:47:00
|
| Thanks jaikrishna for the clarification.i have an other issue now the periodenddate should be same for both the tables like previously and should run the query for a specific date and when added an other where condition X.Periodenddate = '10/31/08' it is not giving a right number of records.can u suggest.Thanks, |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-12 : 09:50:47
|
| [code]select billindID from Xwhere not exists (select 1 from Y where x.billingID = y.billingID and x.periodEnddate = y.periodenddate and Periodenddate = '10/31/08')group by billindID having sum(balance) > 0[/code] |
 |
|
|
sreenu9f
Yak Posting Veteran
73 Posts |
Posted - 2008-12-12 : 10:30:56
|
| Thanks Visakh; but still the resultset is huge it is giving only for the date selected but it is doing it for every date in the table. like i have for 10/31/08 in Table X a total of 47,000 records and for the same date table Y has 15,000 records. so the result set should not exceed 47,000 right? but using the above listed query it is resulting 459,832 records.Please suggest. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-12 : 10:35:10
|
| so you want only those records from your main table which are on 10/31/08 and does not have corresponding record on second table? |
 |
|
|
sreenu9f
Yak Posting Veteran
73 Posts |
Posted - 2008-12-12 : 10:40:34
|
| Yes; but the period end date should be 10/31/08 in the table Y too.to summarize i need the records from table x which are not present in table y where the periodenddate on both the tables is same and for a specific periodenddate (like 10/31/08) then group by billingid on table x with a having sum(balance) > 0.Thanks for all ur help. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-13 : 00:57:10
|
| [code]select x.billindID from Xleft join YON x.billingID = y.billingID and x.periodEnddate = y.periodenddateand y.Periodenddate = '10/31/08'WHERE y.billingID IS NULLgroup by x.billindID having sum(x.balance) > 0[/code] |
 |
|
|
|