SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Other SQL Server 2008 Topics
 need help for a request
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

tngtng
Starting Member

7 Posts

Posted - 03/15/2010 :  10:11:49  Show Profile  Reply with Quote
Hello, I have 2 tables:

order: reference (primary key), date (datetime, may be null), provider_reference (int), amount (decimal(18,2)), status ('valided', 'accepted', 'saved', null)

provider: reference (primary key, which is order.provider_reference), annuel_amount(decimal(18,2))

I'd like to add all amounts (order.amount) of orders validated (order.status='validates') of the current year (something like year(order.date)=year(getdate())) & put this result into provider.annuel_amount.

How can I do, thanks for your help!

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 03/15/2010 :  10:20:22  Show Profile  Reply with Quote

update p
set p.annuel_amount=o.totamnt
from provider p
inner join (select reference,sum(amount) as totamnt
            from order
            where date >= dateadd(yy,datediff(yy,0,getdate()),0)
            and date < dateadd(yy,datediff(yy,0,getdate())+1,0)
            and status = 'validated'
            group by reference)o
on o.reference=p.reference


EDIT:Included missing column

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/


Edited by - visakh16 on 03/15/2010 10:44:57
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22744 Posts

Posted - 03/15/2010 :  10:43:26  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by visakh16


update p
set p.annuel_amount=o.totamount
from provider p
inner join (select reference,sum(amount) as totamnt
            from order
            where date >= dateadd(yy,datediff(yy,0,getdate()),0)
            and date < dateadd(yy,datediff(yy,0,getdate())+1,0)
            and status = 'validated'
            group by reference)o
on o.reference=p.reference


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Madhivanan

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

tngtng
Starting Member

7 Posts

Posted - 03/15/2010 :  11:48:40  Show Profile  Reply with Quote
oups, sorry, I mistaked: I have 3 tables:

order: reference (primary key), date (datetime, may be null), provider_reference (int), budget_reference (int), status ('valided', 'accepted', 'saved', null)

provider: reference (primary key, which is order.provider_reference), annuel_amount(decimal(18,2))

budget: reference (primary key, which is order.budget_reference), amount (decimal(18,2))

(amount is in another table, not in the order table). Please help me again!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 03/15/2010 :  11:53:42  Show Profile  Reply with Quote
update p
set p.annuel_amount=tmp.totamnt
from provider p
inner join (select o.reference,sum(b.amount) as totamnt
            from order o
            join budget b
            on b.reference  = o.reference 
            where o.date >= dateadd(yy,datediff(yy,0,getdate()),0)
            and o.date < dateadd(yy,datediff(yy,0,getdate())+1,0)
            and o.status = 'validated'
            group by o.reference)tmp
on tmp.reference=p.reference


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

tngtng
Starting Member

7 Posts

Posted - 03/17/2010 :  04:02:24  Show Profile  Reply with Quote
thank you so much!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 03/17/2010 :  04:28:35  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.24 seconds. Powered By: Snitz Forums 2000