简体   繁体   中英

subtract data of different 1st column and 2nd column, showing its answer in 3rd column

I am working on inventory management system. I don't know much about database quires. How to subtract data of 1st column with 2nd column and showing its result in 3rd column. For Example i am trying

Total Quantity      Use Packets          Remaining Packets
     300                50                    250
     250                10                    240
     240                40                    200

and so on..

the first column must contain the value of 3rd column. This all work has to be done in SQL database in visual studio. Hope so you understand my question.

Try This

    DECLARE @InventoryTotal AS TABLE
    ( 
      Quantity INT,
      UsePackets INT
    )
   INSERT INTO @InventoryTotal
   SELECT 300, 50 UNION ALL                  
   SELECT 250, 10 UNION ALL                 
   SELECT 240, 40    

   SELECT * FROM @InventoryTotal

    SELECT  Quantity,
            UsePackets ,
            (Quantity-UsePackets) AS RemainingPackets  
    FROM @InventoryTotal
    ORDER BY Quantity DESC

Result

Quantity    UsePackets  RemainingPackets
---------------------------------------
300             50          250
250             10          240
240             40          200

I would do it by coding if you are in Visual Studio. In my example I have a table called Data with the Columns TotalQuantity , UsePackets and RemainingPackets . I can update the Column RemainingPackets like this:

    using (var context = new PlutoContext())
        {

            foreach (var contextData in context.Datas)
            {
                contextData.RemainingPackets = contextData.TotalQuantity - contextData.UsePackets;
            }

            context.SaveChanges();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM