Author |
Topic |
temp2escape
Starting Member
6 Posts |
Posted - 2007-09-08 : 02:50:10
|
Hi. table name is tblpaymentRegNo PaymentDate12 02/12/07 12 12/12/0712 03/05/06 15 12/12/0515 12/07/0715 01/31/0717 01/01/0617 06/09/05The ouput should be like this:RegNo PaymentDate12 03/05/0615 12/12/0517 01/01/06As you can see. It only selects one RegNo followed with its latest PaymentDate excluding PaymentDate with the year 2007.I tried this:SELECT *FROM tblPaymentWHERE (YEAR(PaymentDate) <> 2007) IN(SELECT TOP 1 * FROM tblPayment GROUP BY RegNo HAVING COUNT(*) > 1)ORDER BY PaymentDate DESCBut its not working. Any idea? |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-09-08 : 04:05:49
|
TrySelect Regno, max(paymentdate) as paymentdate from tableWHERE YEAR(PaymentDate) <> 2007MadhivananFailing to plan is Planning to fail |
 |
|
temp2escape
Starting Member
6 Posts |
Posted - 2007-09-08 : 04:14:03
|
nope. not working.show this error.column 'tblpayment.regno' is invalid in the select list because it is not contained in an aggregate function and there is no group by clause. |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-09-08 : 04:16:36
|
Well. I missed group by. Use thisSelect Regno, max(paymentdate) as paymentdate from tableWHERE YEAR(PaymentDate) <> 2007group by RegnoMadhivananFailing to plan is Planning to fail |
 |
|
temp2escape
Starting Member
6 Posts |
Posted - 2007-09-08 : 09:26:50
|
thanks dude. It works. Highly appreciated. |
 |
|
temp2escape
Starting Member
6 Posts |
Posted - 2007-09-08 : 10:49:03
|
Erm... there is one problem left madhivanan.How the exclude entirely those who made payment in the year 2007?For example:RegNo PaymentDate12 02/12/0712 12/12/0712 03/05/06 From the SQL command u gave me... it still shows the payment made for the above RegNo (for year 2006). I mean, since RegNo=12 already made the payment for the year 2007. So, he shouldnt be on the list. |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-09-08 : 11:00:40
|
Try thisSelect Regno, max(paymentdate) as paymentdate from table tWHERE not exists(Select * from table where regno=T.regno and YEAR(PaymentDate) =2007)group by RegnoMadhivananFailing to plan is Planning to fail |
 |
|
temp2escape
Starting Member
6 Posts |
Posted - 2007-09-08 : 11:37:43
|
nope... return no record at all. |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-09-08 : 12:32:06
|
quote: Originally posted by temp2escape nope... return no record at all.
Why?declare @t table(RegNo int, PaymentDate datetime)insert into @tselect 12, '02/12/07' union allselect 12, '12/12/07' union allselect 12, '03/05/06' union allselect 15, '12/12/05' union allselect 15, '12/07/07' union allselect 15, '01/31/07' union allselect 17, '01/01/06' union allselect 17, '06/09/05'Select Regno, max(paymentdate) as paymentdate from @t tWHERE not exists(Select * from @t where regno=T.regno and YEAR(PaymentDate) =2007)group by Regno MadhivananFailing to plan is Planning to fail |
 |
|
temp2escape
Starting Member
6 Posts |
Posted - 2007-09-08 : 18:46:22
|
I dont know. I'm not familiar with the EXIST or NOT EXIST syntax.It didnt show any result.The code should only take out single RegNo followed with their latest payment. But if the RegNo already make the payment for year 2007, it should be exclude entirely from the output.The output should be (based on the table from my 1st post):RegNo PaymentDate17 01/01/06Because RegNo 12 and 15 already make payment for 2007. This is the SQL command you gave me.SELECT RegNo, MAX(PaymentDate) AS paymentdateFROM tblPaymentWHERE (NOT EXISTS(SELECT *FROM tblPaymentWHERE regno = tblpayment.regno AND YEAR(PaymentDate) = 2007))GROUP BY RegNoCorrect me if I am wrong. |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-09-10 : 06:40:45
|
quote: Originally posted by temp2escape I dont know. I'm not familiar with the EXIST or NOT EXIST syntax.It didnt show any result.The code should only take out single RegNo followed with their latest payment. But if the RegNo already make the payment for year 2007, it should be exclude entirely from the output.The output should be (based on the table from my 1st post):RegNo PaymentDate17 01/01/06Because RegNo 12 and 15 already make payment for 2007. This is the SQL command you gave me.SELECT RegNo, MAX(PaymentDate) AS paymentdateFROM tblPaymentWHERE (NOT EXISTS(SELECT *FROM tblPaymentWHERE regno = tblpayment.regno AND YEAR(PaymentDate) = 2007))GROUP BY RegNoCorrect me if I am wrong.
It is becuase you didnt use the alias as I used in my queryRun thisSELECT RegNo, MAX(PaymentDate) AS paymentdateFROM tblPayment tWHERE (NOT EXISTS(SELECT *FROM tblPaymentWHERE regno = t.regno AND YEAR(PaymentDate) = 2007))GROUP BY RegNoMadhivananFailing to plan is Planning to fail |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-10 : 06:48:36
|
And if you wish to utilize any present indexSELECT t.RegNo, MAX(t.PaymentDate) AS PaymentDateFROM @t AS tLEFT JOIN ( SELECT RegNo FROM @t WHERE PaymentDate >= '20070101' AND PaymentDate < '20080101' ) AS e ON e.RegNo = t.RegNoWHERE e.RegNo IS NULLGROUP BY t.RegNoORDER BY t.RegNo E 12°55'05.25"N 56°04'39.16" |
 |
|
|