0

我是这个网站和一般 SQL 的新手。我希望你能帮上忙。我创建了以下 SQL 查询。我现在想创建一个新表并将查询结果传输到这个新表中。

是否可以向以下查询添加语法以自动创建表?


SELECT
    Value [Value],
        DateTime [DateTime], 
            DatePart (Week, DateTime) [WeekNumber],
                hs.StorageItemID [StorageItemId]

FROM History_StorageItem hs

    JOIN History_Float hf ON hs.StorageItemId = hf.StorageItemId

GROUP BY DatePart (Week,DateTime), hs.StorageItemId, Value
ORDER BY hs.StorageItemId DESC

此外,与 DateTime 相关的此查询的结果返回 yyyy-mm-dd hh:mm:ss...... 是否可以从我的查询和数据添加到我的表的结果值中删除时间只是“日期”。

我非常感谢您的支持,因为我对 SQL 的有限了解只能带我到此为止。

谢谢。

4

5 回答 5

1

答案可能因 RDBMS 而异。您可以使用INTOSELECT结果创建表,并且可以将其转换DATETIMEDATE

SELECT
    Value [Value],
        CAST (DateTime AS DATE) [DateTime], 
            DatePart (Week, DateTime) [WeekNumber],
                hs.StorageItemID [StorageItemId]
INTO newTable
FROM History_StorageItem hs

    JOIN History_Float hf ON hs.StorageItemId = hf.StorageItemId

GROUP BY DatePart (Week,DateTime), hs.StorageItemId, Value
ORDER BY hs.StorageItemId DESC
于 2013-07-03T15:59:35.640 回答
0

另一方面,如果你想创建一个视图而不是一个表,你可以这样做:

CREATE VIEW [newView] AS
SELECT
Value [Value],
    DateTime [DateTime], 
        DatePart (Week, DateTime) [WeekNumber],
            hs.StorageItemID [StorageItemId]
FROM History_StorageItem hs
JOIN History_Float hf ON hs.StorageItemId = hf.StorageItemId
GROUP BY DatePart (Week,DateTime), hs.StorageItemId, Value
于 2013-07-03T16:06:44.473 回答
0

您应该使用日期格式化方法,这些方法会根据您的 sql server 类型而变化

Postgres

MySQL

甲骨文

于 2013-07-03T16:06:53.397 回答
0

确保您可以 INTO在 FROM 之前对选择进行子句

SELECT Value [Value], DateTime [DateTime], DatePart (Week, DateTime) [WeekNumber], hs.StorageItemID [StorageItemId]
INTO [TABLENAME]
FROM History_StorageItem hs

JOIN History_Float hf ON hs.StorageItemId = hf.StorageItemId
GROUP BY DatePart (Week,DateTime), hs.StorageItemId, Value ORDER BY hs.StorageItemId DESC

我向您推荐这些教程

http://www.w3schools.com/sql/sql_select_into.asp

于 2013-07-03T16:00:32.550 回答
0
create table YOURTABLENAME AS
SELECT
    Value [Value],
        DateTime [DateTime], 
            DatePart (Week, DateTime) [WeekNumber],
                hs.StorageItemID [StorageItemId]

FROM History_StorageItem hs

    JOIN History_Float hf ON hs.StorageItemId = hf.StorageItemId

GROUP BY DatePart (Week,DateTime), hs.StorageItemId, Value
ORDER BY hs.StorageItemId DESC
于 2013-07-03T16:04:25.463 回答