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
 General SQL Server Forums
 New to SQL Server Programming
 Adding records

Author  Topic 

Vack
Aged Yak Warrior

530 Posts

Posted - 2008-12-03 : 13:01:52
Trying to figure out how to add two fields from different records together.

Working with an Order with multiple lines.
Table: OrderLine

Line_no item_no Unit_price User_field
1 ABC 2.00
2 UZ 1.50 Z

item UZ has user_field populated with a Z.

If I'm reading an order and come across an item with user_field = Z I want to take its unit price and add it to the line before it.

I'm actually working with Crystal Reports but I think if I can see some SQL syntax that can do this it would help me figure it out using Crystal.

My ultimate goal is to have 3.50 print along with Line_no 1.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-03 : 13:12:40
[code]UPDATE ol1
SET ol1.Unit_Price=ol1.Unit_Price + ol2.Unit_Price
FROM OrderLine ol1
JOIN OrderLine ol2
ON ol1.Line_no=ol2.Line_no-1
WHERE ol2.User_field='Z'
[/code]
Go to Top of Page
   

- Advertisement -