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 |
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 ENDFinal Result. cost_ctr-----part_code-----qty-----Expr12000---------11040---------100-------03000---------11040---------200-------04000---------11040---------300-------05000---------11040---------400---------02000---------1389---------50---------1503000---------1389---------60---------2604000---------1389---------75---------3755000---------1389---------15---------415thanks.. |
|
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 MVPhttp://visakhm.blogspot.com/ |
|
|
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-----Expr12000---------11040---------100-------03000---------11040---------200-------04000---------11040---------300-------05000---------11040---------400-------02000---------1389--------- 50---------03000---------1389--------- 60---------04000---------1389--------- 75---------05000---------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 Expr1FROM Tabel_1 |
|
|
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,Expr1FROM(SELECT *,SUM(CASE WHEN part_code IN ('11040','1389') THEN qty ELSE 0 END) OVER (PARTITION BY cost_ctr) AS totalFROM table)t[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|