I am having issues on SQL Server.
I need to do an insert select into another database.
The select is very specific
there are 3 tables (2 have the same primary key with one being the main table containing the information <so it may exist in one table but not the other>) No problem I just LEFT OUTER JOIN and that takes care of that issue.
the other table has a many relationship to the main table. there is guaranteed to be one in this table for every one in the main table, but there can be more. I need to find the first instance of that table based on the first one and do a selection. I am having difficulties trying to think of a way to do this. Any help would be much appreciated.
Here is the code. I have changed the names and titles to preculde any secure information
any ideas?
**UPDATE**
I actually got it. Someone from another place suggested a subquery in the join (to replace the tblMany) that was grouped by the mainID.
It would not work to set up a distinct value and treat the query as distinct. that would not pull the correct information.
the new query is actually quite simple. I don't know why I didn't think about that. Must be because I was a little annoyed at the time.
Of course this isn't an elegant solution, because it only select the min or maximum value of the info, so if there are any better solutions, feel free to post them.
But for now, I am going to try this.
I need to do an insert select into another database.
The select is very specific
there are 3 tables (2 have the same primary key with one being the main table containing the information <so it may exist in one table but not the other>) No problem I just LEFT OUTER JOIN and that takes care of that issue.
the other table has a many relationship to the main table. there is guaranteed to be one in this table for every one in the main table, but there can be more. I need to find the first instance of that table based on the first one and do a selection. I am having difficulties trying to think of a way to do this. Any help would be much appreciated.
Here is the code. I have changed the names and titles to preculde any secure information
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[sp_InsertSelect]
AS
BEGIN
SET NOCOUNT ON;
INSERT tblForInsert (mainID, infoT1, infoT2, infoT3)
SELECT t1.mainID, t1.info, t2.info, t3.info
FROM
tableMain t1
INNER JOIN tableMany t2 -- this is causing duplications because the tableMany is just that, many. (I cannot GROUP BY because of the other join, I don't think EXISTS would help, but I am not sure)
ON t1.mainID = t2.mainID
LEFT OUTER JOIN addressINFO t3
ON t1.mainID = t3.mainID
WHERE
t1.pseudoField LIKE 'V%' OR t1.pseudoField LIKE 'H%' OR t1.pseudoField LIKE 'J[0-9]'
ORDER BY t1.pseudoField
END
any ideas?
**UPDATE**
I actually got it. Someone from another place suggested a subquery in the join (to replace the tblMany) that was grouped by the mainID.
It would not work to set up a distinct value and treat the query as distinct. that would not pull the correct information.
the new query is actually quite simple. I don't know why I didn't think about that. Must be because I was a little annoyed at the time.
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[sp_InsertSelect]
AS
BEGIN
SET NOCOUNT ON;
INSERT tblForInsert (mainID, infoT1, infoT2, infoT3)
SELECT t1.mainID, t1.info, t2.infoMin, t3.info --or infoMax depending on what you want
FROM
tableMain t1
INNER JOIN (select min(info) As infoMin, max(info) As infoMax, mainID FROM tableMany t2 GROUP BY mainID)
ON t1.mainID = t2.mainID
LEFT OUTER JOIN addressINFO t3
ON t1.mainID = t3.mainID
WHERE
t1.pseudoField LIKE 'V%' OR t1.pseudoField LIKE 'H%' OR t1.pseudoField LIKE 'J[0-9]'
ORDER BY t1.pseudoField
END
Of course this isn't an elegant solution, because it only select the min or maximum value of the info, so if there are any better solutions, feel free to post them.
But for now, I am going to try this.
