Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 INSERT - SELECT QUERY

Author  Topic 

mshsilver
Posting Yak Master

112 Posts

Posted - 2009-05-21 : 06:35:28
Hi,

I am using this query to select query that pulls only one row of data per employer, each employer should have a unique edrs number unfortunatly 500 records don't and won't for a little while.

I am cross referenceing the wce_ilr and wce_contact tables, finding any records in the wce_ilr table that do not exist in the wce_contact table and then trying to insert only one instance of those that don't exist into the wce_contact table.

The problem i am having is:

i use the edrs as the uniqueid in the wce_contact table and this select part of the query brings, as it is being asked, one instance of a record where the unqiue value in the edrs field is the same, so it looks at the 500 records without an edrs and selects one but I can't insert a null value as a uniqueid...

My Question is:

can i then add some code to remove the null edrs row and just insert the values where the edrs exists? but in case i wasn't clear in the wce_ilr table the edrs numbers will exist many times.

is because records do not have an edrs the below query is displaying one record that has a blank edrs.

Thanks for any help.



SELECT wce_ilr.EDRS AS Uniqueid, MAX(wce_ilr.EDRS) AS EDRS, MAX(wce_ilr.Employer_Name) AS Employer_name, MAX(wce_ilr.Emp_Phone) AS emp_phone,
MAX(wce_ilr.Emp_Address_1) AS Address1, MAX(wce_ilr.Emp_Address_2) AS Address2, MAX(wce_ilr.Emp_Address_3) AS Address3,
MAX(wce_ilr.Emp_Address_4) AS Address4, MAX(wce_ilr.Emp_Address_Pcode1) + ' ' + MAX(wce_ilr.Emp_Address_Pcode2) AS Postcode,
'Company' AS Record_Type
FROM wce_ilr LEFT OUTER JOIN
wce_contact AS wce_contact ON wce_ilr.EDRS = wce_contact.EDRS COLLATE database_default
WHERE (wce_contact.EDRS IS NULL)
GROUP BY wce_ilr.EDRS
ORDER BY EDRS

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-05-22 : 03:55:52
same as
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=126016
Go to Top of Page
   

- Advertisement -