How to place 2 column in a SUM or GROUP BY SQL script

We need a sum result but need 2 column in the result.  We keep getting an error until we manage to place or unite column1 and column2 with “+”.  The problem is that when we place it in a an excel, we have to use a function (left, etc.) to extract the first column so it can be in separate column.

The working script is:
SELECT column1 + ' - ' + column2, sum(column3*column4) FROM table
GROUP BY column1 + ' - ' + column2
ORDER BY 1

the result is

Column1                        Column2
DataColumn1 - DataColumn2       0.00
DataColumn1 - DataColumn2       0.00
DataColumn1 - DataColumn2       0.00
DataColumn1 - DataColumn2       0.00
DataColumn1 - DataColumn2       0.00
DataColumn1 - DataColumn2       0.00

we would like script to result in:

Column1            Column2            Column3
DataColumn1      DataColumn2       0.00
DataColumn1      DataColumn2       0.00
DataColumn1      DataColumn2       0.00
DataColumn1      DataColumn2       0.00
DataColumn1      DataColumn2       0.00
DataColumn1      DataColumn2       0.00
rayluvsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lcohanDatabase AnalystCommented:
SELECT column1 + ' - ' + column2, sum(column3*column4) FROM [table]
GROUP BY column1,column2
ORDER BY 1
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Not entirely sure what you're asking, but you can have multiple columns selected to be aggregated, like..
SELECT column1, column2, sum(column3*column4) 
FROM [table]
GROUP BY column1, column2
ORDER BY column1, column2 

Open in new window

You can even have both columns plus the string of both...
SELECT column1, column2, column1 + ' - ' + column2 as both_columns, sum(column3*column4) 
FROM [table]
GROUP BY column1, column2
ORDER BY column1, column2 

Open in new window

btw I have an article out on Common GROUP BY solutions that would be a good read.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>DataColumn1 - DataColumn2
btw are these columns text or numeric?  If numeric, to display with a dash and not subtract 1 from two, you'll need to convert both to text
CAST(DataColumn1 as varchar(max)) + ' - ' + CAST(DataColumn2 as varchar(max))

Open in new window

0
rayluvsAuthor Commented:
Thanx it workd!
0
rayluvsAuthor Commented:
thanx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.