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?
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
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With