SQL选择小于特定日期的最大日期

时间:2018-09-04 18:55:31

标签: sql date ms-access max

我试图在Access中进行查询,从表中选择最大日期,该日期小于表中其他特定记录的日期,我必须检查该记录是否存在。

我知道描述有些混乱,所以我将尝试用伪代码进行解释:

If(Record exists where editing_type = "Notranja kontrola")
   SELECT MAX(created_at) that is smaller than ( MIN(created_at) Where 
   editing_type = "Notranja kontrola") 
   WHERE editing_type = "Zajem"

如果不存在带有editing_type =“ Notranja kontrola”的记录,则我不想选择日期。我想返回NULL。 如果是这样,我想选择最大日期,其中editing_type =“ Zajem”,小于MIN(date)WHERE edit_type =“ Notranja kontrola”)

我提出的查询:

SELECT 
spr.ob_id AS Obcina, 
spr.ko, 
'DA' AS Dodeljeno,
(   
SELECT MAX(created_at)
FROM ( 
    SELECT created_at
    FROM spremljanje_uporabnik_zajem
    WHERE created_at < (
        SELECT MIN(created_at)
        FROM spremljanje_uporabnik_zajem
        WHERE editing_type = "Notranja kontrola" AND ko = spr.ko AND VAL(ko) BETWEEN  0 AND 9999
        )
    )   
WHERE editing_type = "zajem" AND ko = spr.ko AND VAL(ko) BETWEEN  0 AND 9999
    AND EXISTS (
        SELECT 1
        FROM spremljanje_uporabnik_zajem
        WHERE editing_type = "Notranja kontrola" AND ko = spr.ko AND VAL(ko) BETWEEN  0 AND 9999
    )
) AS [krneki],

(SELECT
.....


)

FROM spremljanje_uporabnik_zajem AS spr

WHERE VAL(ko) BETWEEN  0 AND 9999

GROUP BY spr.ob_id, spr.ko

ORDER BY Val(spr.ko);

WHERE VAL(ko) BETWEEN 0 AND 9999部分仅用于检查条目是否有效。

当我尝试运行查询时,出现提示要求我输入spr.ko,而不是将其作为外部查询的值。 我该如何解决?

存在外部查询是因为我也需要选择其他一些东西。

EDIT + EDIT2: 我只能从表格“ spremlanje_uporabniki_zajem”中选择

样本数据(spremlanje_uporabniki_zajem):

    ob_id   ko     editing_type        created_at
    40      100       zajem             1.01.2018
    40      100       zajem             2.01.2018
    40      100  notranja kontrola      3.01.2018
    40      100       zajem             4.01.2018
    90      550       zajem             2.01.2018
    40      100       zajem             6.01.2018
    40      20        zajem             7.01.2018
    40      20        zajem             8.01.2018
    40      20   notranja kontrola      9.01.2018
    40      20        zajem            10.01.2018
    40      100       zajem            11.01.2018
    40      20        zajem            12.01.2018
    40      20  notranja kontrola      13.01.2018
    40     100  notranja kontrola      13.01.2018


Expected REXPECTED RESULT:      


    ob_id    ko   Dodeljeno     krneki
      40    100      DA       2.01.2018
      90    550      DA          NULL
      40    20       DA       8.01.2018

谢谢

4 个答案:

答案 0 :(得分:0)

也许您可以进行此查询,然后自己完成其余的操作。

select
  t3.ob_id,
  t3.ko,
  t4.max_created_at
from(
select distinct
  ob_id,
  ko
from dummyTable
) t3
left join (
SELECT
  t1.ob_id,
  t1.ko,
  max(t1.created_at) max_created_at
from dummyTable t1
where exists (
  select
    1
  from dummyTable t2
  where t2.ob_id = t1.ob_id and
  t2.ko = t1.ko and
  t2.editing_type = 'nontranja kontrola' and
  t1.created_at < t2.created_at
)) t4
on t4.ob_id = t3.ob_id and
t4.ko = t3.ko

您可以尝试here

答案 1 :(得分:0)

  • 首先,您需要为每个组计算MIN(date)
  • 然后您执行左联接以获取日期早于找到的MIN(date)的行
  • 现在您将所有这些行分组在一起
  • 使用条件MAX(CASE)删除具有空值的日期。

SQL DEMO

WITH cte as (
      SELECT  "ob_id", "ko", MIN("created_at") min_kontrola
      FROM Table1 
      WHERE "editing_type" = 'notranja kontrola'
      GROUP BY "ob_id", "ko"
)  
SELECT t."ob_id", 
       t."ko", 
       MAX(CASE WHEN c.min_kontrola IS NOT NULL 
                THEN t."created_at"
           END) as krneki
FROM Table1 t 
LEFT JOIN cte c
  ON t."ob_id" = c."ob_id"
 AND t."ko" = c."ko"
 AND t."created_at" < c.min_kontrola
GROUP BY t."ob_id", t."ko" 
ORDER BY  t."ob_id", t."ko";

输出

| ob_id |  ko |        max |
|-------|-----|------------|
|    40 |  20 | 2018-01-08 |
|    40 | 100 | 2018-01-02 |
|    90 | 550 |     (null) |

在SQL DEMO中,您有两个查询。第二个我删除了GROUP BY,因此您可以在最后一步之前查看结果。

答案 2 :(得分:0)

了解“ NULL要求”后,我不得不修改答案。您的查询还算不错,但是我发现有一些需要改进/更正的地方:

  • “ zajem WHERE子句”不得为WHERE子句,而应为下一个内部级别的WHERE子句的附加条件。
  • 可以删除“ EXISTS条件”,因为当不存在任何记录时,也不存在MIN,因此与任何“ zajem记录”的比较将失败,这将导致“无最大值”。 “(NULL)。
  • 不需要“ zajem子查询”,条件可以直接在“ MAX查询”中应用。

结果如下:

SELECT 
    spr.ob_id AS Obcina
  , spr.ko
  , 'DA' AS Dodeljeno
  , (   
        SELECT MAX(created_at)
        FROM spremljanje_uporabnik_zajem
        WHERE editing_type = "zajem" AND ko = spr.ko AND VAL(ko) BETWEEN  0 AND 9999
          AND created_at < (
            SELECT MIN(created_at)
            FROM spremljanje_uporabnik_zajem
            WHERE editing_type = "Notranja kontrola" AND ko = spr.ko AND VAL(ko) BETWEEN  0 AND 9999
            )
    ) AS [krneki]
FROM spremljanje_uporabnik_zajem AS spr
WHERE VAL(ko) BETWEEN  0 AND 9999
GROUP BY spr.ob_id, spr.ko
ORDER BY VAL(spr.ko);

Access可以在没有任何提示的情况下运行它:

query result

答案 3 :(得分:0)

解决方案,以防万一有人感兴趣(从@Juan Carlos Oropeza帖子修改-用子查询替换CT​​E并使用MS Access语法)

SELECT 
    t.ob_id, 
    t.ko, 
    MAX(iif(c.min_kontrola IS NOT NULL,t.created_at)) as krneki,

FROM spremljanje_uporabnik_zajem t

LEFT JOIN 
    (
    SELECT  ob_id, ko, MIN(created_at) AS min_kontrola
    FROM spremljanje_uporabnik_zajem 
    WHERE editing_type = 'notranja kontrola'
    GROUP BY ob_id, ko
    )AS c

ON (t.ob_id = c.ob_id
AND t.ko = c.ko
AND (t.created_at < c.min_kontrola)
AND VAL(t.ko) BETWEEN 0 AND 9999
AND t.editing_type = "zajem"
)
GROUP BY t.ob_id, t.ko 
ORDER BY  t.ob_id, t.ko;