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)
 Table Joins

Author  Topic 

DaveC11
Starting Member

43 Posts

Posted - 2009-03-16 : 04:48:56
I'm having a problem to trying to get the desired results for an sql query below.
The query is supposed to allow users to search jobs on our DB based on a keyword and a location.

The problem I'm having is that a when you run the query to search for a job the same job can apperar several times.

This is because a job can have several jobattributes assigned to it. For example a job that is coded up with the attributes London and South London will appear twice if a user searches for jobs in London.

How do I alter my SP to solve this problem?




@Keyword nvarchar(500),
@Location nvarchar(500))
as
select distinct j.jobid,
j.jobtitle,
convert(varchar(11),j.createdon, 106) as date,
a.description as 'location',
left(cast(j.publishedjobdescription as Nvarchar(500)), 300)+ ' ....' as 'Description',
j.createdon,
cast(round((j.maxbasic),2,1) as decimal(10))as 'salary'
FROM ATTRIBUTES A
INNER JOIN JOBATTRIBUTES JA ON A.ATTRIBUTEID = JA.ATTRIBUTEID
INNER JOIN JOBS J ON JA.JOBID = J.JOBID


where a.description like '%'+@Location+'%'
and (j.jobtitle like '%'+@keyword+'%'or publishedjobdescription like '%'+@keyword+'%')
and a.attributemasterid = '419'
and j.updatedon > dateadd(month,-6,getdate())
and DATALENGTH(publishedjobdescription) > 100
and archived = 'n'
and published = 'y'

order by j.createdon desc






sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-03-16 : 07:15:32
You'd have to define a criteria then. Which attribute would you like to display when user searches for jobs in London, London or South London ?
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2009-03-16 : 09:15:02
Or you could concatenate the locations.
Something like:

CREATE PROCEDURE YourProc
(
@Keyword nvarchar(500)
,@Location nvarchar(500)
)
AS

SET NOCOUNT ON

;WITH JobLocations (JobId, Location)
AS
(
SELECT JA.JobId
,A.[Description]
FROM Attributes A
JOIN JobAttributes JA
ON A.AttributeId = JA.AttributeId
WHERE A.AttributeMasterId = 419
)
SELECT
J.jobid
,J.jobtitle
,CONVERT(varchar(11), J.CreatedOn, 106) as [Date]
,D.Locations
,SUBSTRING(J.PublishedJobDescription, 1, 300) + ' ....' as [Description]
,J.CreatedOn
,CAST(ROUND((J.MaxBasic),2,1) as decimal(10)) as Salary
FROM Jobs J
JOIN
(
SELECT JobId
,STUFF
(
(
SELECT ' ' + Location + ','
FROM JobLocations JL1
WHERE JL1.JobId = JL.JobId
FOR XML PATH('')
)
,1 ,1 ,''
) AS Locations
FROM JobLocations JL
GROUP BY JobId
) D
ON J.JobId = D.JobId
WHERE J.UpdatedOn > DATEADD(m, -6, GETDATE())
AND
(
J.JobTitle LIKE '%' + @keyword + '%'
OR J.PublishedJobDescription LIKE '%' + @keyword + '%'
)
AND J.Archived = 'N'
AND J.Published = 'Y'
AND D.Locations LIKE '%' + @Location + '%'
ORDER BY J.CreatedOn DESC
GO

Go to Top of Page
   

- Advertisement -