SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query to sum only few values in a same column


Query to sum only few values in a same column

Author
Message
park1432
park1432
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 110
Hello all,
I am kind of new to sql server and I am stuck on this. It may sound stupid and simple but here is my situation:
CREATE TABLE For_Test(ID int not null,Name varchar(255),Amount decimal(18,2))
INSERT INTO For_Test VALUES (1,'A',220)
,(2,'B',340)
,(3,'C',540)
,(4,'D',780)

I just want to add amount of ID 1,2 and 3 excluding 4. There will be more than four rows in a table. So, how to do this?
bitbucket-25253
bitbucket-25253
SSCertifiable
SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)SSCertifiable (7.8K reputation)

Group: General Forum Members
Points: 7777 Visits: 25280
Here is a quick method. NOT recommended for speed or flexibility ... If you would outline your requirements in greater detail perhaps some one can / will give you further assistance.

   CREATE TABLE For_Test(ID int not null,Name varchar(255),Amount decimal(18,2))
INSERT INTO For_Test VALUES (1,'A',220),(2,'B',340),(3,'C',540)
,(4,'D',780)

--I just want to add amount of ID 1,2 and 3 excluding 4.
SELECT SUM(Amount) FROM For_Test WHERE ID in (1,2,3)

--Different select statement but same result
SELECT SUM(Amount) FROM For_Test WHERE ID <> 4
--Yet a 3rd way
SELECT SUM(Amount) FROM For_Test WHERE ID < 4
--Yet a 4th way - using MOD function
SELECT SUM(Amount) FROM For_Test WHERE ID % 4 <> 0;
Result:
(No column name)
1100.00



If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
park1432
park1432
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 110
Thank you very much. This will work for me now.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84957 Visits: 41071
park1432 (2/3/2013)
Thank you very much. This will work for me now.


Since you're new, I have to ask, which one is the "This" you speak of and why did you choose it?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
jshahan
jshahan
Mr or Mrs. 500
Mr or Mrs. 500 (538 reputation)Mr or Mrs. 500 (538 reputation)Mr or Mrs. 500 (538 reputation)Mr or Mrs. 500 (538 reputation)Mr or Mrs. 500 (538 reputation)Mr or Mrs. 500 (538 reputation)Mr or Mrs. 500 (538 reputation)Mr or Mrs. 500 (538 reputation)

Group: General Forum Members
Points: 538 Visits: 2083
What I would ask is "What characteristic/attribute of the fourth record makes you want to exclude it?"
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84957 Visits: 41071
@park1432,

Two way street here. We're actually interested in your problem and would like a bit more info if, for nothing else, a little education on a strange request. ;-)

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search