adding rows PLEASE HELP!!!

Answered adding rows PLEASE HELP!!!

  • Saturday, January 19, 2013 4:54 AM
     
     
    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..

All Replies

  • Saturday, January 19, 2013 6:32 AM
     
     

    where is your insert query?

    add row = insert



    signature

  • Saturday, January 19, 2013 6:47 AM
     
     Answered Has Code
    try this...replace test123 with your tablename...
    Select B.cost_ctr,part_Code,qty,Case when part_code=1389 then A.sum1 Else 0 End as sum1
    from dbo.test123 B INNER JOIN
     (Select SUM(qty)as sum1,Cost_Ctr from dbo.test123 WHERE part_code IN ('1389','11040') GROUP by Cost_ctr) A
     on A.Cost_Ctr=B.Cost_Ctr
    sample output 

    Hope it Helps!!


    • Edited by Stan210 Saturday, January 19, 2013 6:53 AM
    • Marked As Answer by kiridu Saturday, January 19, 2013 9:24 AM
    •  
  • Saturday, January 19, 2013 7:48 AM
    Moderator
     
     

    Pituach,

    This is an English language forum. Can you change your signature block to English? Thanks.


    Kalman Toth SQL 2008 GRAND SLAM
    Paperback: Pass SQL Exam 70-461 & Job Interview: Programming SQL Server 2012

  • Saturday, January 19, 2013 9:04 AM
     
     

    thanks Stan210,

    that did worked perfectly. now i have another question. what if i want to do the same thing with a different part_code, using the same table.

    I have part_code '1354' and would like to add its qty to '1404'.

    sample below

    cost_ctr part_Code qty sum1 
    2000 11040 100 0
    2000 1389 50 100
    3000 1389 750 275
    3000 11040 200 0
    2000 1404 500 600
    2000 1354 100 0
    3000 1404 500 600
    3000 1354 100 0

    thanks again for all your help...

  • Saturday, January 19, 2013 10:58 AM
     
     

    Pituach,

    This is an English language forum. Can you change your signature block to English? Thanks.

    Hi

    Unfortunately, The microsoft's forum/Board interface does not allow holds a different signature in various forums :-(. Since I am very active and mainly helps in numerous forums in hebrew and much less active in the english forums, I made a choice of the language in which I present the signature. More than that I also serve as the Editor/Moderator in hebrew forums (i have to choice one language) so the signature mainly for the Israeli users.

    When Microsoft will improve the interface and enable a different signature forums for Hebrew and English forums I will keep a separate signature probably :-)

    In any case the signature is just forum Usage instructions:
    HEBREW:

    * אני לא עובד במייקרוסופט וכמוני רוב האנשים שבאים לעזור בפורום. העזרה ניתנת בפורומים היא בהתנדבות מוחלטת! אנא התייחס לכך ב כל פנייה.
    * אם פתחת שירשור אז אתה אחראי עליו! אנא זכור לבדוק את התגובות עד לרגע שבחרת לסגור את השירשור. אני שים השקיעו זמן כדי להגיב לשאלה שלך. זו רק הגנות מינימלית להראות שקראת את התגובות ואולי גם לזרוק מילת תודה לפי שניסה לעזור.
    * בסיום כל שירשור יש לסגור את השירשור על ידי סימון תשובה אחת או יותר שקיבלת, כמו כן ניתן ומומלץ לסמן את כל התגובות המועילות בשירשור. בברכה, גלישה מהנה

    English Translation:

    * I do not working at Microsoft and so most of the people who come to help in the forum. the help is giving completly voluntary! Please treat it like this.
    * If you open a thread then you are responsible for it! Please remember to check the responses until you choose to close the thread. People invest time to respond to your question. it is Only minimal Fairness to show that you have read the comments and maybe throw a word of thanks.
    * Close each thread in the end by checking one or more replies you receive as unswer, and you can and should mark the entire helpful responses too.

    Sincerely, Surfing the Internet fun

    * I can add a comment in English: Proper user guide Instructions



    signature

    • Edited by pituach Saturday, January 19, 2013 11:12 AM
    •