sql server - How to select the 2 newest entries in a SQL database -
i new sql , dont have knowledge on subject yet , have problem. trying select data database tables using query:
select distinct upper(m.name) 'member name', m.memberid 'member id', (b.booktitle + ' / ' + a.firstname + ' ' + a.lastname) 'title', l.barcode 'barcode', (convert(varchar(8), lr.duedate, 5) + ' renewed ' + convert(varchar(1), (select count(lr.loanid) loan l, loanrenewal lr lr.loanid = l.loanid , l.memberid = 's002')) + ' times') 'status', bc.callnumber 'call number' book b, author a, bookauthor ba, bookcopy bc, member m, membertype mt, loan l, loanrenewal lr, publishwork pw m.memberid = 's002' , m.memberid = l.memberid , l.loanid = lr.loanid , l.barcode = bc.barcode , bc.isbn = pw.isbn , pw.bookid = b.bookid , ba.bookid = b.bookid , b.main_authorid = a.authorid
when run this, result returns this
member name | member id | title | barcode | status | call number | lim hai mei | s002 | developing app / ben grimm| 1234567 | 20-02-16 renewed 4 times|tk5105.887 kar| lim hai mei | s002 | java 101 / yee chak thong | abcdefg | 20-02-16 renewed 4 times|tk5105.886 kar| lim hai mei | s002 | java 101 / yee chak thong | abcdefg | 25-02-16 renewed 4 times|tk5105.886 kar| lim hai mei | s002 | java 101 / yee chak thong | abcdefg | 28-02-16 renewed 4 times|tk5105.886 kar|
but want return want recent records , how dynamically isolate data
member name | member id | title | barcode | status | call number | lim hai mei | s002 | developing app / ben grimm| 1234567 | 20-02-16 renewed 1 times|tk5105.887 kar| lim hai mei | s002 | java 101 / yee chak thong | abcdefg | 28-02-16 renewed 4 times|tk5105.886 kar|
please me not find suitable code dynamic , not in anyway hardcoding
you need use group & aggregation on column status(max recent one)
select [member name],[member id],[title],[barcode],max(status),[call number] ( --your code mentioned in question )as group [member name],[member id],[title],[barcode],[call number]
Comments
Post a Comment