将所选数据传递给sql server中的另一个查询

时间:2014-11-02 05:46:38

标签: sql sql-server select

我在SQL Server中有这样的查询

SELECT JOINT_NO_PGZ,
       MAT_0_PGZ,
       Sum(QTY_0_PGZ),
       Mat_1_PGZ,
       Sum(qty_check_1_PGZ),
       Line_Number_PGZ
FROM   [PGC].[dbo].[HISTORies]
GROUP  BY JOINT_NO_PGZ,
          Line_Number_PGZ,
          MAT_0_PGZ,
          Mat_1_PGZ 

在选择查询中我有2列MAT_0_PGZ和Mat_1_PGZ,我应该将这些值传递给另一个表并根据该表获得结果但我不知道如何将这些值传递给另一个查询我的意思:

SELECT JOINT_NO_PGZ,
       MAT_0_PGZ,
       Sum(QTY_0_PGZ),
       Mat_1_PGZ,
       Sum(qty_check_1_PGZ),
       Line_Number_PGZ,
       ( Function(MAT_0_PGZ, Mat_1_PGZ) )
FROM   [PGC].[dbo].[HISTORies]
GROUP  BY JOINT_NO_PGZ,
          Line_Number_PGZ,
          MAT_0_PGZ,
          Mat_1_PGZ 

我是SQL Server新手。

祝你好运

2 个答案:

答案 0 :(得分:1)

您应该使用insert into...

insert into [your table] values (col1, col2)

SELECT JOINT_NO_PGZ,
       MAT_0_PGZ,
       Sum(QTY_0_PGZ),
       Mat_1_PGZ,
       Sum(qty_check_1_PGZ),
       Line_Number_PGZ
FROM   [PGC].[dbo].[HISTORies]
GROUP  BY JOINT_NO_PGZ,
          Line_Number_PGZ,
          MAT_0_PGZ,
          Mat_1_PGZ

并使用您的第二个查询

答案 1 :(得分:0)

检查一下:

insert into (table_name) (column_names separated by commas) (the select statement with the columns you want to transfer separated also by commas)

示例:

insert into studentTable (Student_ID, Student_Class) (select Student_ID, Student_Class from StudentMarks)

这将有效。