Question : SQL Simple syntax issue - based on Group By / Having



I have table name : PHARMACYORDERS which has Column Name PharmacyOrderId

Table 2 :- PHARMACYORDERDETAILS which has LASTFILLEDDATE Column

Both these tables are related by PharmacyOrderId.

Members  ---> PharmacyOrders ---> PharmacyOrderDetails

MemberId          MemberId,PharmacyOrderId         PharmacyOrderId

I need to fetch Most recent or Top 1 Descending PharmacyOrder Id.
And
For every PharmacyOrderId we have atleast more than one LastFilledDate Record.

In general, i need the output of Maximum PharmacyOrderId for a Member and his most top 1 LastFilledDate Record.

I tried .. group / having .. which is not giving me exact result


1:
2:
3:
4:
5:
6:
SELECT		MEM.FIRSTNAME,MEM.LASTNAME,POD.LASTFILLEDDATE 
FROM		PHARMACYORDERDETAILS POD
INNER JOIN	PHARMACYORDERS PO
ON			PO.PHARMACYORDERID = POD.PHARMACYORDERID
INNER JOIN	MEMBERS MEM
ON			MEM.MEMBERID = PO.MEMBERID

Answer : SQL Simple syntax issue - based on Group By / Having

Umm.. oops? :)
You're right angelIII
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
SELECT          FIRSTNAME,LASTNAME,LASTFILLEDDATE
FROM
(
	SELECT          MEM.FIRSTNAME,MEM.LASTNAME,POD.LASTFILLEDDATE,
					RN=ROW_NUMBER() over (PARTITION BY MEM.MEMBERID ORDER BY PO.PHARMACYORDERID DESC, POD.LASTFILLEDDATE  DESC)
	FROM            PHARMACYORDERDETAILS POD
	INNER JOIN      PHARMACYORDERS PO
	ON                  PO.PHARMACYORDERID = POD.PHARMACYORDERID
	INNER JOIN      MEMBERS MEM
	ON                  MEM.MEMBERID = PO.MEMBERID
) SQ
WHERE RN=1
Random Solutions  
 
programming4us programming4us