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 2008 Forums
 Transact-SQL (2008)
 Distinct Rows combined with FOR XML concatenation

Author  Topic 

deadtrees
Starting Member

26 Posts

Posted - 2012-03-29 : 11:25:58
I'm trying to make a report that lists a row specific to the details of an impression, with the last column as a summary of all the evaluations listed for that impression.

I was asked to add a field 'LiftedBy' which will be filled in when the Evaluation = IND. The problem is now that whenever there is a 'LiftedBy' value and more than one evaluation has been captured for that impression, it shows two rows; One with all the impressions and the Lifted by field filled in and one with all the information listed again but where the lifted by field is blank.

Sample data output looks like this:

 ITEM#	LAB#	LISTED AS:	     ANALYSIS	               LIFTED BY	   VISUAL COMPARISON RESULTS
AB123 AA02 A DOOR HANDLE FINGER / VALUE / NO AFIS RUN IND to R. Palm of JUSTICE, BUFORD T, EXC to PUBLIC, JOHN Q ICP to DOE, JOHN
AB123 AA02 A DOOR HANDLE FINGER / VALUE / NO AFIS RUN BC 12345 IND to R. Palm of JUSTICE, BUFORD T, EXC to PUBLIC, JOHN Q ICP to DOE, JOHN


I need the select statement outside of the FOR XML PATH to pull only distinct rows, but everything I've tried (WHERE EXISTS and different join types) has not worked. Perhaps it's the syntax, but could someone point me in the right direction?

SELECT  Upper(I.ItemNumber) as [ITEM#],Upper(I."Lab Number" +' '+ ISNULL(I.LatentLetter,' '))AS [LAB#] 	,Upper(I.ItemSource) As [LISTED AS:]
,Upper(ISNULL((Case I.Finger
WHEN 1
THEN'Finger'
Else (Case I.Palm
WHEN 1
THEN 'Palm'
ELSE (Case I.Impression
WHEN 1
THEN'Impression'
ELSE NULL
END)
END)
END),'NLPC') + ' / ' +
+ (Case I.LatentAnalysis
WHEN 1
THEN 'NLPC'
WHEN 2
THEN 'VALUE'
WHEN 3
THEN 'VALUE'
ELSE ' '
END)) + ' / ' + (Case WHEN I.AFISRun <> '' THEN 'AFIS RUN ' ELSE 'NO AFIS RUN' END) AS [ANALYSIS]
,ISNULL(UPPER(E1.LiftedBy), '') as [LIFTED BY]
,ISNULL((SELECT (CASE E2.Evaluation
WHEN 1
THEN 'IND'
WHEN 2
THEN 'EXC'
WHEN 3
THEN 'INC'
WHEN 4
THEN 'ICP'
END) + ' to ' + ISNULL(CASE E2.IndTo
WHEN 1
THEN 'R. Thumb of '
WHEN 2
THEN 'R. Index of '
WHEN 3
THEN 'R. Middle of '
WHEN 4
THEN 'R. Ring of '
WHEN 5
THEN 'R. Pinky of '
WHEN 6
THEN 'L. Thumb of '
WHEN 7
THEN 'L. Index of '
WHEN 8
THEN 'L. Middle of '
WHEN 9
THEN 'L. Ring of '
WHEN 10
THEN 'L. Pinky of '
WHEN 12
THEN 'L. Palm of '
WHEN 13
THEN 'R. Palm of '
WHEN 14
THEN 'L. Foot of '
WHEN 15
THEN 'R. Foot of '
END ,'') + '' + Upper(S.SubjectLastName +', '+S.SubjectFirstName + CHAR(10))
FROM LIMS_DATA_Production.dbo.EvidenceSubjects E2
INNER
JOIN LIMS_DATA_Production.dbo.Subjects S
ON E2.SubjectID = S.ID
WHERE S.CaseID = I.CaseID AND S.SDelete = 0 AND E1.EvidenceID = E2.EvidenceID
FOR XML PATH ('') ),'NO RESULTS RECORDED') AS [VISUAL COMPARISON RESULTS]
FROM LIMS_DATA_Production.dbo.EvidenceSubjects E1
RIGHT
JOIN LIMS_DATA_Production.dbo.Impressions I
ON E1.EvidenceID = I.ID
WHERE ((I.CaseID = 12345) and (I.SDelete = 0))
GROUP BY I.ItemNumber,I.AFISRun,I.CaseID, E1.EvidenceID, I."Lab Number", I.LatentLetter, I.ItemSource,I.Finger, I.Palm, I.Impression, I.LatentAnalysis,E1.LiftedBy
ORDER BY [LAB#]

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-29 : 12:23:08
if you want both rows to be combined into one you need to group on all except LiftedBy field and then apply some kind of aggregation (MIN,MAX) on LiftedBy field

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

deadtrees
Starting Member

26 Posts

Posted - 2012-03-29 : 12:26:56
Damn you're good. As always, thanks for the timely, spot on help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-29 : 12:32:03
welcome
but is it really that you're looking at?
what if you've multiple records with non blank lifted by value?
in that case which one you want to return?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -