简体   繁体   中英

Excluding anything before a certain date in SQL

I am trying to build a widget in Confirm Ondemand that displays all our assets that have been inspected and not inspected in a stacked column format, after a certain date. So for example, I would have a column with a location split in two, one colour would be inspected and and another colour would be not inspected. Here is my SQL statement:

SELECT
   feature.site_code,
   feature.plot_number,
   cs.site_name,
   ward.ward_name,
   CASE WHEN feature.survey_date >= to_date('02/10/2017','DD/MM/YYYY') THEN 'Sprayed'
   ELSE 'Not Sprayed' END as spray_staus,
   feature.survey_date

FROM
   feature,
   ward,
   central_site cs


WHERE
   feature.plot_number BETWEEN 400000 AND 400001 AND
   feature.site_code = cs.site_code AND
   ward.ward_code = feature.ward_code AND
    feature.feature_deadflag ='N' 

The problem is, I'm getting EVERY inspection from each asset when I only want the latest one.

What do I need to do???

Yes, your question lacks a lot of information.

You should use ROW_NUMBER() , but it's hard to say where or how. It should be something like (change it to the table you want the latest record from):

FROM
   (SELECT s.*, ROW_NUMBER() OVER(PARTITION BY s.YourGroup ORDER BY s.YourDate
    FROM feature s) feature,
   ward,
   central_site cs
....
  AND feature.rnk = 1

I used feature table for dates, but again, it's only a guess.

Also, this will work for most RDBMS, but not all, so it really depends on it.

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