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 rows please help!!!!!

Author  Topic 

kiridu
Starting Member

4 Posts

Posted - 2013-01-18 : 23:35:59
Hello,

Im new to SQL, so any help would be awesome. Im trying to add rows with the same cost_ctr value, but i can't seem to figure out. below is my code and the table on how i want it to look.

here is my code:
CASE WHEN part_code = '1389' THEN (SELECT SUM(qty) FROM dbo.Table1 WHERE part_code IN ('1389' , '11040')) ELSE 0 END

Final Result.

cost_ctr-----part_code-----qty-----Expr1
2000---------11040---------100-------0
3000---------11040---------200-------0
4000---------11040---------300-------0
5000---------11040---------400---------0
2000---------1389---------50---------150
3000---------1389---------60---------260
4000---------1389---------75---------375
5000---------1389---------15---------415

thanks..

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-19 : 02:17:13
from where are you trying to insert? how is the data existing in your source? please post sample data.

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

Go to Top of Page

kiridu
Starting Member

4 Posts

Posted - 2013-01-19 : 02:55:38
sorry my first post was a bit confusing,

Here is what I’m trying to accomplish.

I have an existing table:

cost_ctr----part_code----- qty-----Expr1

2000---------11040---------100-------0
3000---------11040---------200-------0
4000---------11040---------300-------0
5000---------11040---------400-------0
2000---------1389--------- 50---------0
3000---------1389--------- 60---------0
4000---------1389--------- 75---------0
5000---------1389--------- 15---------0


If the selected part_code is ‘1389’ I want to take the available qty of part_code ‘11040’ and add it to the qty of 1389. Both part_code have the same cost_ctr.

here is my current code:

SELECT part_code, cost_ctr, qty, CASE WHEN part_code = '1389' THEN
(SELECT SUM(qty)FROM Tabel_1 WHERE part_code IN ('1389', '11040')) ELSE 0 END AS Expr1
FROM Tabel_1

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-19 : 03:12:02
[code]
SELECT cost_ctr,part_code, CASE WHEN part_code = '1389' THEN Total ELSE qty END AS qty,Expr1
FROM
(
SELECT *,SUM(CASE WHEN part_code IN ('11040','1389') THEN qty ELSE 0 END) OVER (PARTITION BY cost_ctr) AS total
FROM table
)t
[/code]

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

Go to Top of Page
   

- Advertisement -