I need to write a query to fetch data of item code and max date along with its corresponding column values lies in that row.
I tried below code and it gives mupltiple rows for each item code.I want only one row
for each itemcode along with max date and other column data.
any help would be greatfull
query output:
batch_No,bill_No,bill_Date,srno,item_Code
'GG','123','2019-08-19',39,'000365'
'AL','1217','2017-06-17',53,'000211'
'AL','123','2019-08-19',64,'000211'
'AL','12h55','2019-09-03',75,'000211'
'GG','1df4','2019-09-01',36,'000365'
'AL','123','2019-08-19',35,'000211'
'AL','123','2019-08-19',78,'000211'
'AL','123','2019-08-19',79,'000211'
'AL','123fz','2019-09-02',76,'000211'
'AL','123','2019-08-19',80,'000211'
'AL','123','2019-08-19',82,'000211'
now it might be only two item codes its just smaple data some times it may conatin 5 or 10 itemcodes please suggest consider this point
what i want from this query is just one row based on maximum date for itemcode
'GG','123','2019-08-19',39,'000365'
'AL','12h55','2019-09-03',75,'000211'
What I have tried:
select purdet.c_batch_no batch_No,purmst.c_bill_no bill_No,max(purmst.d_bill_date) bill_Date,purmst.n_srno srno,purdet.c_item_code item_Code
from pur_mst purmst
join pur_det purdet on purdet.c_br_code=purmst.c_br_code and purdet.c_year=purmst.c_year
and purmst.c_prefix=purdet.c_prefix and purdet.n_srno=purmst.n_srno
where purdet.c_item_code + '/' + purdet.c_batch_no in('000211/AL','000365/GG')
group by purdet.c_batch_no,purmst.c_bill_no,purmst.n_srno,purdet.c_item_code