简体   繁体   中英

SSRS 2012: How do I return the most recent rows only?

Okay, here is my current query:

SELECT pk.RxFill.PATIENT_PRICE
,PersonQuotas.QuotaYear
,PersonQuotas.QuotaMonth
(and a bunch of other stuff to link them)
FROM
pk.RxMain
INNER JOIN pk.Doctor
ON pk.RxMain.PHARMACY_ID = pk.Doctor.PHARMACY_ID AND pk.RxMain.DOCTOR_ID = pk.Doctor.DOCTOR_ID
INNER JOIN pk.Formula
ON pk.RxMain.PHARMACY_ID = pk.Formula.PHARMACY_ID AND pk.RxMain.FORMULA_ID = pk.Formula.FORMULA_ID
INNER JOIN pk.Patient
ON pk.RxMain.PHARMACY_ID = pk.Patient.PHARMACY_ID AND pk.RxMain.PATIENT_ID = pk.Patient.PATIENT_ID
INNER JOIN pk.RxFill
ON pk.RxMain.PHARMACY_ID = pk.RxFill.PHARMACY_ID AND pk.RxMain.RXMAIN_ID = pk.RxFill.RXMAIN_ID
INNER JOIN pk.Insurance
ON pk.RxFill.PHARMACY_ID = pk.Insurance.PHARMACY_ID AND pk.RxFill.INSURANCE_ID = pk.Insurance.INSURANCE_ID
INNER JOIN PersonsRepId
ON pk.Doctor.PHARMACY_ID = PersonsRepId.PharmacyId AND pk.Doctor.SALES_PERSON_ID = PersonsRepId.SalesRepId
INNER JOIN Persons
ON PersonsRepId.PersonId = Persons.PersonId
INNER JOIN PersonQuotas
ON Persons.PersonId = PersonQuotas.PersonId
WHERE
Year(pk.RxFill.FILL_DATE) >= Year(GetDate())

I'm only really interested in the most recent QuotaYear/QuotaMonth combination, and our records database keeps returning RXFILL rows for every QuotaYear/QuotaMonth combination since we've started keeping track of them in SQL (so, I get a row for 2014/4, 2014/5, 2014/6, etc.).

To further complicate matters, the current year/month combination may not exist in my table (right now, for instance, no one has entered quotas for this current month), so I need to essentially be able to both identify the most recent year/month (both are entered as Integers) and pull only those rows (so that I can do other calculations).

How would I do this -- EDIT: in such a way that I wouldn't have to know what the most recent month is at run-time?

I'm taking a stab at this, there may be something more optimal. But basically, use a CTE to filter the year/date, and join on it.

with cte1 as (
    select top 1 max(QuotaYear) as QuotaYear, QuotaMonth
    from PersonQuotas
    group by QuotaMonth
    order by QuotaYear desc, QuotaMonth desc
)
SELECT pk.RxFill.PATIENT_PRICE
,PersonQuotas.QuotaYear
,PersonQuotas.QuotaMonth
--(and a bunch of other stuff to link them)
FROM
pk.RxMain
INNER JOIN pk.Doctor
ON pk.RxMain.PHARMACY_ID = pk.Doctor.PHARMACY_ID AND pk.RxMain.DOCTOR_ID = pk.Doctor.DOCTOR_ID
INNER JOIN pk.Formula
ON pk.RxMain.PHARMACY_ID = pk.Formula.PHARMACY_ID AND pk.RxMain.FORMULA_ID = pk.Formula.FORMULA_ID
INNER JOIN pk.Patient
ON pk.RxMain.PHARMACY_ID = pk.Patient.PHARMACY_ID AND pk.RxMain.PATIENT_ID = pk.Patient.PATIENT_ID
INNER JOIN pk.RxFill
ON pk.RxMain.PHARMACY_ID = pk.RxFill.PHARMACY_ID AND pk.RxMain.RXMAIN_ID = pk.RxFill.RXMAIN_ID
INNER JOIN pk.Insurance
ON pk.RxFill.PHARMACY_ID = pk.Insurance.PHARMACY_ID AND pk.RxFill.INSURANCE_ID = pk.Insurance.INSURANCE_ID
INNER JOIN PersonsRepId
ON pk.Doctor.PHARMACY_ID = PersonsRepId.PharmacyId AND pk.Doctor.SALES_PERSON_ID = PersonsRepId.SalesRepId
INNER JOIN Persons
ON PersonsRepId.PersonId = Persons.PersonId
INNER JOIN PersonQuotas
ON Persons.PersonId = PersonQuotas.PersonId
WHERE
--Year(pk.RxFill.FILL_DATE) >= Year(GetDate())
PersonQuotas.QuotaYear = cte1.QuotaYear
and PersonQuotas.QuotaMonth = cte1.quotaMonth
WITH Ordinal (PersonID, QuotaYear, QuotaMonth, TRxAmount, QRxAmount, Ord) AS
(
SELECT PersonQuotas.PersonID, PersonQuotas.QuotaYear, PersonQuotas.QuotaMonth, PersonQuotas.TRxAmount, PersonQuotas.QRxAmount,
    ROW_NUMBER() OVER (PARTITION BY PersonID ORDER BY QuotaYear DESC, QuotaMonth DESC)
FROM PersonQuotas
)
SELECT
pk.RxMain.PHARMACY_ID AS [RxMain PHARMACY_ID]
[STUFF FROM OTHER DATA TABLES]
,Ordinal.PersonID AS [Ordinal PersonID]
,Ordinal.QuotaYear
,Ordinal.QuotaMonth
,Ordinal.TRxAmount
,Ordinal.QRxAmount
FROM
pk.RxMain
INNER JOIN pk.Doctor
ON pk.RxMain.PHARMACY_ID = pk.Doctor.PHARMACY_ID AND pk.RxMain.DOCTOR_ID = pk.Doctor.DOCTOR_ID
INNER JOIN pk.Formula
ON pk.RxMain.PHARMACY_ID = pk.Formula.PHARMACY_ID AND pk.RxMain.FORMULA_ID = pk.Formula.FORMULA_ID
INNER JOIN pk.Patient
ON pk.RxMain.PHARMACY_ID = pk.Patient.PHARMACY_ID AND pk.RxMain.PATIENT_ID = pk.Patient.PATIENT_ID
INNER JOIN pk.RxFill
ON pk.RxMain.PHARMACY_ID = pk.RxFill.PHARMACY_ID AND pk.RxMain.RXMAIN_ID = pk.RxFill.RXMAIN_ID
INNER JOIN pk.Insurance
ON pk.RxFill.PHARMACY_ID = pk.Insurance.PHARMACY_ID AND pk.RxFill.INSURANCE_ID = pk.Insurance.INSURANCE_ID
INNER JOIN PersonsRepId
ON pk.Doctor.PHARMACY_ID = PersonsRepId.PharmacyId AND pk.Doctor.SALES_PERSON_ID = PersonsRepId.SalesRepId
INNER JOIN Persons
ON PersonsRepId.PersonId = Persons.PersonId
INNER JOIN Ordinal
ON Persons.PersonId = Ordinal.PersonId
WHERE
( pk.RxFill.FILL_DATE >= dateadd(year, datediff(year, 0, getdate()), 0) AND
Ordinal.Ord = 1 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM