Compare two tables

Reply

Join Date: Aug 2005
Posts: 26
Reputation: aish is an unknown quantity at this point 
Solved Threads: 0
aish's Avatar
aish aish is offline Offline
Light Poster

Compare two tables

 
0
  #1
Nov 21st, 2005
I'm working with sql servre database I have two tables in the same
database.
thse are my tables,
  1. CREATE TABLE [dbo].[Applicant] (
  2. [UserID] [int] IDENTITY (1, 1) NOT NULL ,
  3. [UserName] [varchar] (50) COLLATE Latin1_General_CI_AS NULL ,
  4. [EducationDetails1] [varchar] (50) COLLATE Latin1_General_CI_AS NULL ,
  5. [EducationDetails2] [varchar] (50) COLLATE Latin1_General_CI_AS NULL ,
  6. [EducationDetails3] [varchar] (50) COLLATE Latin1_General_CI_AS NULL ,
  7. [Skills1] [varchar] (50) COLLATE Latin1_General_CI_AS NULL ,
  8. [Skills2] [varchar] (50) COLLATE Latin1_General_CI_AS NULL ,
  9. [Skills3] [varchar] (50) COLLATE Latin1_General_CI_AS NULL ,
  10. [Score] [int] NULL
  11. ) ON [PRIMARY]
  12. GO
  13.  
  14. CREATE TABLE [dbo].[Vacancy] (
  15. [VacancyID] [int] IDENTITY (1, 1) NOT NULL ,
  16. [EducationDetails1] [varchar] (50) COLLATE Latin1_General_CI_AS NULL ,
  17. [EducationDetails2] [varchar] (50) COLLATE Latin1_General_CI_AS NULL ,
  18. [EducationDetails3] [varchar] (50) COLLATE Latin1_General_CI_AS NULL ,
  19. [Skills1] [varchar] (50) COLLATE Latin1_General_CI_AS NULL ,
  20. [Skills2] [varchar] (50) COLLATE Latin1_General_CI_AS NULL ,
  21. [Skills3] [varchar] (50) COLLATE Latin1_General_CI_AS NULL
  22. ) ON [PRIMARY]
  23. GO
I want to compare values in this two tables.if Applicant.EducationDetails==Vacancy.EducationDetails
or Applicant.Skills == Vacancy.Skills then I want to insert values to Score feild.according to that score I want to
orderby UserID and UserName.I managed to write a query,

  1. declare @UserID int,@count int
  2.  
  3. SET @UserID=2
  4. SET @count=0
  5. IF ((SELECT DISTINCT EducationDetails1 FROM Applicant WHERE UserID=@UserID)LIKE(SELECT EducationDetails1 FROM Vacancy WHERE VacancyID=8))
  6. begin
  7. SET @count=@count+1
  8.  
  9. end
  10.  
  11. .
  12. .
  13. .
  14. .
  15.  
  16. IF((SELECT DISTINCT Skills1 FROM Applicant WHERE UserID=@UserID)LIKE(SELECT Skills1 FROM Vacancy WHERE VacancyID=8))
  17. begin
  18. SET @count=@count+1
  19.  
  20. end
  21. .
  22. .
  23. .
  24. .
  25.  
  26. UPDATE Applicant SET Score=@count WHERE UserID=@UserID
  27.  
  28. SELECT DISTINCT UserID,UserName FROM dbo.Applicant a JOIN dbo.Vacancy j
  29. ON '%'+a.EducationDetails1+ '%' LIKE '%'+j.EducationDetails1 + '%' OR '%'+a.EducationDetails2+ '%' LIKE '%'+j.EducationDetails2 + '%'
  30. OR '%'+a.Skills1+ '%' LIKE '%'+j.Skills1 + '%' ......
  31.  
  32. WHERE j.VacancyID=8 ORDER BY Score DESC
is there any other efficient way to do this? pl help me.
:!:
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 40
Reputation: noman78 is an unknown quantity at this point 
Solved Threads: 0
noman78's Avatar
noman78 noman78 is offline Offline
Light Poster

Re: Compare two tables

 
0
  #2
Nov 22nd, 2005
HERE IS THE SAMPLE FOR CREATE AN UPDATE QUERY USING COMPARISON IN TWO TABLES
<sAMPLE qUERY<
UPDATE Table1
SET t.field1 = t.field3 + s.field2
FROM table1 t, table2 s
WHERE t.field4 = s.field1
AND s.ord_date =
(SELECT MAX(field5) FROM table3)

<end query>
this is the best and most efficient way
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC