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 |
|
nkz
Starting Member
1 Post |
Posted - 2009-11-23 : 14:26:49
|
| I'm beginner programmer.Please help me with correct query for this task:select * from ml_OrderHeaderOrder, Product, Qty, Price1 1010 4 881 1020 3 452 1010 4 90description: one delivery of two products in 4 and 3 boxes. The purchase price is in the last column.select * from ml_DespatchLinesId, Product, Price1002 1010 1151002 1010 1151052 1010 2001323 1020 651323 1020 65description: sold 5 boxes with price in the last column.The rest of boxes are stay in the stock. I would like to make report for all bought products (boxes) with selling price. Boxes from stock should have the price from last purchase price for right products.The final results should be like this:1 1010 1 1151 1010 2 1151 1010 1 2001 1010 1 901 1020 1 651 1020 1 651 1020 1 452 1010 1 902 1010 1 902 1010 1 902 1010 1 90 |
|
|
april198474
Starting Member
11 Posts |
Posted - 2009-11-24 : 06:27:40
|
| I am not so clear about third column in final results. I guess it means the numbers of box. If so, the "2" in the second row is not correct. I think it should be "1".Then the script is as follows:create table #temp(id int primary key identity(1,1),[order] int,product int,Qty int default(1),price int)declare @order int, @product int, @price int, @Qty int, @i intdeclare cursor_Qty cursor for(select Qty, [order], product from ml_OrderHeader)open cursor_Qtyfetch next from cursor_Qty into @Qty, @order, @productwhile @@fetch_status = 0beginselect @i = @Qtywhile @i<>0begininsert into #temp([order], product, price)(select [order], product, Price from ml_Orderheader where Qty = @Qty and [order] = @order)update #tempset price = (select max (Price) from ml_Orderheader where Qty = @Qty and product = @product)where #temp.product = @productselect @i = @i-1endfetch next from cursor_Qty into @Qty, @order, @productendclose cursor_Qtydeallocate cursor_Qtydeclare cursor_Change cursor for(select price, product from ml_Despatchlines)open cursor_Changefetch next from cursor_Change into @price, @productset @i = 0while @@fetch_status = 0beginupdate #temp set price = @price where id = (select top 1 id from #temp where price <> @price and product = @product and id >@i)set @i=@i+1fetch next from cursor_Change into @price, @productendclose cursor_Changedeallocate cursor_Changeselect [order], product, qty, price from #tempdrop table #tempGood Luck! |
 |
|
|
|
|
|
|
|