【问题标题】:Teradata : Delete n rows from a tableTeradata:从表中删除 n 行
【发布时间】:2017-04-15 06:59:57
【问题描述】:

我想从 Teradata 的表中删除前 n 行或后 n 行。但是我没有得到任何正确的查询。谁能告诉我该怎么做?

【问题讨论】:

  • 定义前n行。表中没有 firstlast 行,它是一组无序的行 :-)
  • 我的意思是说从头到尾没有任何行。
  • 你如何定义开始或最后
  • 举个例子,我想从我的表中删除 1/4 条记录。我该怎么做?
  • delete from mytable where random(1,4) = 1 随机删除大约 25%。但是你为什么要这样做呢?

标签: teradata sql-delete


【解决方案1】:

我没有任何使用 TeraData 的经验,但如果我使用 SQL,我会这样做:

Delete From |TableName| where |the ID column Name|
IN (Select top(n) |the ID column Name| from |TableName|)

例如,如果我有一个客户表,并且该表包含一个 CustomerID 作为主键列和 CustomerName,我想删除前 10 行,我会是这样:

Delete From Customer  where CustomerID
IN (Select top(10) CustomerID  from Customer)

希望对你有帮助

【讨论】:

  • 如果 CustomerID 上没有 ORDER BY,TOP 将返回首先完成的 AMP 行。这意味着每次执行查询时都会产生随机结果。如果在生产环境中盲目运行,这可能会产生不良后果。
  • 上述查询无效,报错:Top N is not supported in sub query
【解决方案2】:

子选择中的前 N ​​个被限制在删除中,而不是在其他上下文中。

所以,创建一个可变表而不是子选择

create volatile table myDel as ( 
   select id from myTable
   sample .25
) with data on commit preserve rows;
delete T
from myTable T, myDel D
where T.id = D.id;

将删除 25% 的数据(顺便说一句,样本有很多选择如何构建样本)

使用 volatile table 和 top n,以及一些订单标准,您可以清楚地定义 top/last 是什么。

create volatile table myDel as ( 
     select top 3 id from myTable order by col_1
) with data on commit preserve rows;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    相关资源
    最近更新 更多