Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6350897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:53:55+00:00 2026-05-24T21:53:55+00:00

I have the following table structure: Customers – Cust_Orders – Cust_Items – Cust_Payments –

  • 0

I have the following table structure:

Customers  - Cust_Orders - Cust_Items  - Cust_Payments  -  Drivers
  id           id             id            id             id
  company      cid            oid           oid            name
               driver         price         amount
               date           qty           date
                              vat

What I want to do is showing last unpaid order marked by a specific driver id + the sum of all unpaid orders for that particular customer except the order that is already selected.

Since there might be more than one cust_items & more than one cust_payments I had to use select from select as otherwise I would have wrong sums & things got messy till I reached a point I forgot what I was doing.

Any Help would be greatly appreciated.

My current SQL which lacks the final part only (sum of other unpaid orders amounts):

SELECT `customers`.`company`, 
T1.*, 
ROUND( IFNULL( SUM(`cust_payments`.`amount`), 0 ), 2) AS `paid` 
FROM (
SELECT `cust_orders`.*, 
    ROUND( IFNULL( SUM(`cust_items`.`qty` * `cust_items`.`price`), 0 ), 2) AS `total`, 
    SUM( ( `cust_items`.`price` * `cust_items`.`qty` * `vat` ) / 100) AS `vat` 
    FROM `cust_orders` 
    LEFT JOIN `cust_items` ON `cust_orders`.`id` = `cust_items`.`oid` 
    GROUP BY `cust_orders`.`id`
) `T1` 
LEFT JOIN `customers` ON `T1`.`cid` = `customers`.`id` 
LEFT JOIN `cust_payments` ON `T1`.`id` = `cust_payments`.`oid` 
WHERE `T1`.`driver` = ? GROUP BY `T1`.`id` HAVING (`T1`.`total` - `paid`) > ? 
ORDER BY `T1`.`id` DESC  LIMIT 1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:53:55+00:00Added an answer on May 24, 2026 at 9:53 pm

    Can you try

    SELECT
        x.id,
        x.company,
        y.id,
        y.cid,
        y.driver,
        y.date,
        @ut:=ROUND(SUM(z.qty*z.price),2) AS unpaid_total,
        @uv:=SUM((@ut*z.vat)/100) AS unpaid_vat,
        @st:=ROUND(SUM(b.qty*b.price),2)-@ut AS sum_total,
        SUM((@st*b.vat)/100)-@uv AS sum_vat
    FROM Customers x
    INNER JOIN Cust_Orders y ON x.id=y.cid
    INNER JOIN Cust_Items z ON y.id=z.oid
    LEFT JOIN Cust_Orders a ON x.id=a.cid
    LEFT JOIN Cust_Items b ON a.id=b.oid
    WHERE
        y.driver=? AND
        NOT EXISTS (SELECT * FROM Cust_Payments WHERE oid=y.id) AND
        NOT EXISTS (SELECT * FROM Cust_Payments WHERE oid=a.id)
    GROUP BY x.id,x.company, y.id, y.cid, y.driver, y.date
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following MySQL table structure: num field company phone website 1 Gas
I have following table structure in my DB ID Name -------------------------- ID_1 Name1 ID1
I have the following table structure: <tr> <th><label for=ctl00_bodyContent_username>username:</label></th> <td class=field><input name=ctl00$bodyContent$username type=text id=ctl00_bodyContent_username
I have following table structure: CREATE TABLE pilot_groups ( id INT PK, name VARCHAR(50),
I have following table structure ID Name Parent_ID 1 abc 0 2 efg 1
I have the following table structure: table A --------- id | name table B
I have a MySQL 5.1 InnoDB table ( customers ) with the following structure:
I have the following table structure: Table Users ID | Name 1 | John
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I have the following table structure CREATE TABLE `table` ( `id` int(11) NOT NULL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.