If you want info_message to be less general, as for him, to switch to this when it is only 1 account, I CAN do this, but this will require a case logic, which, in my opinion, is not needed. It is up to you, though. Just let me know if you want me to change it.
DECLARE @Cnt_list VARCHAR(MAX) =
(
SELECT CAST(COUNT(*) AS VARCHAR(10)) + ' '
FROM Person
GROUP BY [Type]
ORDER BY [Type]
FOR XML PATH('')
)
SELECT @Cnt_list as cnt_list
Results:
cnt_list
2 1 1 1 1
Then for the second part:
SELECT 'There are ' + CAST(COUNT(*) AS VARCHAR(10)) + ' person(s) with a type of ' + CAST([type] AS VARCHAR(10)) + '(The first column value is: ' + CAST(COUNT(*) AS VARCHAR(10)) + ')' info_message
FROM Person
GROUP BY [Type]
Results:
info_message
There are 2 person(s) with a type of 1(The first column value is: 2)
There are 1 person(s) with a type of 2(The first column value is: 1)
There are 1 person(s) with a type of 3(The first column value is: 1)
There are 1 person(s) with a type of 4(The first column value is: 1)
There are 1 person(s) with a type of 5(The first column value is: 1)
source
share