Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query Inserting repeated rows

Tags:

sql-server

I am trying to write a query that will insert a Disposition-Code and Disposition-Desc into my table ONLY if the Disposition-Code does not exist. I currently have this, but for some reason it is inserting all the dispositions of all my records into my disposition table. My goal is to have a dispositions table with all the UNIQUE DISPOSITIONS.

INSERT INTO TexasBexarCountyMisdemeanorDispositions   ([DISPOSITION-CODE], [DISPOSITION-DESC]) 
SELECT t1.[DISPOSITION-CODE], t1.[DISPOSITION-DESC]   
FROM TexasBexarCountyMisdemeanorPublicRecords t1  
WHERE t1.[DISPOSITION-CODE] NOT IN (SELECT [DISPOSITION-CODE] FROM TexasBexarCountyMisdemeanorDispositions)

This block of code is inserting every disposition of every record into my new table. So instead of having a table with 30 dispositions, I have a table the size of my records table. Lets say I have a disposition code of 619 that has already been inserted into my table. I want the query to NOT INSERT this disposition again.

Does any one have any insight of how to do this?

like image 861
Lostaunaum Avatar asked Dec 30 '25 15:12

Lostaunaum


1 Answers

Please try this query. Note the DISTINCT constraint on select

INSERT INTO 
 TexasBexarCountyMisdemeanorDispositions   
 (
  [DISPOSITION-CODE], 
  [DISPOSITION-DESC]
 ) 
SELECT DISTINCT
 [DISPOSITION-CODE], 
 [DISPOSITION-DESC]   
FROM 
 TexasBexarCountyMisdemeanorPublicRecords
WHERE [DISPOSITION-CODE] NOT IN 
(
 SELECT 
  DISTINCT [DISPOSITION-CODE] 
 FROM 
 TexasBexarCountyMisdemeanorDispositions
)
like image 89
DhruvJoshi Avatar answered Jan 02 '26 08:01

DhruvJoshi