SQL query to report on what invoices have not been paid, ordered by invoice number - sql

new to SQL and only been doing it for a week and a half. So I apologise now for the question being simple or appearing to be stupid.
I want to present a report on invoices that have not been paid by a given date, ordered by invoice number.
This is how I have displayed, the paid invoices but without the given date.. How do I display, the invoices that have not been paid by say 31-MAR-14.
SELECT INVOICE.INVOICE_NUMBER, INVOICE.INVOICE_DATE, PAYMENT.PAYMENT_NO, PAYMENT.INVOICE_NUMBER
FROM INVOICE, PAYMENT
WHERE INVOICE.INVOICE_NUMBER = PAYMENT.INVOICE_NUMBER
ORDER BY INVOICE.INVOICE_NUMBER;

SELECT PAYMENT.PAYMENT_NO
FROM INVOICE, PAYMENT
WHERE INVOICE.INVOICE_NUMBER = PAYMENT.INVOICE_NUMBER
AND INVOICE.INVOICE_DATE = 'AAAAMMJJ'
ORDER BY INVOICE.INVOICE_NUMBER;
Try something like this.
I have question. How do you say the invoice are not paid ?

You need to join the two tables together with a LEFT JOIN, then look to see where the payment date is > 31-MAR-14. Try something like this:
SELECT INVOICE.INVOICE_NUMBER, INVOICE.INVOICE_DATE, PAYMENT.PAYMENT_NO, PAYMENT.INVOICE_NUMBER
FROM INVOICE
LEFT JOIN PAYMENT ON INVOICE.INVOICE_NUMBER = PAYMENT.INVOICE_NUMBER
WHERE PAYMENT.Date_of_payment > '3/31/2014'
ORDER BY INVOICE.INVOICE_NUMBER;
This looks for all payments that were made after 3/31/2014. You may need to add another condition, that limits what invoices you're looking for.
To check payments that have not been made, look where any field in the PAYMENT table is null. SQL like this:
SELECT INVOICE.INVOICE_NUMBER, INVOICE.INVOICE_DATE, PAYMENT.PAYMENT_NO, PAYMENT.INVOICE_NUMBER
FROM INVOICE
LEFT JOIN PAYMENT ON INVOICE.INVOICE_NUMBER = PAYMENT.INVOICE_NUMBER
WHERE INVOICE.INVOICE_DATE = '3/31/2014'
AND PAYMENT.PAYMENT_NO IS NULL
ORDER BY INVOICE.INVOICE_NUMBER;
LEFT JOIN in this case will always return values for the INVOICE table, but may return NULL values in place of the PAYMENT if a payment has not been entered yet. Checking AND PAYMENT.PAYMENT_NO IS NULL will tell you that a payment has not been made yet.

Related

Calculate differences between two columns of two different tables

I want to calculate the difference between purchase order amount and purchase invoice amount. But I am not able to fetch the purchase invoice amount i.e. "pi.grand_total" and hence also not able to fetch the difference of "(pi.grand_total - po.grand_total)" . Please help.Below is my query.
PO = Purchase Order
PI = Purchase Invoice
SELECT DISTINCT
po.name AS "PO #:Link/Purchase Order:120",
po.supplier AS "Supplier:Link/Supplier:120",
po.Company AS "Company:Data:120",
po.currency AS "Currency:Link/Currency:120",
po.base_grand_total AS "Grand Total:Currency:120",
po.status AS "Status:Data:120",
po.per_received AS "Per Received:Data:120",
CEILING(po.per_billed) AS "Per Billed:Data:120",
po.delivery_date AS "Delivery Date:Date:120",
pi.grand_total AS "Final PI Total:120",
(pi.grand_total - po.grand_total) AS "Amount Difference:120"
FROM
"tabPurchase Order" as po
LEFT JOIN "tabPurchase Invoice" as pi ON po.name = pi.parent
WHERE
po.per_received = 100
AND
CEILING(po.per_billed) < 100
ORDER BY po.delivery_date ASC
You are using LEFT JOIN. This means when your second table has no data which matches with your first table you will receive no data from second table but nonetheless your first table will return all of its data.
You probably should check your join condition. If you wanted to join these two tables the way you want then use NVL Function for pi.grand_total column. Because it is on the left join it could have a NULL value thus NULL minus po.grand_total will give you NULL. NVL function turns NULL values into intended values like NVL(pi.grand_total,0)
A good example how joins work

Access 2013 SQL, three tables, two using sum wrong results

Can someone please help me with this issue? I've scoured the Internet looking at dozens of examples, but i just can't find a solution that works.
I am using Access 2013. The problem is that I am trying to make a query that will highlight all part numbers from a supplier that either has customer back orders and/or overdue deliveries.
I am using three tables:
tbl_Inventory_Master which I require the part number, on hand stock value, and the supplier code.
For any back orders I need to join the tbl_Customer_Back_Order table as I need the count of back order lines and the sum of the back order quantity.
If the supplier has a late delivery, then I need to add the tbl_On_Order table showing the count of overdue deliveries and the sum of the overdue quantities.
The query is retrieving the data but the returned quantities are double what they should be.
SELECT
I.Inventory_Part_Num, I.Description, I.On_Hand_Stock,
COUNT (B.Part_Number) AS Back_Order_Count, SUM(B.Back_Order_Qty) as BO_Qty,
COUNT(O.Part_Number) AS Late_Deliveries_Count, SUM(O.Order_Qty) AS Late_Qty
FROM (tbl_Inventory_Master AS I
LEFT OUTER JOIN tbl_Customer_Back_Order AS B
ON I.Inventory_Part_Num = B.Part_Number)
LEFT OUTER tbl_On_Order AS O
ON I.Inventory_Part_Num = O.Part_Number
WHERE
I.Customer_Code = '274' AND
O.Due_Date < [ENTER TODAYS DATE IN FORMAT DD/MM/YYYY]
GROUP BY I.Inventory_Part_Num, I.Description, I.On_Hand_Stock
For example, for the part number 2022940 I should have 10 back order lines and an overdue quantity of 43. Instead, the query is returning 20 back order lines and an overdue quantity sum of 86.
From the on order table I have three orders totaling 144 pieces, instead the query is returning 960.
Can someone please advise, as this is driving me crazy?
You are joining along unrelated dimensions, so you need to aggregate before joining:
SELECT I.Inventory_Part_Num, I.Description, I.On_Hand_Stock,
B.Back_Order_Count, B.BO_Qty,
O.Late_Deliveries_Count, O.Late_Qty
FROM (tbl_Inventory_Master AS I LEFT OUTER JOIN
(SELECT B.Part_Number, COUNT(*) as Back_Order_Count,
SUM(B.Back_Order_Qty) as BO_Qty
FROM tbl_Customer_Back_Order AS B
GROUP BY B.Part_Number
) as B
ON I.Inventory_Part_Num = B.Part_Number
) LEFT JOIN
(SELECT O.Part_Number, COUNT(O.Part_Number) AS Late_Deliveries_Count,
SUM(O.Order_Qty) AS Late_Qty
FROM tbl_On_Order AS O
WHERE O.Due_Date < [ENTER TODAYS DATE IN FORMAT DD/MM/YYYY]
GROUP BY O.Part_Number
) as O
ON I.Inventory_Part_Num = O.Part_Number
WHERE I.Customer_Code = '274';
Notice the outer aggregation is no longer needed.

Remove Row Data If Qty Zero

I have a data like this in Table Receipts, for the plus value is the amount of receipt of goods while the negative value in case of returns.
In the picture above, the Receiver_No 01267A is the receipt of the first item with Qty_Received 1. Then there is the return of goods on the receipt number of goods 01268A. I want this transaction not to show up.
Here's statement where i have tried.
select I2.Receiver_No,
I2.Purchase_order,
I2.PO_Line,
I2.Qty_received
from (Select Purchase_order,
PO_Line
from Receipts
group by Purchase_order,
PO_Line
having sum(qty_received) <>'0') I1 inner join
Receipts I2 On
I1.Purchase_Order = I2.Purchase_Order and
I1.PO_Line = I2.PO_Line
But the result did not meet with i want.
Please Help.
This will work for the very limited data that you have provided. If you care to provide more information about what the data looks like, then it will be easier to come up with a better solution.
If you have data with a lot of lines that have identical quantities, then this solution will not work.
SELECT
r.Receiver_No, r.Purchase_order, r.PO_Line, r.Qty_received
FROM
receipts r
WHERE
NOT EXISTS (SELECT
purchase_order
FROM
receipts s
WHERE
r.qty_received = -s.qty_received
)
If your requirement is to fetch rows with quantities less than one and not the negative ones :
SELECT * FROM Receipts WHERE ABS(QtyRecvd) >0 AND ABS(QtyRecvd) < 1
Try This
SELECT * FROM Receipts where Qty_received>0 AND Qty_received < 1

SQL Table Joining issue

I have a two tables: "invoice" as I and "Accounts Receivable" as AR.
AR table includes invoice issued (with id 0) and cash received (with id 4), while I table includes invoice amount column and adjustment column.
Apart from regular invoices and adjustments there are cases where adjustments were made to invoice and net affect is 0.00 on AR table. Plus sometimes, invoices are created and written off at invoice table before posting, so AR will have 0.00 amount in AR but I table has $100 in amount and -$100 in adjustment.
I am trying to create a query where it gives me invoice issued and cash received side by side and also create a new column that includes adjustment made for invoices with 0.00 balance in AR. Columns that might help:
AR.ID = unique ID
AR.ARinvnumber= Invoice number from Invoice table
Ar.Type= 0=invoice, 1 = payment received
Ar.Amount= ARamount saved from invoice
I.Id= unique ID
Invoice number = number of invoice
Invamount= Actual invoice amount
Inv Adjustment= Adjustment applied on invoice
Any idea how I can achieve that? I am able to match I and AR table and cash and AR from AR table
Select *
From (select ar.customerId, ar.customername,ar.invnumber ar.amount, i.invamout, i.invadjustment from Ar join I on ar.arinvnumber=i.invoicenumber where ar.artype=1) inv
join
select (select ar.customerId, ar.customername, ar.invnumber ar.amount, i.invamout, i.invadjustment from Ar join I on ar.arinvnumber=i.invoicenumber where ar.artype=1) cash
on inv.invnumber=cash.invnumber and inv.customerid=cash.customerid
after getting this, how can I include those invoices for whom adjustment were made but there was no AR because adjustment equals invoice amount.
Answer:
The following answer worked for me. Basically I wanted to include all the adjustments from invoice table including those ones which are not populated in AR table because the adjustments were made to clear client’s balance related to the work done after final invoice was issues. I used following query
Select *
From (select AR.ARInvnum as ARInvnum, AR.Arclientnumber as Aclient, sum(AR.Amount), I.Invoicenumber, sum(distinct(I.IAmount)), sum(I.IAdjust)
From AR.ARInvnum=I.Invoicenumber
Where ar.artype=0
Group by AR.ARInvnum, I.Invoicenumber, AR.Arclientnumber)AInvoice
Left join
(select AR.ARInvnum as PARInvnum, AR.Arclientnumber as PClient, sum(AR.Amount), I.Invoicenumber, sum (I.IAmount), sum(I.IAdjust)
From AR.ARInvnum=I.Invoicenumber
Where ar.artype=4
Group by AR.ARInvnum, I.Invoicenumber, AR.ARclientnumber)PInvoice
on
AInvoice.ARInvnum=PInvoice.PARInvnum
and
AInvoice.Aclient=PInvoice.PClient
Keep in Mind the Distinct clause in the first portion of the subquery removes any duplicates as well as sum them, so it will look like one summary related to particular invoice number. first portion of the subquery is summarizing based on the invoice issues and the adjustments related to the client. and second part is matching all payments received. I hope, this helps.
You need to include a limiting clause, also known as a where clause. MSDN documentation on select lays out the syntax as follows:
SELECT select_list [ INTO new_table ]
[ FROM table_source ] [ WHERE search_condition ]
[ GROUP BY group_by_expression ]
[ HAVING search_condition ]
[ ORDER BY order_expression [ ASC | DESC ] ]
Be aware, you will not need every expression to create a valid select statement. Also you do not need to join the same query to itself. Why not simplify by running the query by itself?
select
ar.customerId, ar.customername, ar.invnumber,
ar.amount, i.invamout, i.invadjustment
from Ar
join I on ar.arinvnumber=i.invoicenumber
where ar.artype=1
But your questions asks how to limit the results to only
those invoices for whom adjustment were made but there was no [account receivable]
Update your where clause to something like
where ar.artype=1 and i.adjustment is not null and i.adjustment = i.invamount
This will limit the results returned by the select statement to only those records that meet all three of the following criteria:
artype is equal to 1
adjustment is not null
adjustment is equal to invamount
If that result set is too narrow, tweak your where clause. I find it helpful to identify a single record that looks like other records I would like to find. Use that records unique identifier as a test for whether or not your query is working like you expect.

SQL Query Daily Sales Per Month of a specific product

I've got a query which gets the lists of customers who've made a purchase of the product for the current day
select Customer.customerName, sum(InvoiceDetail.itemPrice * InvoiceDetail.itemQuantity) as dailyPurchase from Invoice
inner join InvoiceDetail on Invoice.invoiceID = InvoiceDetail.invoiceID
inner join Customer on Invoice.customerID = Customer.customerID
inner join Item on InvoiceDetail.itemID = Item.itemID
inner join Branch on Branch.branchID = Invoice.branchID
inner join Region on Region.regionID = Branch.regionID
where
Invoice.inactive = 0 and InvoiceDetail.inactive = 0
and Item.itemTypeID = 3
and Region.regionCode = 'CR'
and cast(Invoice.invoiceDate as date) = convert(date, '01/08/2016', 103)
group by Customer.customerName
What I need is a table with a list of all the dates in the current month, listing ALL customers who have at least purchased the product ONCE. It should resemble something similar to this image here.
Any help on how to get started or a general idea of how to get the desired result, is much appreciated. Thanks!
Sample Data from Results:
customerName dailyPurchase
AGH COMMUNICATIONS 450.00
ARIEL AMARCORD SHOP 285.00
AKN COMMUNICATION 300.00
AWSDAC TELECOMMUNICATION 2850.00
BARLEY MOBILE & SERVICES 285.00
Table Structure - I'm sorry, I don't know an easier way to copy this.
First get the customers who have purchased the product atleast once this month alongwith date.
Then use pivot to get the result in the form that you want (as seen in image). Search stackoverflow for pivot in sql server, you will get good info.
Give some information about the table structure and sample data and I might be able to help you with the query to get the results.