简体   繁体   English

在 SQL 中排除某个日期之前的任何内容

[英]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.我正在尝试在 Confirm On demand 中构建一个小部件,该小部件在某个日期之后以堆叠列格式显示所有已检查和未检查的资产。 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:这是我的 SQL 语句:

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.您应该使用ROW_NUMBER() ,但很难说在哪里或如何使用。 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.我使用了日期的feature表,但同样,这只是一个猜测。

Also, this will work for most RDBMS, but not all, so it really depends on it.此外,这适用于大多数 RDBMS,但不是全部,因此它确实取决于它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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