使用两个“FROM”子句将两个值插入表中

时间:2015-04-28 10:29:47

标签: sql ms-access sql-insert

有没有办法使用两个“FROM”子句将两个值插入表中? 我尝试插入百分位数值 - 曝光和意识:

INSERT INTO tbReport (Exposure, Awareness) 
SELECT MAX([q_Exposure])
  FROM (SELECT TOP 30 PERCENT [q_Exposure]
  FROM tbQuestions
  WHERE q_Exposure IS NOT NULL ORDER BY [q_Exposure]),
MAX([q_Awareness])
  FROM (SELECT TOP 30 PERCENT [q_Awareness]
  FROM tbQuestions
  WHERE q_Awareness IS NOT NULL ORDER BY [q_Awareness]);

4 个答案:

答案 0 :(得分:1)

我很确定你不能使用这样的两个SELECT语句,你也可以做类似的事情,

INSERT INTO tbReport (Exposure, Awareness) 
SELECT 
    Max(tmpQ.Exposure) As MaxExpo, 
    Max(tmpQ.Awareness) As MaxAware
FROM
    (SELECT MAX([q_Exposure]) As Exposure, 0 As Awareness FROM (SELECT TOP 30 PERCENT [q_Exposure] FROM tbQuestions WHERE q_Exposure IS NOT NULL ORDER BY [q_Exposure])
    UNION ALL
    SELECT 0 As Exposure, MAX([q_Awareness]) As Awareness FROM (SELECT TOP 30 PERCENT [q_Awareness] FROM tbQuestions WHERE q_Awareness IS NOT NULL ORDER BY [q_Awareness])) As tmpQ;

答案 1 :(得分:0)

我不认为您提到的语法会起作用,因为典型的插入语法将是:

INSERT INTO table_name (col_names) VALUES (col_values);

从上述查询中更清楚地了解您的需求?

答案 2 :(得分:0)

我建议您尝试以下代码

INSERT INTO tbReport (Exposure, Awareness)
SELECT MAX(SELECT TOP 30 PERCENT [q_Exposure]
FROM tbQuestions
WHERE q_Exposure IS NOT NULL
ORDER BY [q_Exposure]),
MAX(SELECT TOP 30 PERCENT [q_Awareness]
FROM tbQuestions
WHERE q_Awareness IS NOT NULL
ORDER BY [q_Awareness]);  

答案 3 :(得分:0)

请尝试运行以下查询,希望它有效:)

INSERT INTO tbReport (Exposure, Awareness) 
 VALUES
 (
  (SELECT MAX (Q_E.q_Exposure) 
  FROM (SELECT TOP 30 PERCENT q_Exposure FROM tbQuestions WHERE q_Exposure IS NOT NULL ORDER BY q_Exposure) Q_E)
 ,
  (SELECT  MAX(Q_A.q_Awareness) 
  FROM (SELECT TOP 30 PERCENT q_Awareness FROM tbQuestions WHERE q_Awareness IS NOT NULL ORDER BY q_Awareness) Q_A)
 )

我试图模拟它,它对我来说非常好。

FYR, 我创建了一个表[t_empSalary]

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

 CREATE TABLE t_empSalary
     (
     empSalary INT,
     empid INT
     )

GO

SET ANSI_PADDING OFF
GO

请查看以下截图

enter image description here

 INSERT INTO [t_empSalary](empSalary, empid ) VALUES 
  (
  (SELECT MAX(t_empSalary.empSalary) FROM (SELECT TOP 30 PERCENT empSalary FROM  [dbo].[t_empSalary]) t_empSalary)
   ,
  (SELECT MAX(t_emp.empid) FROM  (SELECT TOP 30 PERCENT empid FROM  [dbo].[t_empSalary]) t_emp)
  )