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.
| Author |
Topic |
|
pickslides
Starting Member
7 Posts |
Posted - 2012-07-26 : 01:37:29
|
Hi all, very new to SQL queriesI have the following code and output. I am trying to fliter some fields then sum a few with the remaining records Code:select * from TEMP_MQ1 where [TARIFF TYPE]='R' and Days >0 and KL >0 sum(TEMP_MQ1.[KL]) AS SumOfKL, sum(TEMP_MQ1.[DAYS]) AS SumOfDAYSFrom temp_mq1 Group by TEMP_MQ1.[ACCOUNT NUMBER]; Output:Msg 102, Level 15, State 1, Line 2Incorrect syntax near 'sum'. cheers, MQBe who you are and say what you feel, because those who mind don't matter and those who matter don't mind. |
|
|
sql-programmers
Posting Yak Master
190 Posts |
Posted - 2012-07-26 : 03:26:39
|
USE CPI_LCP_July6I think your data will be quote: [ACCOUNT NUMBER] [Days] [KL]1 2 11 3 22 6 23 8 13 2 1and your result will be [ACCOUNT NUMBER] [Days] [KL]1 2 11 3 2--------------------------------Total 5 3--------------------------------2 6 2--------------------------------Total 6 2--------------------------------3 8 13 2 1--------------------------------Total 10 2--------------------------------
So Use this query IF not exact result pls explain what yo want with example DATA,SELECT [ACCOUNT NUMBER] , [Days] , [KL]FROM ( SELECT 1 AS Sno , [ACCOUNT NUMBER] , [Days] , [KL] FROM TEMP_MQ1 WHERE [TARIFF TYPE] = 'R' AND Days > 0 AND KL > 0 UNION SELECT 2 AS Sno , [ACCOUNT NUMBER] , SUM(TEMP_MQ1.[KL]) AS [Days] , SUM(TEMP_MQ1.[DAYS]) AS [KL] FROM TEMP_MQ1 WHERE [TARIFF TYPE] = 'R' GROUP BY TEMP_MQ1.[ACCOUNT NUMBER] ) AS tblORDER BY [ACCOUNT NUMBER], SnoSQL Server Programmers and Consultantshttp://www.sql-programmers.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-26 : 10:13:41
|
looks like what you're after is thisselect TEMP_MQ1.[ACCOUNT NUMBER], sum(TEMP_MQ1.[KL]) AS SumOfKL, sum(TEMP_MQ1.[DAYS]) AS SumOfDAYSFrom temp_mq1 where [TARIFF TYPE]='R' and Days >0 and KL >0Group by TEMP_MQ1.[ACCOUNT NUMBER]; ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
pickslides
Starting Member
7 Posts |
Posted - 2012-07-26 : 19:44:39
|
quote: Originally posted by visakh16 looks like what you're after is thisselect TEMP_MQ1.[ACCOUNT NUMBER], sum(TEMP_MQ1.[KL]) AS SumOfKL, sum(TEMP_MQ1.[DAYS]) AS SumOfDAYSFrom temp_mq1 where [TARIFF TYPE]='R' and Days >0 and KL >0Group by TEMP_MQ1.[ACCOUNT NUMBER]; ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
hi there, thanks for your help, this query ran well. Also thanks to the help provided in post two. If I wanted to edit the query to change the condition of Days >0 to days are between 42 and 100 how would this work?Kind regards, MQBe who you are and say what you feel, because those who mind don't matter and those who matter don't mind. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-27 : 00:50:24
|
quote: Originally posted by pickslides
quote: Originally posted by visakh16 looks like what you're after is thisselect TEMP_MQ1.[ACCOUNT NUMBER], sum(TEMP_MQ1.[KL]) AS SumOfKL, sum(TEMP_MQ1.[DAYS]) AS SumOfDAYSFrom temp_mq1 where [TARIFF TYPE]='R' and Days >0 and KL >0Group by TEMP_MQ1.[ACCOUNT NUMBER]; ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
hi there, thanks for your help, this query ran well. Also thanks to the help provided in post two. If I wanted to edit the query to change the condition of Days >0 to days are between 42 and 100 how would this work?Kind regards, MQBe who you are and say what you feel, because those who mind don't matter and those who matter don't mind.
why dont you try the change and see first? just put what you told in suggestion like belowselect TEMP_MQ1.[ACCOUNT NUMBER], sum(TEMP_MQ1.[KL]) AS SumOfKL, sum(TEMP_MQ1.[DAYS]) AS SumOfDAYSFrom temp_mq1 where [TARIFF TYPE]='R' and Days BETWEEN 42 AND 100 and KL >0Group by TEMP_MQ1.[ACCOUNT NUMBER]; ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|