SQL Server syntax

jknj72
jknj72 used Ask the Experts™
on
I have this query but I only want the last comment returned from ItemComments table.
Could I do a MAX(ItemCommentID) to get the last comment back?

	SELECT WWT.WeekID, CONVERT(varchar(50), StartWorkDay, 101) + ' - ' + CONVERT(varchar(50), EndWorkDay, 101) as 'WeekRange', 
	CONVERT(varchar(50), StartWorkDay, 101) as 'StartWorkDay', StatusID, Comments, <--Need the last Comment entered???
		 ISNULL((SELECT COUNT(wid.WorkItemDetailID)
			 FROM WorkItem wi
				 INNER JOIN WorkItemDetails wid ON wi.WorkItemID=wid.WorkItemID
			 WHERE wi.WorkItemID = wwt.WorkItemID), 0) as 'WorkCount',
		ISNULL((SELECT COUNT(tid.TravelItemDetailID)
			 FROM TravelItem ti 
				 INNER JOIN TravelItemDetails tid ON ti.TravelItemID = tid.TravelItemID
			WHERE wwt.TravelItemID = ti.TravelItemID), 0) as 'TravelCount',
		 ISNULL((SELECT COUNT(iid.ImageItemDetailID)
			 FROM ImageItem ii 
				 INNER JOIN ImageItemDetails iid ON ii.ImageItemID = iid.ImageItemID
			  WHERE wwt.ImageItemID = ii.ImageItemID), 0) as 'ImageCount', WW.WorkWeekID as 'WeekKey', 
		CONVERT(varchar(50), StartDay, 101) as 'StartDay', CONVERT(varchar(50), EndDay, 101) as 'EndDay'
	FROM WorkWeek WW
	INNER JOIN WorkWeekTracker WWT ON WW.WorkWeekID = WWT.WorkWeekID
	LEFT JOIN ItemComments ic on wwt.WeekID = ic.WeekID and wwt.UserID=ic.UserID
	Where StatusID = @StatusID AND WWT.UserID = @UserID 

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Vitor MontalvãoIT Engineer
Distinguished Expert 2017

Commented:
Yes, you can but don't forget the respective GROUP BY clause.
Also, Comments shouldn't be a BLOB column. If it is you'll need to convert it first to VARCHAR datatype, something like:
CAST(Comments AS VARCHAR(MAX))

Open in new window

ste5anSenior Developer

Commented:
Just a comment:

Yes, you can do this, IF and only IF this value is strictly increasing. When I need to guess: ~90% of such values are not. Even when they should be. Cause it's hard to implement and requires strict process when working with such tables, e.g. correct usage of DBCC CHECKIDENT with RESEED.

Author

Commented:
Im not sure why you think the column is a BLOB? Its a varchar(255) and what Im for is how to get the records back with the last Comment entered for a specific week, if there is one!
JK
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Im returning Comments from the ItemComments table if there is a value. I want the last comment inserted, if there are multiple values
Vitor MontalvãoIT Engineer
Distinguished Expert 2017

Commented:
Im not sure why you think the column is a BLOB?
It was just an alert because isn't the first time that I would see a Comments or Observation column created as a text data type. You can ignore my comment since you have it defined as VARCHAR(255).
ste5anSenior Developer

Commented:
Is this addressed to my comment?

"Im returning Comments from the ItemComments table if there is a value. I want the last comment inserted, if there are multiple values "

The get the "last comment inserted", you need a sort criteria which gives you that. In over 20 years experience, I've even used this approach myself and failed. Cause there are for example restore scenarios, where you restore old data with new ID values which break the strictly increasing property. Thus MAX(ID) does not reliably work.

Also in parallel scenarios, there may not the last comment inserted is not necessarily that one you've expect.

Thus you should at least use a DateAdded column for your comment and use this column as primary sort column. This will reduce changes that changes in ID handling will result in wrong data drastically.

btw, such ID values are often artificial, thus it is so hard to implement the strictly increasing property, cause it is artificial alike.

Author

Commented:
I have a CreateDate column but I am trying to get the last ItemCOmmentID from the LEFT JOIN to ItemCOmments table

I need this to return the last comment so I tried this in my LEFT JOIN
LEFT JOIN ItemComments ic on Wwt.WeekID = ic.WeekID
AND ic.ItemCommentID = 
        (
           SELECT MAX(ItemCommentID) 
           FROM ItemComments i 
           WHERE wwt.CommentID = i.ItemCommentID
        )

Open in new window

Author

Commented:
FYI, ItemCommentID is primary key in ItemComments table
IT Engineer
Distinguished Expert 2017
Commented:
You'll need to add more columns to the WHERE clause so you know you're filtering the correct records:
LEFT JOIN ItemComments ic on Wwt.WeekID = ic.WeekID
AND ic.ItemCommentID = 
        (
           SELECT MAX(ItemCommentID) 
           FROM ItemComments i 
           WHERE i.WeekID = ic.WeekID
                AND i.UserID= ic.UserID
                AND i.CommentID = ic.ItemCommentID
        )

Open in new window

ste5anSenior Developer
Commented:
btw, SQL Server version? Using ROW_NUMBER() is in most cases faster than a correlated subquery.

Author

Commented:
I decided to separate out this logic instead of trying to do it this way. Thank you both for your help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial