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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Sum question

Author  Topic 

eurob
Posting Yak Master

100 Posts

Posted - 2005-08-30 : 09:36:40
I have two tables:
articles
article_id
weight

status
article_id
sold

I want to have the sum of weight minus the weight for items that are sold. I simplified my example, so the table structure might not make sense, but I just need to know the mechanism on how to to this.

select sum(weight) -
CASE
WHEN s.sold = TRUE then
--> how can I select the row that contains the sold item
e.g select weight from articles...etc.
ELSE
0
END
from articles
left outer join status s on s.article_id = a.article_id

Thanks,

robert

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-30 : 09:41:39
Post full table structures with some sample data and your expected result

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

eurob
Posting Yak Master

100 Posts

Posted - 2005-08-30 : 10:10:49
ok...

articles
article_id
weight

status
article_id
sold

Articles
article_id.....1
weight........10

article_id.....2
weight........20

article_id.....3
weight........30

statusses
article_id....2
sold.......TRUE

so my weight would be 10+20+30 - 20 = 40

Thanks


robert
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2005-08-30 : 10:24:53
select sum(a.weight) - sum(c.weight)
from articles a
left join status b on a.id = b.article_id
left join articles c on.a.id = c.id and c.sold = true
Go to Top of Page

eurob
Posting Yak Master

100 Posts

Posted - 2005-08-30 : 10:55:49
Thank you,

That is simpler than I thought.

robert
Go to Top of Page
   

- Advertisement -