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
 General SQL Server Forums
 New to SQL Server Programming
 adding rows please help!!!!!
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

kiridu
Starting Member

USA
4 Posts

Posted - 01/18/2013 :  23:35:59  Show Profile  Reply with Quote
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..

Edited by - kiridu on 01/18/2013 23:41:11

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/19/2013 :  02:17:13  Show Profile  Reply with Quote
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

USA
4 Posts

Posted - 01/19/2013 :  02:55:38  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 01/19/2013 :  03:12:02  Show Profile  Reply with Quote

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


------------------------------------------------------------------------------------------------------
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.03 seconds. Powered By: Snitz Forums 2000