Basic need is if a record has an Attribute of ‘Urgent’, then the attributevalue should be displayed in the Urgent column. If the record has an attribute value of ‘closed’, then the attributevalue must be displayed in the ‘Closed’ column.
I have a query below. My problem is that among the results I am getting back, there are two records with the same RequesterID (one with a valid value in ‘Urgent’ column and one with a value in ‘Closed’ colum) My problem is that I need these two particular records to be displayed as one record.
Any ideas?
SELECT DISTINCT r.RequesterID, sr.ModifiedDate, p.FirstName + ' ' + p.LastName AS RequesterName, CASE WHEN sa.Attribute = 'Urgent' THEN sa.AttributeValue ELSE NULL END AS Urgent, CASE WHEN sa.Attribute = 'Closed' THEN sa.AttributeValue ELSE NULL END AS Closed FROM Requester AS r INNER JOIN SubRequester AS sr ON r.RequesterID = sr.RequesterID INNER JOIN SubRequesterAttribute AS sa ON sr.SubRequesterID = sa.SubRequesterID CROSS JOIN Personnel AS p WHERE (r.UserID = p.ContractorID OR r.UserID = p.EmployeeID) AND (sa.Attribute IN ('Urgent', 'Closed')) GROUP BY r.RequesterID, sr.ModifiedDate, p.FirstName, p.LastName, sa.Attribute, sa.AttributeValue
You will need to join to your sub requester attribute table to the query twice. One with the attribute of Urgent and one with the attribute of Close.
You will need to LEFT join to these for the instances where they may be null and then reference each of the tables in your SELECT to show the relevent attribute.
I also wouldn’t reccomend the cross join. You should perform your ‘OR’ join on the personnel table in the FROM clause rather than doing a cross join and filtering in the WHERE clause.
EDIT: Sorry, my first response was a bit rushed. Have now had a chance to look further. Due to the sub requester and the sub requester attribute both being duplicates you need to split them both up into a subquery. Also, your modified date could be different for both values. So i’ve doubled that up. This is completely untested, and by no means the ‘optimum’ solution. It’s quite tricky to write the query without the actual database to check against. Hopefully it will explain what I meant though.
SECOND EDIT: My last edit was that there were multiple SubRequesters as well as multiple Attribute, from your last comment you want to show all SubRequesters and the two relevent attributes? You can achieve this as follows.