-1

I have to modify an open source ERP for my job. It is developped in php / mysql. Theres a feature that calculates the customers outstanding debt. (I don't know if its the correct term : it is the money the client has to pay for products he allready ordered and recieved)

In the customer class, the outstanding debt is calculed as follows :

SELECT ref_contact, SUM (outstanding_debt) outstanding_debt
FROM (SELECT ref_contact, MAX(including_tax_and_VAT_amout) outstanding_debt
      FROM (SELECT ref_contact, SUM(ROUND(qty * unit_price)

and it continues. But the part I don't understand is right there : Its the SELECT statement with a SELECT in the FROM. I've never seen something like this. What does it means ?

Sherlock
  • 7,525
  • 6
  • 38
  • 79
duxkater
  • 23
  • 4
  • 2
    One select statement will return a table. Select outer to that will do the selection from that table and so on.. – arunrc Oct 24 '14 at 09:06
  • Possible Duplicate of http://stackoverflow.com/questions/12475850/how-can-an-sql-query-return-data-from-multiple-tables My second answer pretty much covers what a select statement inside different parts of a query can do. – Fluffeh Oct 24 '14 at 09:13
  • 1
    Why the downvotes? The title is a little broad, but the question itself looks legit. – Sherlock Oct 24 '14 at 09:17

1 Answers1

0

Those are sub queries. Instead of using SELECT [xyz] FROM [tablename], the sub query its result is used as "table" (data set).

See this question for detailed information on working with multiple tables in queries.

Elias
  • 1,532
  • 8
  • 19