Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Is it possible to count a sum of several rows with just one SQL command?For example table like this:
column1|column2--------------- 50 | text 3 | text 18 | text
I have tried doing it with this kind of SQL command, but it didn't work:
SELECT DISTINCT SUM(column1) AS [colsum], column2 FROM tableGROUP BY column1, column2;
For me that just gives me all rows. It SHOULD give me this kind of result: 61 | text
JimL
SQL Slinging Yak Ranger
1537 Posts
Posted - 2005-06-07 : 15:31:56
IF column2 is not equal your statement will return your example.GROUP BY column1, column2If you need the sum of column1 with each of column2Set nocount on Declare @column1 intSelect @column1 = Sum(column1) From yourtableSelect @column1 as sumofcolumn1, Column2From yourtableJimUsers <> Logic
Bee-Z
Starting Member
6 Posts
Posted - 2005-06-20 : 10:56:31
quote:Originally posted by keripukki
SELECT DISTINCT SUM(column1) AS [colsum], column2 FROM tableGROUP BY column1, column2;
SELECT DISTINCT SUM(column1) AS [colsum], column2 FROM tableGROUP BY column2