You will need to select all your columns that are not part of the GROUP BY using an aggregate function such as AVG() or SUM() . Otherwise, the database does not know what to do with several records that are returned by grouped records.
For example, your select statement should start with something like this:
SELECT SUM(DVPname), AVG(RVPname),
If these are text entries, and you know that they are all the same, you can also add them to the GROUP BY .
GROUP BY DVPname, RVPname, AEname, Dealer, Product, Distribution
In addition, here is a good format for your group instruction:
GROUP BY CASE WHEN @SortColumn = '0' THEN [id] END, CASE WHEN @SortColumn = '1' THEN [name] END;
Matthew
source share