编辑:以下是我正在使用的数据。我写的查询给了我基于OCCPSTAT字段的重复项(最右边的数据列,C或N)。
我需要一个查询来拉取最接近我定义的日期的记录(代码执行此操作),但是,如果有记录的C和N副本,我只需要N记录。
5000 1000 D001 RNT 100 11/1/2014 0:00 C
5000 1000 D001 GAS 200 11/1/2014 0:00 C
5000 1000 D001 ELC 300 11/1/2014 0:00 C
5000 1000 D001 WAT 400 11/1/2014 0:00 C
5000 1000 D001 TAX 500 11/1/2014 0:00 C
5000 1000 D001 INS 600 11/1/2014 0:00 C
5000 1000 D001 RNT 200 11/1/2015 0:00 C
5000 1000 D001 GAS 300 11/1/2015 0:00 C
5000 1000 D001 ELC 400 11/1/2015 0:00 C
5000 1000 D001 WAT 500 11/1/2015 0:00 C
5000 1000 D001 TAX 600 11/1/2015 0:00 C
5000 1000 D001 INS 700 11/1/2015 0:00 C
5000 1000 D001 RNT 300 11/1/2016 0:00 C
5000 1000 D001 GAS 400 11/1/2016 0:00 C
5000 1000 D001 ELC 500 11/1/2016 0:00 C
5000 1000 D001 WAT 600 11/1/2016 0:00 C
5000 1000 D001 TAX 700 11/1/2016 0:00 C
5000 1000 D001 INS 800 11/1/2016 0:00 C
5000 1000 D001 RNT 500 11/1/2017 0:00 C
5000 1000 D001 GAS 600 11/1/2017 0:00 C
5000 1000 D001 ELC 700 11/1/2017 0:00 C
5000 1000 D001 WAT 800 11/1/2017 0:00 C
5000 1000 D001 TAX 900 11/1/2017 0:00 C
5000 1000 D001 INS 100011/1/2017 0:00 C
5000 1000 D001 RNT 100 11/1/2014 0:00 N
5000 1000 D001 GAS 200 11/1/2014 0:00 N
5000 1000 D001 ELC 300 11/1/2014 0:00 N
5000 1000 D001 WAT 400 11/1/2014 0:00 N
5000 1000 D001 TAX 500 11/1/2014 0:00 N
5000 1000 D001 INS 600 11/1/2014 0:00 N
5000 1000 D001 RNT 200 11/1/2015 0:00 N
5000 1000 D001 GAS 300 11/1/2015 0:00 N
5000 1000 D001 ELC 400 11/1/2015 0:00 N
5000 1000 D001 WAT 500 11/1/2015 0:00 N
5000 1000 D001 TAX 600 11/1/2015 0:00 N
5000 1000 D001 INS 700 11/1/2015 0:00 N
5000 1000 D001 RNT 300 11/1/2016 0:00 N
5000 1000 D001 GAS 400 11/1/2016 0:00 N
5000 1000 D001 ELC 500 11/1/2016 0:00 N
5000 1000 D001 WAT 600 11/1/2016 0:00 N
5000 1000 D001 TAX 700 11/1/2016 0:00 N
5000 1000 D001 INS 800 11/1/2016 0:00 N
5000 1000 D001 RNT 500 11/1/2017 0:00 N
5000 1000 D001 GAS 600 11/1/2017 0:00 N
5000 1000 D001 ELC 700 11/1/2017 0:00 N
5000 1000 D001 WAT 800 11/1/2017 0:00 N
5000 1000 D001 TAX 900 11/1/2017 0:00 N
5000 1000 D001 INS 1000 11/1/2017 0:00 N
查询是:
SELECT
BLDGID,
LEASID,
SUITID,
INCCAT,
AMOUNT,
EFFDATE,
OCCPSTAT
FROM(
SELECT
CM.BLDGID,
CM.LEASID,
LS.SUITID,
CM.INCCAT,
CM.AMOUNT,
CM.EFFDATE,
LS.OCCPSTAT,
ROW_NUMBER() OVER(PARTITION BY CM.BLDGID, CM.LEASID, LS.SUITID, CM.INCCAT, LS.OCCPSTAT
ORDER BY CM.EFFDATE DESC) AS rowno
FROM SQLDATA.DBO.CMRECC CM INNER JOIN SQLDATA.DBO.LEAS LS ON CM.LEASID = LS.LEASID
WHERE (CM.EFFDATE < '20170601') AND (CM.BLDGID = '85000') AND (LS.OCCPSTAT = 'C' OR LS.OCCPSTAT = 'N')
) AS d
WHERE rowno = 1
答案 0 :(得分:1)
您不需要case
。一种方法是只跟踪每个组的occpstat
列,然后在where
中应用逻辑。
但是,我认为有一个更简单的解决方案。将occpstat
从partition by
移至order by
并首先选择“N”值:
SELECT BLDGID, LEASID, SUITID, INCCAT, AMOUNT, EFFDATE, OCCPSTAT
FROM (SELECT CM.BLDGID, CM.LEASID, LS.SUITID, CM.INCCAT
CM.AMOUNT, CM.EFFDATE, LS.OCCPSTAT,
ROW_NUMBER() OVER (PARTITION BY CM.BLDGID, CM.LEASID, LS.SUITID, CM.INCCAT
ORDER BY LS.OCCPSTAT DESC, CM.EFFDATE DESC) AS rownum
FROM SQLDATA.DBO.CMRECC CM INNER JOIN
SQLDATA.DBO.LEAS LS
ON CM.LEASID = LS.LEASID
WHERE CM.EFFDATE < '20170601' AND CM.BLDGID = '85000' AND
LS.OCCPSTAT IN ('C', 'N')
) d
WHERE rownum = 1