feesu
asked on
SQL SERVER 2000 - Searching Inside A Table That Is Inner Joined With A Sub Table Returns Duplicates
Experts,
Ive got a query that searches inside a table called Questions. This table is linked to QuestionCategories, in a one to many relation.
I want the user to be able to search in the questions, but my problem is that it returns the same record in duplicates depending on how many categories is this question related to.
The search is open, it searches for the same keyword in all fields, even the category name that is in a different table.
How may I achieve returning a result set that meets the users search but dont show the same record more than once.
I am attaching my stored procedure.
Thanks in advance
Ive got a query that searches inside a table called Questions. This table is linked to QuestionCategories, in a one to many relation.
I want the user to be able to search in the questions, but my problem is that it returns the same record in duplicates depending on how many categories is this question related to.
The search is open, it searches for the same keyword in all fields, even the category name that is in a different table.
How may I achieve returning a result set that meets the users search but dont show the same record more than once.
I am attaching my stored procedure.
Thanks in advance
CREATE PROCEDURE [FAQ_OpenSearch]
(
@keyword nvarchar(100)
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @Err int
SELECT Q.Module_IKey,Q.IKey,Q.QuestionTitle, Q.QuestionHTML, C.CategoryName, S.SoftwareManufacturer, S.ProductName AS S_ProductName, S.SoftwareVersion,
S.OtherDetails AS S_OtherDetails, H.HardwareManufacturer, H.ProductName AS H_ProductName, H.ProductModel, H.OtherDetails AS H_OtherDetails,
D.[Language], D.Browser, D.URL, E.ErrorMessage, O.RelatedInformation, U.FullName
FROM dbo.FAQ_Question Q INNER JOIN
dbo.sys_Users U ON Q.EditedBy = U.IKey INNER JOIN
dbo.FAQ_QuestionCategories QC ON Q.IKey = QC.Question_IKey INNER JOIN
dbo.FAQ_Categories C ON QC.Category_IKey = C.IKey LEFT OUTER JOIN
dbo.FAQ_QuestionTags_Hardware H ON Q.IKey = H.Question_IKey LEFT OUTER JOIN
dbo.FAQ_QuestionTags_Software S ON Q.IKey = S.Question_IKey LEFT OUTER JOIN
dbo.FAQ_QuestionTags_Development D ON Q.IKey = D.Question_IKey LEFT OUTER JOIN
dbo.FAQ_QuestionTags_OtherInformation O ON Q.IKey = O.Question_IKey LEFT OUTER JOIN
dbo.FAQ_QuestionTags_ErrorMessages E ON Q.IKey = E.Question_IKey
WHERE
(
CategoryName LIKE '%' + @keyword + '%'
OR QuestionHTML LIKE '%' + @keyword + '%'
OR QuestionTitle LIKE '%' + @keyword + '%'
OR RelatedInformation LIKE '%' + @keyword + '%'
OR S.OtherDetails LIKE '%' + @keyword + '%'
OR SoftwareVersion LIKE '%' + @keyword + '%'
OR S.ProductName LIKE '%' + @keyword + '%'
OR SoftwareManufacturer LIKE '%' + @keyword + '%'
OR H.OtherDetails LIKE '%' + @keyword + '%'
OR ProductModel LIKE '%' + @keyword + '%'
OR H.ProductName LIKE '%' + @keyword + '%'
OR HardwareManufacturer LIKE '%' + @keyword + '%'
OR ErrorMessage LIKE '%' + @keyword + '%'
OR URL LIKE '%' + @keyword + '%'
OR Browser LIKE '%' + @keyword + '%'
OR Language LIKE '%' + @keyword + '%'
)
ORDER BY Q.SystemDate DESC
SET @Err = @@Error
RETURN @Err
END
GO
try making line 12 SELECT DISTINCT
ASKER
I have an ntext field that doesn't accept a DISTINCT selection!
does it belong to any of these four tables?
dbo.FAQ_Question Q
dbo.sys_Users U
dbo.FAQ_QuestionCategories QC
dbo.FAQ_Categories C
dbo.FAQ_Question Q
dbo.sys_Users U
dbo.FAQ_QuestionCategories
dbo.FAQ_Categories C
ASKER
It's the QuestionHTML field. Belongs to dbo.FAQ_Question Q
The other question I have, is if it is found in more than one category, which CategoryName do you show?
Is the FAQ_QuestionTags_Software also many to one with Question?
What if it matches two FAQ_QuestionTags_Software records, doesn't that also show the question more than once?
What if it matches two FAQ_QuestionTags_Software records, doesn't that also show the question more than once?
ASKER
If the keyword is in more than one category then let it show the first one.
Regarding ntext, i don't think that you need to worry about it cuz i will be using the function you sent me in another thread that converts html to text and will not be ntext anymore, but the question is, will calling that function such as dbo.ufn_removetags(fieldna me) LIKE '%' + @Keyword + '%" will accept DISTINCT?
Regarding ntext, i don't think that you need to worry about it cuz i will be using the function you sent me in another thread that converts html to text and will not be ntext anymore, but the question is, will calling that function such as dbo.ufn_removetags(fieldna
ASKER
BTW, i posted the errors i got while trying to create that HTML function on the other thread.
do you ever need to return more than 4000 chars from ANY column to the end user?? If not, ntext is too large, consider using CONVERT(nVarchar(4000), columnname) as ShorterColumn in the SELECT, and then you can use distinct
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
imitchie,
You didn't comment on my previous comment. I was asking that if i will use the remove tag function, then i won't need the ntext field cuz that function will return 4000 chars.
Do i still need to use the SP you modified or you will change it now?
You didn't comment on my previous comment. I was asking that if i will use the remove tag function, then i won't need the ntext field cuz that function will return 4000 chars.
Do i still need to use the SP you modified or you will change it now?
feesu,
Use http:#20402349
Even if the NText problem is solved, Distinct doesn't solve the problem of one question being matched in two categories
i.e.
Question, Category
What is the matrix, Matrix
What is the matrix, Movie
Distinct will still leave the question in the result set twice
Use http:#20402349
Even if the NText problem is solved, Distinct doesn't solve the problem of one question being matched in two categories
i.e.
Question, Category
What is the matrix, Matrix
What is the matrix, Movie
Distinct will still leave the question in the result set twice
ASKER
imitchie,
This worked perfectly, but i have one observation though, i need to search and exclude html but while displaying my search results in a grid i need it to at least have those breaks between lines that make it look kinda readable because this way the ntext paragraph appears as a whole block of text in the grid!
This worked perfectly, but i have one observation though, i need to search and exclude html but while displaying my search results in a grid i need it to at least have those breaks between lines that make it look kinda readable because this way the ntext paragraph appears as a whole block of text in the grid!
Can you elaborate a bit on that? Do you have a sample of what a HTML block looks like from your table? Do you mean that you need <BR/> tags in there to cause the browser to put line breaks in?
ASKER
Well, the reason behind saving the html code is to display it in a formated way, and edit it using a rich text editor. When searching, i have to exclude the tags to get accurate results, but then if the select statement has my column value in a plain text format, it won't display properly on my page.
How do you know where the line breaks should be? Are they in there as char(13) and/or char(10)?
ASKER
It's not only about breaks, that was just an example. I solved it by searching with a query, and binding the grid to another one that is passed the condition. Thanks.