更改 table 以更改时间范围 [WHERE/ALTER]
Altering a table to change time frame [WHERE/ALTER]
编辑:当前 运行 SQuirreL SQL v.3.8.0.
我创建并保存了一个 table,它使用 WHERE
语句在特定时间范围内提取客户数据。我将如何更新时间范围?我尝试使用 ALTER TABLE
函数来更改 WHERE
语句,但收到一条错误消息。
原版table
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')
这给了我以下错误信息:
错误:“*”附近的语法不正确。
SQL状态:S0001
错误代码:102
在大多数 SQL 方言中,您使用的查询基本上是 "Perform this query and store the result into a new table"。新的 table 与用于为其获取数据的查询之间没有任何关系。
您可能真正想要使用的是视图(始终显示给定查询的当前数据 - 认为 "named query")或物化视图(实际上将数据存储在 table 中,但用户可以从查询的新结果触发数据刷新。
听起来您想要截断现有数据并加载(插入)现有 table 结构?
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;
编辑:当前 运行 SQuirreL SQL v.3.8.0.
我创建并保存了一个 table,它使用 WHERE
语句在特定时间范围内提取客户数据。我将如何更新时间范围?我尝试使用 ALTER TABLE
函数来更改 WHERE
语句,但收到一条错误消息。
原版table
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')
这给了我以下错误信息:
错误:“*”附近的语法不正确。
SQL状态:S0001
错误代码:102
在大多数 SQL 方言中,您使用的查询基本上是 "Perform this query and store the result into a new table"。新的 table 与用于为其获取数据的查询之间没有任何关系。
您可能真正想要使用的是视图(始终显示给定查询的当前数据 - 认为 "named query")或物化视图(实际上将数据存储在 table 中,但用户可以从查询的新结果触发数据刷新。
听起来您想要截断现有数据并加载(插入)现有 table 结构?
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;