sql server - SQL: Using Max function and returning max per client, not overall -


i new using sql , attempting build first query/report, , hoping on (this seems place that!). want create report shows when last time employee or contractor paid. have database information, want return distinct list of every person last pay date. end getting either list of every pay have made (person1 on list 20+ times each pay date), or list every person , recent paydate of anyone, not person. here have far:

select  table1.office ,     table1.ee_no ,     table1.name ,     table1.code ,     table1.freq ,     ( select distinct                 max(table2.paydate)            table2     ) last_paycheck    table1     inner join table2 on table1.uniqueid = table2.uniqueid   table1.enddate null 

what returns list of every employee 8/30/2013 listed, last time has got paid, not everyone. doing wrong here max function? i've tried lot of different ways , no luck, must missing obvious here!

you need try like

select table1.office,  table1.ee_no,  table1.name,  table1.code,  table1.freq,  (select distinct max (table2.paydate)  table2 table1.uniqueid = table2.uniqueid ) last_paycheck  table1 on   table1.enddate null 

the other way like

select table1.office,  table1.ee_no,  table1.name,  table1.code,  table1.freq,  max (table2.paydate) last_paycheck  table1 inner join table2 on table1.uniqueid = table2.uniqueid  table1.enddate null group table1.office,  table1.ee_no,  table1.name,  table1.code,  table1.freq 

also note comment states, use more descriptive table names, improve maitainability later.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -