1

我真的不确定这个标题是否是我想说的,但这是我能来的最接近的。我想要做的是从查询中获取初始结果,并在 INSERT 语句中的嵌套 Select 语句中使用它们。这是我的示例:这是我的初始查询,它为我提供了我正在寻找的正确工作;

    SELECT Min(Inquirer.ID) as MinID, Max(Inquirer.ID) as MaxID, 
    Min(Inquirer.Program) as MinProg, Max(Inquirer.Program) as MaxProg,
     COUNT(Inquirer.Name) AS NumOccurrences
    FROM Inquirer
    GROUP BY Inquirer.Name
    HAVING ( COUNT(*) = 2 )

结果显示如下;

MinID         MaxID           MinProg          MaxProg          NumOccurrences
 27             81            Pavilion         Appleton              2

我想使用我在相应的 INSERT 中检索到的 MinID 和 MaxID 值,其中包含类似这样的嵌套 SELECT;

Insert into Transfer(InquiryID_fk, ReferElsewhere, ReferMcLean, ReferExternal, ReferredFrom, TransferInquiryID_fk)
Values(SELECT Inquiry.ID from Inquiry 
Where Inquiry.InquirerID_fk = MinID,'Yes',MaxProg,NULL,MinProg,
SELECT Inquiry.ID from Inquiry WHERE Inquiry.InquirerID_fk = MaxID)

简而言之,这就是它,不确定它是否可以完成或如何完成,我是否必须事先制作一个临时表并循环通过它来获取值?任何帮助表示赞赏,在此先感谢您,

尼克G

4

4 回答 4

2

在这种情况下不要使用 VALUES,直接使用 INSERT INTO ( )... SELECT。

例如

INSERT INTO Transfer(InquiryID_fk, ReferElsewhere, ReferMcLean, ReferExternal, ReferredFrom, TransferInquiryID_fk)
SELECT Min(Inquirer.ID), 'Yes', Max(Inquirer.Program), NULL,     Min(Inquirer.Program), Max(Inquirer.ID)
    FROM Inquirer
    GROUP BY Inquirer.Name
    HAVING ( COUNT(*) = 2 )
于 2012-08-01T19:55:44.507 回答
1

尝试使用此代码

Insert into Transfer(
InquiryID_fk, 
ReferElsewhere,
 ReferMcLean,
 ReferExternal, 
ReferredFrom, 
TransferInquiryID_fk)
SELECT Min(Inquirer.id), `yes`,Max(Inquirer.Program), null, Min(Inquirer.Program), Max(Inquirer.ID)
FROM Inquirer
GROUP BY Inquirer.Name
HAVING ( COUNT(*) = 2 )
于 2012-08-01T20:01:55.363 回答
0

使用新表可以解决您的问题,但我不建议这样做,因为这会为您的查询添加太多嵌套选择,这可能会降低性能。

在你的情况下,我会为此创建一个存储过程

  • 使用两个变量为此创建一个存储过程:MinIdMaxId
  • 在您的第一个选择(您获得 minid 和 maxId)中,您设置这些变量的值

他们在您的查询中使用这些变量:

Insert into Transfer(InquiryID_fk, ReferElsewhere, ReferMcLean, ReferExternal, ReferredFrom, TransferInquiryID_fk)
 SELECT Inquiry.ID from Inquiry Where Inquiry.InquirerID_fk = @MinID
 ,'Yes'
 ,MaxProg
 ,NULL
 ,MinProg
 ,SELECT Inquiry.ID from Inquiry WHERE Inquiry.InquirerID_fk = @MaxID
于 2012-08-01T19:51:50.347 回答
0

这应该有效。

;with cte as
(
    SELECT Min(Inquirer.ID) as MinID, Max(Inquirer.ID) as MaxID, 
    Min(Inquirer.Program) as MinProg, Max(Inquirer.Program) as MaxProg,
    COUNT(Inquirer.Name) AS NumOccurrences
    FROM Inquirer
    GROUP BY Inquirer.Name
    HAVING ( COUNT(*) = 2 )
)
    Insert into Transfer
    (InquiryID_fk, ReferElsewhere, ReferMcLean, ReferExternal, ReferredFrom, TransferInquiryID_fk)
    SELECT imin.id,'Yes',MaxProg,NULL,MinProg,imax.ID 
    from cte
        inner join inquiry imin on MinID = imin.inquirer_Id 
        inner join inquiry imax on MaxID = imax.inquirer_Id 

但我认为你想要的是选择阅读......

    SELECT imin.id,'Yes',qmax.Program,NULL,qmin.Program,imax.ID 
    from cte
        inner join inquiry imin on MinID = imin.inquirer_Id 
                 inner join inquirer qmin on minid = qmin.id
        inner join inquiry imax on MaxID = imax.inquirer_Id 
                 inner join inquirer qmax on minid = qmax.id

...但这取决于您拥有哪种 SQL

于 2012-08-01T20:01:27.320 回答