Alex Chaplianka
Alex Chaplianka

Reputation: 25

Remove a column after selection with SQL

I want my result set to include only one column, but I'm using a different column to group by and order by. Can I somehow, after selecting and order by removing the column from the result set?

Using MSSQL2008

Upvotes: 0

Views: 4805

Answers (2)

Alex
Alex

Reputation: 21766

You do not have to select all the columns your order or group by, you can just select the column you want.

SELECT A
FROM dbo.Table
GROUP BY A,B
ORDER BY A,B

Upvotes: 0

Radu Gheorghiu
Radu Gheorghiu

Reputation: 20489

Just add another SELECT around your query, like so:

SELECT
     sum_columnB
FROM
(SELECT 
    columnA
    , SUM(columB) sum_columnB
FROM Table
GROUP BY columnA
ORDER BY columnA
       , sum_columnB) resultset

But if you would post your query, my answer could be more specific and maybe clearer.

Upvotes: 5

Related Questions