How can I use subquery to get the total price here?

sql multiple subqueries in (select statement)
select from subquery
sql subquery join
how to join two subqueries in sql
subquery in sql w3schools
how to use subquery in group by clause in sql server
how to join a table with a subquery
nested queries in sql w3schools

I have an orders, products and order details junction table. I need to find the total price of each order (price of all products combined for every order).

I need to output the order ID with the total price which is calculated by the quantity * price. Here is what I have so far but outputs the same ID's over and over. It needs to be a subquery so joins won't help me here.

Select  order_id, (order_qty * p.price) as Order_Total
    From    order_details od, product p
    Where   p.product_id In
                (Select od.product_id
                From    order_details);

Try this -

Select  o.order_id, sum((od.order_qty * p.price)) as Order_Total
From    orders o, order_details od, product p
Where   
o.order_id = od.order_id
and od.product_id = p.product_id
group by order_id
;

Output -

order_id    Order_Total
50125   82
50126   80
50127   196
50128   178
50129   92
50130   98

SQL Bible, SELECT v total.customername, vtotal.total price FROM vcustomertotals vtotal vcustomertotals wilebess) ORDER BY totalprice ASC; The subquery here returns​  The subquery can be nested inside a SELECT, INSERT, UPDATE, or DELETE statement or inside another subquery. A subquery is usually added within the WHERE Clause of another SQL SELECT statement. You can use the comparison operators, such as >, <, or =. The comparison operator can also be a multiple-row operator, such as IN, ANY, or ALL.

I'm answering the question for three reasons First, there should be an answer that uses proper, explicit JOIN syntax. Second, the answer should be syntactically correct. And third, the query can be simplified because the orders table is not needed

select od.order_id, sum(od.order_qty * p.price) as Order_Total
from order_details od join
     product p
     on od.product_id = p.product_id
group by od.order_id;

The Guru's Guide to Transact-SQL, The outer subquery takes this total, multiplies it by the book unit price, and adds the Here's the previous query rewritten as a join: SELECT t. title FROM titles t JOIN You could code this using a correlated sub- query or as a self-join. but the​  Subqueries can be used in different ways and at different locations inside a query: Here is an subquery with the IN operator. SELECT column-names FROM table-name1 WHERE value IN (SELECT column-name FROM table-name2 WHERE condition) Subqueries can also assign column values for each record:

First to answer your question you can use JOINS which reduces complexity, among with this you can use a GROUP BY plus a SUM to make it easier which will result in a query like this

SELECT A.id, B.name, SUM(B.qty * C.cost) AS total FROM A JOIN B ON A.id = B.a_id JOIN C ON C.id = A.c_id GROUP BY A.id

Im on a phone sorry if I am not specific in the query :)

As an advice, I would recommend you to save in Order Details the value of the Product when you write it. This simplifies the query and will keep your sales records constant in time otherwise they will change everytime you update your product price

The Power of Subqueries, A subquery is a SELECT statement that is nested within another T-SQL statement​. A subquery SELECT SUM(Betrag) Betrag FROM ExportKreditor Here we used the first subquery to return a column value in the final result set. The second​  Using a subquery, display the sales order IDs, order quantity, and unit price for all products that are silver or partially silver in color. Paste your SQL here. Paste a screen shot of your results here.

SQL Subquery | IN SELECT, SQL SELECT with Subquery and JOIN. SQL Count, Sum, Avg · SQL And Subqueries can be used in different ways and at different locations inside a query​: Subqueries (SQL Server) 02/18/2018; 21 minutes to read; In this article. APPLIES TO: SQL Server Azure SQL Database Azure Synapse Analytics (SQL DW) Parallel Data Warehouse A subquery is a query that is nested inside a SELECT, INSERT, UPDATE, or DELETE statement, or inside another subquery.

Sams Teach Yourself SQL in 21 Days, Here's another example of something not to do: SELECT * FROM ORDERS 0 The following query uses a correlated subquery to find the average total order for a PRICE) > Tip When using correlated subqueries with GROUP BY and  Summary: in this tutorial, we will show you how to use the MySQL subquery to write complex queries and explain the correlated subquery concept. A MySQL subquery is a query nested within another query such as SELECT, INSERT, UPDATE or DELETE. In addition, a subquery can be nested inside another subquery.

Using a Subqueries in the HAVING clause, Get Ready to Learn SQL Server 23: Using Subqueries in the HAVING Clause. HAVING For example, it is now possible to compare the average of a group to the overall average. Now here is the complete statement including the subquery: Here's generally how to select multiple columns from a subquery: SELECT A.SalesOrderID, A.OrderDate, SQ.Max_Foo, SQ.Max_Foo2 FROM A LEFT OUTER JOIN ( SELECT B.SalesOrderID, MAX(B.Foo) AS Max_Foo, MAX(B.Foo2) AS Max_Foo2 FROM B GROUP BY B.SalesOrderID ) AS SQ ON SQ.SalesOrderID = A.SalesOrderID.

Comments
  • Why does it have to be a sub-query?
  • ...'cause I said so. Don't answer a question with a question.
  • Did you run that somewhere? I'd just like to see it for myself
  • Please note that I used random names and dates since they do not matter in the final output.