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

  • SEARCH
  • Home
  • 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 8878545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:44:13+00:00 2026-06-14T19:44:13+00:00

Just started an internship and have been tasked with some SQL. Finding it somewhat

  • 0

Just started an internship and have been tasked with some SQL. Finding it somewhat tricky. Appreciate any help. Thanks!

The task:

“Would it be possible to write an alert for the following. Show all customers who when Open sales orders due in the next month are combined (added) with open sales invoices, they exceed their credit limit.”

My proposed logic:

  1. First what I want to do is SUM all sales orders, within the next month, for each customer (in the SALES table).

  2. Next, SUM all open invoices for each customer, i.e. work out total amount each customer owes (in the INVOICES table).

  3. I then want to add the results of 1 and 2 together, by corresponding customer_id

  4. Next compare the calculation from 3 above to see whether its greater than each customers credit limit.

If the summed total in 3 exceeds the credit limit then only these companies should be displayed in the generated table. The ideal format for the resulting table would be

Cust_ID|Name|Sum_sales_orders (1.above)|Sum_open_invoices (2.above)| Total_orders&invoices
----------------------------------------------------------------------------
   1   |  A |  25000                   |  333                      | 25333 
       |    |                          |                           |

The code I have so far is

 SELECT arc.company, arc.credit_limit, 
     sum (ard.unit_price * ard.invoice_qty) as open_invoice_total, 
     sum (od.total_qty_ord * od.unit_price) as open_orders_total
 FROM iqms.arprepost_detail ard, iqms.arprepost arp, iqms.arcusto arc, iqms.ord_detail od, iqms.orders o
 WHERE ard.arprepost_id = arp.id 
     and arc.id = o.arcusto_id and o.id = od.orders_id
     and arp.arcusto_id = arc.id
  GROUP BY arc.company, arc.credit_limit

I think that this is calculating the correct totals in the sum () functions? or am i wrong? How can I add open_invoice_total and open_orders_total? and then compare them against the credit_limit?

I hope you guys understand what I am trying to do. Thanks for the help in advance! 🙂

  • 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-06-14T19:44:14+00:00Added an answer on June 14, 2026 at 7:44 pm

    You can use the HAVING clause to check whether the sum of the totals exceed the credit limit, like so. I’ve also replaced the joins in the WHERE clause with INNER JOINS, for readability sake.

     SELECT arc.company, arc.credit_limit, 
           sum(ard.unit_price * ard.invoice_qty) as open_invoice_total, 
           sum(od.total_qty_ord * od.unit_price) as open_orders_total
     FROM iqms.arprepost_detail ard 
            INNER JOIN iqms.arprepost arp ON ard.arprepost_id = arp.id
            INNER JOIN iqms.arcusto arc ON arp.arcusto_id = arc.id
            INNER JOIN iqms.ord_detail od ON o.id = od.orders_id
            INNER JOIN iqms.orders o ON arc.id = o.arcusto_id 
      GROUP BY arc.company, arc.credit_limit
      HAVING sum(ard.unit_price * ard.invoice_qty) + sum(od.total_qty_ord * od.unit_price) > arc.credit_limit;
    

    Edit

    Just to clarify Dems comment, GROUP BY doesn’t allow the specification of aliases, hence the above query ‘repeats’ the sum in the SELECT and in the HAVING. This can be eliminated with nesting, although note that the HAVING is switched with a WHERE. So a DRYer version of the query is:

    SELECT company, credit_limit, open_invoice_total, open_orders_total
    FROM
    (
         SELECT arc.company, arc.credit_limit, 
               sum(ard.unit_price * ard.invoice_qty) as open_invoice_total, 
               sum(od.total_qty_ord * od.unit_price) as open_orders_total
         FROM iqms.arprepost_detail ard 
                INNER JOIN iqms.arprepost arp ON ard.arprepost_id = arp.id
                INNER JOIN iqms.arcusto arc ON arp.arcusto_id = arc.id
                INNER JOIN iqms.ord_detail od ON o.id = od.orders_id
                INNER JOIN iqms.orders o ON arc.id = o.arcusto_id 
          GROUP BY arc.company, arc.credit_limit
    ) AS nested
    WHERE (open_invoice_total + open_orders_total) > arc.credit_limit;
    

    And, if you prefer, a CTE may make this more readable:

    ;WITH nested AS
    (
         SELECT arc.company, arc.credit_limit, 
               sum(ard.unit_price * ard.invoice_qty) as open_invoice_total, 
               sum(od.total_qty_ord * od.unit_price) as open_orders_total
         FROM iqms.arprepost_detail ard 
                INNER JOIN iqms.arprepost arp ON ard.arprepost_id = arp.id
                INNER JOIN iqms.arcusto arc ON arp.arcusto_id = arc.id
                INNER JOIN iqms.ord_detail od ON o.id = od.orders_id
                INNER JOIN iqms.orders o ON arc.id = o.arcusto_id 
          GROUP BY arc.company, arc.credit_limit
    )
    SELECT company, credit_limit, open_invoice_total, open_orders_total
      FROM nested
      WHERE (open_invoice_total + open_orders_total) > arc.credit_limit;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just started a tutorial in SQL for beginners. I'm doing some exercises now and
Just started my first WCF rest project and would like some help on what
Just started testing nodejs, and wanted to get some help in understanding following behavior:
Just started learning python (3.2) and have a question. I have created a some
Just started using carrierwave with Rails and things have been going smoothly with one
Just started learning C# (in xaml), so appreciate some elaboration: ((MSAvalon.Windows.Serialization.ILoaded)(_Text_2_)).DeferLoad(); Not sure what
Just started mongo and started having issue with querying already. i have a collection
Just started my first MVC 2.0 .net application. And I set up some default
Just started learning PySide and is having problem with QTimer I have this #!/usr/bin/python
Just started learning Scheme. I'm using Dr. Racket as my compiler/interpreter. I need some

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.