Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Is This Complex Update Possible?

Is This Complex Update Possible?

Scheduled Pinned Locked Moved Database
questionannouncement
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    perlmunger
    wrote on last edited by
    #1

    I am trying to take the result of a caculation of two fields in one table and place it into a field in another table? Is this possible? I have two tables StoreItems and TicketItems. I need to get a unit cost from my StoreItems table by dividing the cost for an entire case (CaseCost) by the case count (CaseCount) and then placing that result into the Cost field of an item in the TicketItems table with a corresponding PLU (like an SKU). PLU and StoreID are the two fields in the tables that link them together. In a nutshell I need this: StoreItems.Cost = TicketItems.CaseCost / TicketItems.CaseCount Keep in mind that I need to run this on every record in the two tables so just passing in a StoreID and PLU is not enough. This has to be done across the board--that is, every TicketItems record needs to have its Cost column set when this has finished running. Thank you. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

    C D 2 Replies Last reply
    0
    • P perlmunger

      I am trying to take the result of a caculation of two fields in one table and place it into a field in another table? Is this possible? I have two tables StoreItems and TicketItems. I need to get a unit cost from my StoreItems table by dividing the cost for an entire case (CaseCost) by the case count (CaseCount) and then placing that result into the Cost field of an item in the TicketItems table with a corresponding PLU (like an SKU). PLU and StoreID are the two fields in the tables that link them together. In a nutshell I need this: StoreItems.Cost = TicketItems.CaseCost / TicketItems.CaseCount Keep in mind that I need to run this on every record in the two tables so just passing in a StoreID and PLU is not enough. This has to be done across the board--that is, every TicketItems record needs to have its Cost column set when this has finished running. Thank you. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Off the top of my head the following should do what you want. [EDIT] Oops. I made a mistake the Cost field you are updating is in the StoreItems table, the original query was updating the TicketItems table :-O [/EDIT]

      UPDATE StoreItems
      SET Cost = TicketItems.CaseCost / TicketItems.CaseCount
      FROM TicketItems, StoreItems
      WHERE TicketItems.PLU = StoreItems.PLU

      You can get more information in your SQL Server books online, look up the index for UPDATE, UPDATE (described) Does this help?


      "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

      P 1 Reply Last reply
      0
      • C Colin Angus Mackay

        Off the top of my head the following should do what you want. [EDIT] Oops. I made a mistake the Cost field you are updating is in the StoreItems table, the original query was updating the TicketItems table :-O [/EDIT]

        UPDATE StoreItems
        SET Cost = TicketItems.CaseCost / TicketItems.CaseCount
        FROM TicketItems, StoreItems
        WHERE TicketItems.PLU = StoreItems.PLU

        You can get more information in your SQL Server books online, look up the index for UPDATE, UPDATE (described) Does this help?


        "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

        P Offline
        P Offline
        perlmunger
        wrote on last edited by
        #3

        Actually, no you were right the first time. The cost field is in the TicketItems table not the StoreItems table. So what did that query look like? -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

        C 1 Reply Last reply
        0
        • P perlmunger

          Actually, no you were right the first time. The cost field is in the TicketItems table not the StoreItems table. So what did that query look like? -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Almost the same actually:

          UPDATE TicketItems
          SET Cost = TicketItems.CaseCost / TicketItems.CaseCount
          FROM TicketItems, StoreItems
          WHERE TicketItems.PLU = StoreItems.PLU

          I'm now a bit confused, which table do the CaseCost and CaseCount items reside? If it is StoreItems then the query will look like this instead:

          UPDATE TicketItems
          SET Cost = StoreItems.CaseCost / StoreItems.CaseCount
          FROM TicketItems, StoreItems
          WHERE TicketItems.PLU = StoreItems.PLU

          The second query assumes: TicketItems.PLU TicketItems.Cost StoreItems.PLU StoreItems.CastCost StoreItems.CaseCount PLU is the common field that joins the two tables. This update will update every row in the TicketItems table. Does this help? Or have I confused you in return?


          "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

          P 2 Replies Last reply
          0
          • C Colin Angus Mackay

            Almost the same actually:

            UPDATE TicketItems
            SET Cost = TicketItems.CaseCost / TicketItems.CaseCount
            FROM TicketItems, StoreItems
            WHERE TicketItems.PLU = StoreItems.PLU

            I'm now a bit confused, which table do the CaseCost and CaseCount items reside? If it is StoreItems then the query will look like this instead:

            UPDATE TicketItems
            SET Cost = StoreItems.CaseCost / StoreItems.CaseCount
            FROM TicketItems, StoreItems
            WHERE TicketItems.PLU = StoreItems.PLU

            The second query assumes: TicketItems.PLU TicketItems.Cost StoreItems.PLU StoreItems.CastCost StoreItems.CaseCount PLU is the common field that joins the two tables. This update will update every row in the TicketItems table. Does this help? Or have I confused you in return?


            "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

            P Offline
            P Offline
            perlmunger
            wrote on last edited by
            #5

            I think the second one is right. Let me try it out. Thanks a lot for your help. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

            1 Reply Last reply
            0
            • C Colin Angus Mackay

              Almost the same actually:

              UPDATE TicketItems
              SET Cost = TicketItems.CaseCost / TicketItems.CaseCount
              FROM TicketItems, StoreItems
              WHERE TicketItems.PLU = StoreItems.PLU

              I'm now a bit confused, which table do the CaseCost and CaseCount items reside? If it is StoreItems then the query will look like this instead:

              UPDATE TicketItems
              SET Cost = StoreItems.CaseCost / StoreItems.CaseCount
              FROM TicketItems, StoreItems
              WHERE TicketItems.PLU = StoreItems.PLU

              The second query assumes: TicketItems.PLU TicketItems.Cost StoreItems.PLU StoreItems.CastCost StoreItems.CaseCount PLU is the common field that joins the two tables. This update will update every row in the TicketItems table. Does this help? Or have I confused you in return?


              "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

              P Offline
              P Offline
              perlmunger
              wrote on last edited by
              #6

              It worked and I understand it. You're a life saver!! Thank you!! -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

              C 1 Reply Last reply
              0
              • P perlmunger

                It worked and I understand it. You're a life saver!! Thank you!! -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                perlmunger wrote: It worked and I understand it. Excellent. :-D


                "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

                1 Reply Last reply
                0
                • P perlmunger

                  I am trying to take the result of a caculation of two fields in one table and place it into a field in another table? Is this possible? I have two tables StoreItems and TicketItems. I need to get a unit cost from my StoreItems table by dividing the cost for an entire case (CaseCost) by the case count (CaseCount) and then placing that result into the Cost field of an item in the TicketItems table with a corresponding PLU (like an SKU). PLU and StoreID are the two fields in the tables that link them together. In a nutshell I need this: StoreItems.Cost = TicketItems.CaseCost / TicketItems.CaseCount Keep in mind that I need to run this on every record in the two tables so just passing in a StoreID and PLU is not enough. This has to be done across the board--that is, every TicketItems record needs to have its Cost column set when this has finished running. Thank you. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

                  D Offline
                  D Offline
                  dishanf
                  wrote on last edited by
                  #8

                  Yes . you can update from one or more table ., Try this sql. may be want to change some col names. I think you get the idea.. ;) UPDATE StoreItems SET Cost = T.CaseCost / T.CaseCount FROM StoreItems S INNER JOIN TicketItems T ON S.PLU = T.PLU HTH D!shan

                  D 1 Reply Last reply
                  0
                  • D dishanf

                    Yes . you can update from one or more table ., Try this sql. may be want to change some col names. I think you get the idea.. ;) UPDATE StoreItems SET Cost = T.CaseCost / T.CaseCount FROM StoreItems S INNER JOIN TicketItems T ON S.PLU = T.PLU HTH D!shan

                    D Offline
                    D Offline
                    dishanf
                    wrote on last edited by
                    #9

                    Oh.. sorry . already answered! .. net is too slow :( D!shan

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups