好的,这是我当前的查询:
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())
我只对最近的QuotaYear / QuotaMonth组合感兴趣,我们的记录数据库会为每个QuotaYear / QuotaMonth组合返回RXFILL行,因为我们已开始在SQL中跟踪它们(所以,我得到2014 / 4,2014 / 5,2014 / 6等等。
为了使问题更加复杂,我的表中可能不存在当前的年/月组合(例如,现在没有人为当前月份输入配额),所以我基本上能够识别最多最近一年/月(两者都以整数形式输入)并仅拉取那些行(以便我可以进行其他计算)。
我该怎么做 - 编辑:这样我就不必知道最近一个月的运行时间是什么?
答案 0 :(得分:0)
我正在捅这个,可能会有一些更优化的东西。但基本上,使用CTE过滤年/日,并加入它。
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
答案 1 :(得分:0)
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 )