在sql中复制临时表中的数据

时间:2015-01-07 20:46:08

标签: sql sql-server

我想从其他表中复制临时数据中的数据......

CREATE TABLE #temp1 (
    Question_ID INT
    ,Excellent INT
    ,Good INT
    ,Average INT
    ,Below_Average INT
    ,Poor INT
    )

INSERT INTO #temp1
VALUES (
    (
        SELECT Question_ID
        FROM Eval
        GROUP BY Question_ID
        )
    ,(
        SELECT COUNT(Answer_Marks) AS "Excellent"
        FROM Eval
        WHERE Answer_Marks = 5
            AND Emp_no = 'biit218'
        GROUP BY Question_ID
        )
    ,(
        SELECT COUNT(Answer_Marks) AS "Good"
        FROM Eval
        WHERE Answer_Marks = 4
            AND Emp_no = 'biit218'
        GROUP BY Question_ID
        )
    ,(
        SELECT COUNT(Answer_Marks) AS "Average"
        FROM Eval
        WHERE Answer_Marks = 3
            AND Emp_no = 'biit218'
        GROUP BY Question_ID
        )
    ,(
        SELECT COUNT(Answer_Marks) AS "Baverage"
        FROM Eval
        WHERE Answer_Marks = 2
            AND Emp_no = 'biit218'
        GROUP BY Question_ID
        )
    ,(
        SELECT COUNT(Answer_Marks) AS "poor"
        FROM Eval
        WHERE Answer_Marks = 1
            AND Emp_no = 'biit218'
        GROUP BY Question_ID
        )
    )

每个子查询都应该在临时表中复制15条记录,但它会返回错误:

  

Msg 512,Level 16,State 1,Line 1
  子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。声明已经终止。

3 个答案:

答案 0 :(得分:6)

您需要使用INSERT INTO ... SELECT .. FROM来执行插入操作。您当前的语法不起作用,因为您尝试将多行插入单个列。如果您使用类似的子查询,它应该只为所有行返回一个值。我建议使用一些带有聚合函数的条件逻辑来使每个列类似于:

INSERT INTO #temp1
SELECT 
    Question_ID,
    sum(case when Answer_Marks = 5 then 1 else 0 end) as Excellent,
    sum(case when Answer_Marks = 4 then 1 else 0 end) as Good,
    sum(case when Answer_Marks = 3 then 1 else 0 end) as Average,
    sum(case when Answer_Marks = 2 then 1 else 0 end) as Baverage,
    sum(case when Answer_Marks = 1 then 1 else 0 end) as Poor
FROM Eval
WHERE Emp_no = 'biit218'
GROUP BY Question_ID

答案 1 :(得分:4)

您需要使用insert into select语法

insert into #temp1
select question_id,
       count( case when Answer_Marks = 5 then 1 end ) as Excellent,
       count( case when Answer_Marks = 4 then 1 end ) as Good,
       count( case when Answer_Marks = 3 then 1 end ) as Average,
       count( case when Answer_Marks = 2 then 1 end ) as Baverage,
       count( case when Answer_Marks = 1 then 1 end ) as Poor
from Eval
where Emp_no ='biit218' and Answer_Marks between 1 and 5
group by Question_ID

答案 2 :(得分:-2)

删除“valus”关键字

INSERT INTO #temp1 SELECT ( SELECT Question_ID FROM Eval GROUP BY Question_ID ) ,( SELECT COUNT(Answer_Marks) AS "Excellent" FROM Eval WHERE Answer_Marks = 5 AND Emp_no = 'biit218' GROUP BY Question_ID ) ,( SELECT COUNT(Answer_Marks) AS "Good" FROM Eval WHERE Answer_Marks = 4 AND Emp_no = 'biit218' GROUP BY Question_ID ) ,( SELECT COUNT(Answer_Marks) AS "Average" FROM Eval WHERE Answer_Marks = 3 AND Emp_no = 'biit218' GROUP BY Question_ID ) ,( SELECT COUNT(Answer_Marks) AS "Baverage" FROM Eval WHERE Answer_Marks = 2 AND Emp_no = 'biit218' GROUP BY Question_ID ) ,( SELECT COUNT(Answer_Marks) AS "poor" FROM Eval WHERE Answer_Marks = 1 AND Emp_no = 'biit218' GROUP BY Question_ID )