Click here to Skip to main content
15,881,044 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a beginner developing a WinForms application in Visual Studio 2015 using C# .Net connecting to a MS Access 2016 database. What I am trying to do is to add (SUM, I guess) the values of three columns: two from the same table and one column from another. The table structure is as follows:

-------------------------------------------------

Table Products: Product, Cost1, Cost2

Table OtherCosts: Cost3

-------------------------------------------------

I need a query that returns the following:

Product, AllCosts

-------------------------------------------------

Thank you very much for your time and help. I really appreciate it.

What I have tried:

SELECT Product
FROM tbl_products

UNION SELECT Price1 AS Price
FROM tbl_products

UNION SELECT Price2 AS Price
FROM tbl_products

UNION SELECT Proce3 AS Price
FROM tbl_othercosts;
Posted
Updated 14-Sep-18 13:42pm

Somehow you need to join both tables; as we do not know the structure of these tables and how they are really related, it is hard to provide a precise answer.
This could be something like:
SQL
SELECT
  a.Product
 ,a.Price1 + a.Price2 + b.Price3 as 'Total cost'
FROM
 tbl_Products a
 INNER JOIN tbl_othercosts b ON b.ForeignKey = a.PrimaryKey
 
Share this answer
 
Use the Sum function with the Join:

SELECT p.Product, SUM(oi.quantity * p.price) AS grand_total,
FROM ORDERITEM oi
    JOIN PRODUCT p ON p.id = oi.productid
        WHERE oi.orderid = @OrderId
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900