SQL - 如何将 UNION 查询结果移动到新的 Table?
SQL - How To Move UNION Query Results Into A New Table?
我在下面有一个示例查询,在尝试将其移动到新的 table 之前工作正常:
SELECT CONVERT(DATE,a.ins_timestamp) AS 'Date',
a.Prod_code,
a.Curr_boxes,
a.Label_barcode,
b.From_ord_no,
NULL AS To_ord_no,
CASE
WHEN a.From_batch >= a.To_batch THEN a.From_batch
WHEN a.To_batch >= a.From_batch THEN a.To_batch
ELSE a.From_batch
END AS 'Batch',
a.Weight,
'IN' AS 'Direction'
FROM a
JOIN b ON a.Label_barcode = b.Label_barcode
WHERE (a.ins_timestamp Between ? And ?) AND (a.To_batch = ?) AND (a.From_batch = 0) AND (a.Type='Consumption') AND (a.To_status<>'STOCK') AND (b.From_status = 'PORDER')
GROUP BY CONVERT(DATE,a.ins_timestamp),
a.Prod_code,
a.Curr_boxes,
a.Label_barcode,
b.From_ord_no,
a.From_batch,
a.To_batch,
a.Weight,
a.From_status,
a.To_status
union
SELECT CONVERT(DATE,b.ins_timestamp) AS 'Date',
b.Prod_code,
b.Curr_boxes,
b.Label_barcode,
NULL AS From_ord_no,
NULL AS To_ord_no,
CASE
WHEN b.From_batch >= b.To_batch THEN b.From_batch
WHEN b.To_batch >= b.From_batch THEN b.To_batch
ELSE b.From_batch
END AS 'Batch',
b.Weight,
'IN' AS 'Direction'
FROM b
WHERE (b.From_batch = 0) AND (b.Type='Consumption') AND (b.ins_timestamp Between ? And ?) AND (b.To_batch = ?) AND (b.To_status<>'STOCK')
GROUP BY CONVERT(DATE,b.ins_timestamp),
b.Prod_code,
b.Curr_boxes,
b.Label_barcode,
b.From_batch,
b.To_batch,
b.Weight,
b.From_status,
b.To_status
如何将这两个查询移动到一个新的 table 中?请注意,我在 Excel 电子表格中使用 SQL 查询。
我尝试了以下...但我一直收到错误提示 'Invalid Parameter Number' 和 'Invalid Descriptor Index'
CREATE TABLE temp_UNION AS
(
SELECT CONVERT(DATE,a.ins_timestamp) AS 'Date',
a.Prod_code,
a.Curr_boxes,
a.Label_barcode,
b.From_ord_no,
NULL AS To_ord_no,
CASE
WHEN a.From_batch >= a.To_batch THEN a.From_batch
WHEN a.To_batch >= a.From_batch THEN a.To_batch
ELSE a.From_batch
END AS 'Batch',
a.Weight,
'IN' AS 'Direction'
FROM a
JOIN b ON a.Label_barcode = b.Label_barcode
WHERE (a.ins_timestamp Between ? And ?) AND (a.To_batch = ?) AND (a.From_batch = 0) AND (a.Type='Consumption') AND (a.To_status<>'STOCK') AND (b.From_status = 'PORDER')
GROUP BY CONVERT(DATE,a.ins_timestamp),
a.Prod_code,
a.Curr_boxes,
a.Label_barcode,
b.From_ord_no,
a.From_batch,
a.To_batch,
a.Weight,
a.From_status,
a.To_status
union
SELECT CONVERT(DATE,b.ins_timestamp) AS 'Date',
b.Prod_code,
b.Curr_boxes,
b.Label_barcode,
NULL AS From_ord_no,
NULL AS To_ord_no,
CASE
WHEN b.From_batch >= b.To_batch THEN b.From_batch
WHEN b.To_batch >= b.From_batch THEN b.To_batch
ELSE b.From_batch
END AS 'Batch',
b.Weight,
'IN' AS 'Direction'
FROM b
WHERE (b.From_batch = 0) AND (b.Type='Consumption') AND (b.ins_timestamp Between ? And ?) AND (b.To_batch = ?) AND (b.To_status<>'STOCK')
GROUP BY CONVERT(DATE,b.ins_timestamp),
b.Prod_code,
b.Curr_boxes,
b.Label_barcode,
b.From_batch,
b.To_batch,
b.Weight,
b.From_status,
b.To_status
)
如有任何建议,我们将不胜感激!
谢谢,
乔丹
您可以改用 SELECT INTO,这实际上很容易在 Excel 电子表格中完成。
重要的是要注意,要实际创建一个 'temporary' table,主题标签是命名临时 table 时的关键,即 #newtable(如图所示在下面的代码中)。
在这个问题的情况下实际上需要联合。你可以先 select 你的第一个查询到你的 #newtable 然后,在它被创建之后,你可以 select 你的第二个查询到完全相同的 table.
代码见下方:
-- First create your temp table
SELECT CONVERT(DATE,a.ins_timestamp) AS 'Date',
a.Prod_code,
a.Curr_boxes,
a.Label_barcode,
b.From_ord_no,
NULL AS To_ord_no,
CASE
WHEN a.From_batch >= a.To_batch THEN a.From_batch
WHEN a.To_batch >= a.From_batch THEN a.To_batch
ELSE a.From_batch
END AS 'Batch',
a.Weight,
'IN' AS 'Direction'
INTO #newtable
FROM a
JOIN b ON a.Label_barcode = b.Label_barcode
WHERE (a.ins_timestamp Between ? And ?) AND (a.To_batch = ?) AND (a.From_batch = 0) AND (a.Type='Consumption') AND (a.To_status<>'STOCK') AND (b.From_status = 'PORDER')
-- Now we insert the second query into the already created table
INSERT INTO #newtable
SELECT CONVERT(DATE,b.ins_timestamp) AS 'Date',
b.Prod_code,
b.Curr_boxes,
b.Label_barcode,
NULL AS From_ord_no,
NULL AS To_ord_no,
CASE
WHEN b.From_batch >= b.To_batch THEN b.From_batch
WHEN b.To_batch >= b.From_batch THEN b.To_batch
ELSE b.From_batch
END AS 'Batch',
b.Weight,
'IN' AS 'Direction'
FROM b
WHERE (b.From_batch = 0) AND (b.Type='Consumption') AND (b.ins_timestamp Between ? And ?) AND (b.To_batch = ?) AND (b.To_status<>'STOCK')
-- Now we can select whatever we want from our temp table
SELECT Date,
Prod_code,
Curr_boxes,
Label_barcode,
max(From_ord_no) From_ord_no,
To_ord_no,
Batch,
Weight,
Direction
FROM #newtable
GROUP BY Date,
Prod_code,
Curr_boxes,
Label_barcode,
To_ord_no,
Batch,
Weight,
Direction
我在下面有一个示例查询,在尝试将其移动到新的 table 之前工作正常:
SELECT CONVERT(DATE,a.ins_timestamp) AS 'Date',
a.Prod_code,
a.Curr_boxes,
a.Label_barcode,
b.From_ord_no,
NULL AS To_ord_no,
CASE
WHEN a.From_batch >= a.To_batch THEN a.From_batch
WHEN a.To_batch >= a.From_batch THEN a.To_batch
ELSE a.From_batch
END AS 'Batch',
a.Weight,
'IN' AS 'Direction'
FROM a
JOIN b ON a.Label_barcode = b.Label_barcode
WHERE (a.ins_timestamp Between ? And ?) AND (a.To_batch = ?) AND (a.From_batch = 0) AND (a.Type='Consumption') AND (a.To_status<>'STOCK') AND (b.From_status = 'PORDER')
GROUP BY CONVERT(DATE,a.ins_timestamp),
a.Prod_code,
a.Curr_boxes,
a.Label_barcode,
b.From_ord_no,
a.From_batch,
a.To_batch,
a.Weight,
a.From_status,
a.To_status
union
SELECT CONVERT(DATE,b.ins_timestamp) AS 'Date',
b.Prod_code,
b.Curr_boxes,
b.Label_barcode,
NULL AS From_ord_no,
NULL AS To_ord_no,
CASE
WHEN b.From_batch >= b.To_batch THEN b.From_batch
WHEN b.To_batch >= b.From_batch THEN b.To_batch
ELSE b.From_batch
END AS 'Batch',
b.Weight,
'IN' AS 'Direction'
FROM b
WHERE (b.From_batch = 0) AND (b.Type='Consumption') AND (b.ins_timestamp Between ? And ?) AND (b.To_batch = ?) AND (b.To_status<>'STOCK')
GROUP BY CONVERT(DATE,b.ins_timestamp),
b.Prod_code,
b.Curr_boxes,
b.Label_barcode,
b.From_batch,
b.To_batch,
b.Weight,
b.From_status,
b.To_status
如何将这两个查询移动到一个新的 table 中?请注意,我在 Excel 电子表格中使用 SQL 查询。
我尝试了以下...但我一直收到错误提示 'Invalid Parameter Number' 和 'Invalid Descriptor Index'
CREATE TABLE temp_UNION AS
(
SELECT CONVERT(DATE,a.ins_timestamp) AS 'Date',
a.Prod_code,
a.Curr_boxes,
a.Label_barcode,
b.From_ord_no,
NULL AS To_ord_no,
CASE
WHEN a.From_batch >= a.To_batch THEN a.From_batch
WHEN a.To_batch >= a.From_batch THEN a.To_batch
ELSE a.From_batch
END AS 'Batch',
a.Weight,
'IN' AS 'Direction'
FROM a
JOIN b ON a.Label_barcode = b.Label_barcode
WHERE (a.ins_timestamp Between ? And ?) AND (a.To_batch = ?) AND (a.From_batch = 0) AND (a.Type='Consumption') AND (a.To_status<>'STOCK') AND (b.From_status = 'PORDER')
GROUP BY CONVERT(DATE,a.ins_timestamp),
a.Prod_code,
a.Curr_boxes,
a.Label_barcode,
b.From_ord_no,
a.From_batch,
a.To_batch,
a.Weight,
a.From_status,
a.To_status
union
SELECT CONVERT(DATE,b.ins_timestamp) AS 'Date',
b.Prod_code,
b.Curr_boxes,
b.Label_barcode,
NULL AS From_ord_no,
NULL AS To_ord_no,
CASE
WHEN b.From_batch >= b.To_batch THEN b.From_batch
WHEN b.To_batch >= b.From_batch THEN b.To_batch
ELSE b.From_batch
END AS 'Batch',
b.Weight,
'IN' AS 'Direction'
FROM b
WHERE (b.From_batch = 0) AND (b.Type='Consumption') AND (b.ins_timestamp Between ? And ?) AND (b.To_batch = ?) AND (b.To_status<>'STOCK')
GROUP BY CONVERT(DATE,b.ins_timestamp),
b.Prod_code,
b.Curr_boxes,
b.Label_barcode,
b.From_batch,
b.To_batch,
b.Weight,
b.From_status,
b.To_status
)
如有任何建议,我们将不胜感激!
谢谢, 乔丹
您可以改用 SELECT INTO,这实际上很容易在 Excel 电子表格中完成。
重要的是要注意,要实际创建一个 'temporary' table,主题标签是命名临时 table 时的关键,即 #newtable(如图所示在下面的代码中)。
在这个问题的情况下实际上需要联合。你可以先 select 你的第一个查询到你的 #newtable 然后,在它被创建之后,你可以 select 你的第二个查询到完全相同的 table.
代码见下方:
-- First create your temp table
SELECT CONVERT(DATE,a.ins_timestamp) AS 'Date',
a.Prod_code,
a.Curr_boxes,
a.Label_barcode,
b.From_ord_no,
NULL AS To_ord_no,
CASE
WHEN a.From_batch >= a.To_batch THEN a.From_batch
WHEN a.To_batch >= a.From_batch THEN a.To_batch
ELSE a.From_batch
END AS 'Batch',
a.Weight,
'IN' AS 'Direction'
INTO #newtable
FROM a
JOIN b ON a.Label_barcode = b.Label_barcode
WHERE (a.ins_timestamp Between ? And ?) AND (a.To_batch = ?) AND (a.From_batch = 0) AND (a.Type='Consumption') AND (a.To_status<>'STOCK') AND (b.From_status = 'PORDER')
-- Now we insert the second query into the already created table
INSERT INTO #newtable
SELECT CONVERT(DATE,b.ins_timestamp) AS 'Date',
b.Prod_code,
b.Curr_boxes,
b.Label_barcode,
NULL AS From_ord_no,
NULL AS To_ord_no,
CASE
WHEN b.From_batch >= b.To_batch THEN b.From_batch
WHEN b.To_batch >= b.From_batch THEN b.To_batch
ELSE b.From_batch
END AS 'Batch',
b.Weight,
'IN' AS 'Direction'
FROM b
WHERE (b.From_batch = 0) AND (b.Type='Consumption') AND (b.ins_timestamp Between ? And ?) AND (b.To_batch = ?) AND (b.To_status<>'STOCK')
-- Now we can select whatever we want from our temp table
SELECT Date,
Prod_code,
Curr_boxes,
Label_barcode,
max(From_ord_no) From_ord_no,
To_ord_no,
Batch,
Weight,
Direction
FROM #newtable
GROUP BY Date,
Prod_code,
Curr_boxes,
Label_barcode,
To_ord_no,
Batch,
Weight,
Direction