I have one Item table of unique records, each with an ID, and then another table with communication history log entries, tied to the Item table by that ID. The communication logs have the associated Item ID, a communication date field, person's name, and text about the communication - I'm hoping to have a view that gets me the 3 most recent entries for each record in the original table. I'd like to get the fields from the 3 recent entries all joined together into one combined text field, formatted like {Date} ((Agent Name}): {Text} , then a carriage return, and the next two entries in the same formatting. So the view would have one concatenated field coming in from the Communication History table with all that, for each ID in the Item table.
In the view, I want each unique item in the LineLists table (Line_List_Num + Project for Primary Key), with the 3 most recent Communications table entries for that item, concatenated in descending order of Comm_Date, formatted as a longtext string that concatenates together the {Agent} and {Comm_Date}, and {Comm_Entry} text for those 3 entries, such as:
Comm_Date (Agent): Comm_Entry (carriage return)
Comm_Date (Agent): Comm_Entry (carriage return)
Comm_Date (Agent): Comm_Entry
Still need some sample data for the above tables - and expected results. Instead of real tables - it is much better to use temp tables (or table variables) and insert statements to insert test data into those tables.
Then - we can take those scripts and run them on our system to create a tested solution that works against the sample data.
If you are on SQL Server 2017 or later - lookup STRING_AGG. If on a prior version - then you would need FOR XML concatenation.
will this suffice? sample data from communications table, with the desired output coalescing the 3 most recent entries for an item (Line_List + Project)