使用两个 "FROM" 子句将两个值插入 table

Insert into a table two values using two "FROM" clauses

有没有办法使用两个 "FROM" 子句将两个值插入到 table 中? 我尝试插入百分位值 - 曝光度和认知度:

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]);

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

INSERT INTO table_name (col_names) VALUES (col_values);

从上面的查询中给出你想要的更清楚的图片?

我建议你试试下面的代码

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]);  

我很确定你不能像这样使用两个 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;

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

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)
 )

我试着模拟了同样的东西,它对我来说工作得很好。

财政年度, 我创建了一个 table [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

请找到下面的截图

 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)
  )