You need to settle on a common data type for the columns that you are essentially merging in the
job value like
'programmer' cannot be converted to an
int of course.
job's string data type then. For example, if
job's data type is
nvarchar(50), use something like this:
Declare @grouBy varchar(50) Set @grouBy = 'job' -- or 'name' or 'jobCode' Select min(Name), min(job), min(jobCode), max(salary) from TempTable -- FORNOW: for quick testing/debugging --from ( -- select 'whatever' as Name, 'a' as job, 1 as jobCode, 1000 as salary -- union -- select 'who cares', 'b', 2, 2000) t Group by Name, Case when @grouBy = 'job' then job when @grouBy = 'jobCode' then cast(jobCode as nvarchar(50)) End /* FORNOW: quick testing/debugging results (No column name) (No column name) (No column name) (No column name) whatever a 1 1000 who cares b 2 2000 */