SQL - DELETE SELECT特殊列

时间:2016-09-13 11:39:29

标签: sql oracle select sql-delete

我有这个声明用于在表格中找到最大值(日期)。

SELECT * 
from (
  select 
  max(TIME) over (partition by artnumber) as "maxTIME",
  artnumber,
  TIME
  from ADE_SPOCK_REP_PRIS
  )
where TIME not like "maxTIME"
order by artnumber desc

如何在此声明前设置删除?

Delete from art where artnumber in 
    (SELECT * 
    from (
      select 
      max(TIME) over (partition by artnumber) as "maxTIME",
      artnumber,
      TIME
      from art
      )
    where TIME not like "maxTIME"
    order by artnumber desc)

这不起作用。 你有什么提示吗?

2 个答案:

答案 0 :(得分:0)

您问题的具体答案是修复子查询中的select *

Delete from art
where artnumber in (select artnumber
                    from (select max(TIME) over (partition by artnumber) as "maxTIME",
                                 artnumber, TIME
                          from art
                         )
                    where TIME not like "maxTIME"
                   );

其他一些评论:

  • 在子查询中使用order by通常没有意义。
  • 我建议不要将like与日期和时间数据类型一起使用(需要使用本地化设置将值转换为字符串)。
  • 据我所知,此版本将删除所有内容,因为每个artnumber都有最长时间。我怀疑这是你的意图。

答案 1 :(得分:0)


尝试使用此查询

DELETE FROM art WHERE MAX(time) IN
(SELECT MAX(time) from art
GROUP BY MAX(time))
WHERE TIME NOT LIKE "%maxTIME%" GROUP BY MAX(time);