2013-07-03 141 views
0

我對這個網站和SQL一般都不熟悉。我希望你能幫上忙。我創建了以下SQL查詢。我現在想創建一個新表並將查詢結果傳送到這個新表中。基於SELECT語句結果創建表

是否可以將語法添加到以下查詢中以自動創建表格?


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 ......是否有可能從所述下降時間結果我的查詢和數據被添加到我的表中的值就是「Date」。

我真的很感謝你的支持,因爲我有限的SQL知識只能讓我走到目前爲止。

謝謝。

+2

您正在使用哪種RDBMS? MS SQL Server,MySQL等? –

+0

SQL標準將是'create table foo as select ...' –

回答

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 
+0

非常感謝您的幫助。它實現了一個夢想 – user2547340

0

確保你可以INTO子句前選擇直接從

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

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 
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