更改表格以更改时间范围[WHERE / ALTER]

时间:2017-12-19 19:58:50

标签: sql alter-table

编辑:当前正在运行SQuirreL SQL v.3.8.0。

我创建并保存了一个表,该表使用WHERE语句在特定时间范围内提取客户数据。我如何更新时间框架?我尝试使用ALTER TABLE函数更改WHERE语句,但收到错误消息。

原始表

SELECT Customer,
AVG(CAST(ItemPrice AS FLOAT)) AS item_price, 
COUNT(DISTINCT(ReceiptIDDesc)) AS number_of_orders, 
SUM(CAST(ItemPrice AS FLOAT)) AS total_spend
INTO Customer_data_A
FROM Tacos 
Where (CAST(PurchaseDate AS DateTime) >= '01/01/2017 12:00') AND (CAST(PurchaseDate AS DateTime) <= '12/01/2017 12:00')
GROUP BY Customer

修改声明

ALTER TABLE Customer_data_A ALTER COLUMN * WHERE (CAST(PurchaseDate AS DateTime) >= '01/01/2017 12:00') AND (CAST(PurchaseDate AS DateTime) <= '11/01/2017 12:00')

这给了我以下错误消息:

错误:'*'附近的语法不正确。

SQLState:S0001

ErrorCode:102

2 个答案:

答案 0 :(得分:0)

在大多数SQL方言中,您使用的查询基本上是“执行此查询并将结果存储到新表中”。新表与用于获取数据的查询之间没有任何关系。

您可能真正想要使用的是视图(始终显示给定查询的当前数据 - 认为“命名查询”)或物化视图(实际上将数据存储在表中,但用户可以触发刷新数据查询的新结果)。

答案 1 :(得分:0)

听起来您正在寻找TRUNCATE现有数据并加载(INSERT INTO)现有的表结构?

ALTER TABLE + ALTER COLUMN将更多地用于更改数据类型或添加约束(即将INT列更改为NUMERIC或添加外键引用)。

TRUNCATE TABLE Customer_data_A;

INSERT INTO Customer_data_A
(Customer, ItemPrice, Number_Of_Orders, Total_Spend)
SELECT Customer
      ,AVG(CAST(ItemPrice AS FLOAT)) AS item_price
      ,COUNT(DISTINCT(ReceiptIDDesc)) AS number_of_orders
      ,SUM(CAST(ItemPrice AS FLOAT)) AS total_spend
FROM Tacos 
WHERE (CAST(PurchaseDate AS DateTime) >= '01/01/2017 12:00') AND (CAST(PurchaseDate AS DateTime) <= '11/01/2017 12:00')
GROUP BY Customer;