mercredi 16 décembre 2015

Merge multiple columns in one single column SQL

I'm having a hard time solving this, i hope someone can help me out with some tips or advice.

I have a Table in SQL with 3 columns:

Pencil Sales    Notebook Sales  Pen Sales
     1                2             3
     9                5             6
     7                8             9

I made a query using "Union all" with the sum of each column.

My query looks like this:

select sum(pencilsales) from table1 union all
select sum(notebooksales) from table1 union all
select sum(pensales) from table1

and it gives me the following:

(No Column Name)
       17           
       15     
       18   

But i wanna know if there's a way of sorting this new query by using "desc" or something like that and to add a new column saying which one is each row, like this:

      Sales        Name
       18       Pen Sales     
       17       Pencil Sales
       15       Notebook Sales

Hope you can help me out with ideas and thank you in advance :)

Aucun commentaire:

Enregistrer un commentaire