How to SUM values in column where values in another column match.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Andy Sears

    How to SUM values in column where values in another column match.

    I am trying to SUM values in a column where values in another column are the same.

    Here is what I need, but I know the 5th line won't work:

    Code:
    UPDATE manufacturing_detail
    SET item_lead_time_1 = 
    (SELECT SUM(cycle_time)
    FROM routing_workcenter
    WHERE routing_workcenter.routing = routing_workcenter.routing)<---HELP!
    FROM manufacturing_detail, routing_workcenter
    WHERE manufacturing_detail.item = routing_workcenter.routing
    Last edited by Atli; Nov 1 '10, 04:41 PM. Reason: Please use [code] tags when posting code.
  • Sandeep M

    #2
    Try this

    Code:
    UPDATE manufacturing_detail 
    SET item_lead_time_1 =  
    (SELECT SUM(cycle_time) 
    FROM routing_workcenter 
    WHERE routing=manufacturing_detail.item)
    Last edited by MMcCarthy; Nov 2 '10, 11:47 AM. Reason: added code tags

    Comment

    • Andy Sears

      #3
      The problem I am having is comparing the same column in a single table, just for different rows. Not a column in one table to a column in another table. There will be multiple records with the same value in the routing column, when this occurs, I want to sum the values in the cycle_time column for those matching records.

      Comment

      • ck9663
        Recognized Expert Specialist
        • Jun 2007
        • 2878

        #4
        Could you post some sample data and the result you want the query to return?


        ~~ CK

        Comment

        • Andy Sears

          #5
          Here is a sample table:
          routing_workcen ter
          routing_id routing status cycle_time
          1 A A 10
          2 A A 12
          3 A A 15
          4 B A 10
          5 B A 10
          6 B A 10
          7 C A 15
          8 C A 10
          9 C A 25

          What I want that 5th line of code to do is SUM based on the same value in the routing column. The results I am looking for would be:
          A - 37
          B - 35
          C - 50

          I then want to update item_lead_time_ 1 from table manufacturing_d etail with these summed values.

          Comment

          • ck9663
            Recognized Expert Specialist
            • Jun 2007
            • 2878

            #6
            Where did you get the 35 for B? Or it should be 30 instead?

            ~~ CK

            Comment

            • Andy Sears

              #7
              Yes, it should be 30. Sorry.

              Comment

              • ck9663
                Recognized Expert Specialist
                • Jun 2007
                • 2878

                #8
                Here, read this...

                Happy Coding!!!

                ~~ CK

                Comment

                Working...