您忘记alias
在查询末尾添加。
您可以通过两种方法做到这一点:
1.如果您已经创建了一个表,那么您可以这样做Insert Into
:
INSERT into foo (CalMonth,CalYear,InvoiceDate,StockCode,QtyInvoiced,Volume)
SELECT * FROM
(
SELECT
DATEPART(MONTH,a.InvoiceDate) as CalMonth
,DATEPART(YEAR,a.InvoiceDate) as CalYear
,a.InvoiceDate
,a.StockCode
,a.QtyInvoiced
,a.Volume
FROM sales a
UNION ALL
SELECT
ds.CalMonth as CalMonth
,ds.CalYear as CalYear
,ds.InvoiceDate
,ds.StockCode
,ds.Cases as QtyInvoiced
,ds.Vol as Volume
FROM sales1 ds
) AS table1
例如看到这个小提琴
2.如果您还没有创建表,那么您可以这样做SELECT * INTO
:
SELECT * INTO foo from
(
SELECT
DATEPART(MONTH,a.InvoiceDate) as CalMonth,
DATEPART(YEAR,a.InvoiceDate) as CalYear,
a.InvoiceDate,
a.StockCode,
a.QtyInvoiced,
a.Volume
FROM sales a
UNION ALL
SELECT
ds.CalMonth as CalMonth,
ds.CalYear as CalYear,
ds.InvoiceDate,
ds.StockCode,
ds.Cases as QtyInvoiced,
ds.Vol as Volume
FROM sales1 ds
) AS table1
例如看到这个小提琴
有关更多参考,请参阅SQL SERVER – 将数据从一个表插入到另一个表 – INSERT INTO SELECT – SELECT INTO TABLE