简体   繁体   中英

Using SQL result variables in Nested SELECT statement

I'm really unsure if the title of this is what I am trying to say, but it's the closest I could come. What I want to do is take the initial results from a query and use them in a nested Select statement within an INSERT statement. Here is my example: Here is my initial query which gives me what I am looking for an works correctly;

    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 )

The results display like this;

MinID         MaxID           MinProg          MaxProg          NumOccurrences
 27             81            Pavilion         Appleton              2

I would like to use the MinID and MaxID values that I retrieve in a corresponding INSERT with a nested SELECT something like this;

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)

And that is really it in a nutshell, not sure if it can be accomplished or how to accomplish it, would I have to make a temp table beforehand and loop through that to get the values? Any help is appreciated, thank you in advance,

NickG

Don't use VALUES in this case, use straight INSERT INTO ( )... SELECT.

Eg

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 )

Try with this code

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 )

Using a new table solve your problem, but I would not recommend since will add too many nested selects to your query, which may reduce the performance.

In your case I would create a stored procedure for this

  • Create a stored procedure for this with two variables: MinId and MaxId
  • In your first select (which you gets the minid and maxId) you set the values of these variables

Them, in your query, use these variables:

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

This should work.

;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 

but what I think you want is for the select to read...

    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

... but it depends on which variety of SQL you have

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM