Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 177
  • Last Modified:

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



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

Open in new window

0
feesu
Asked:
feesu
  • 9
  • 8
1 Solution
 
imitchieCommented:
try making line 12 SELECT DISTINCT
0
 
feesuAuthor Commented:
I have an ntext field that doesn't accept a DISTINCT selection!
0
 
imitchieCommented:
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
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
feesuAuthor Commented:
It's the QuestionHTML field. Belongs to dbo.FAQ_Question Q
0
 
imitchieCommented:
The other question I have, is if it is found in more than one category, which CategoryName do you show?
0
 
imitchieCommented:
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?
0
 
feesuAuthor Commented:
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(fieldname) LIKE '%' + @Keyword + '%" will accept DISTINCT?
0
 
feesuAuthor Commented:
BTW, i posted the errors i got while trying to create that HTML function on the other thread.
0
 
imitchieCommented:
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
0
 
imitchieCommented:
Assuming QuestionHTML is your only NTEXT field, this query ensures that a Question is only ever listed once, regardless of the many one-many relationshipts.
CREATE PROCEDURE [FAQ_OpenSearch]
(
        @keyword nvarchar(100)
)
AS
BEGIN
        SET NOCOUNT ON
        DECLARE @Err int
 
 
 	SELECT	Q.Module_IKey,Q.QuestionTitle, Q.QuestionHTML, INNERQ.*
	FROM
	(
	SELECT    Q.IKey,
		MIN(C.CategoryName) AS CategoryName,
		MIN(S.SoftwareManufacturer) AS SoftwareManufacturer,
		MIN(S.ProductName) AS S_ProductName,
		MIN(S.SoftwareVersion) AS SoftwareVersion, 
                MIN(S.OtherDetails) AS S_OtherDetails,
		MIN(H.HardwareManufacturer) AS HardwareManufacturer,
		MIN(H.ProductName) AS H_ProductName,
		MIN(H.ProductModel) AS ProductModel,
		MIN(H.OtherDetails) AS H_OtherDetails, 
		MIN(D.[Language]) AS Language,
		MIN(D.Browser) AS Browser,
		MIN(D.URL) AS URL,
		MIN(E.ErrorMessage) AS ErrorMessage,
		MIN(O.RelatedInformation) AS RelatedInformation,
		MIN(U.FullName) as 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 + '%'
        
                )
	GROUP BY Q.IKEY        
 	) INNERQ INNER JOIN dbo.FAQ_Question Q ON Q.IKEY = INNERQ.IKEY
		
        ORDER BY Q.SystemDate DESC
 
 
        SET @Err = @@Error
        RETURN @Err
END
GO

Open in new window

0
 
feesuAuthor Commented:
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?
0
 
imitchieCommented:
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
0
 
feesuAuthor Commented:
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!
0
 
imitchieCommented:
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?
0
 
feesuAuthor Commented:
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.
0
 
imitchieCommented:
How do you know where the line breaks should be? Are they in there as char(13) and/or char(10)?
0
 
feesuAuthor Commented:
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now