Solved

Tricky SQL Server Query

Posted on 2014-04-08
5
812 Views
Last Modified: 2014-04-08
I have two tables.  One table has amount billed and the other table has amount paid.  The tables are joined on the InvoiceNumber.  The Billing table can have multiple rows for one Invoice Number.  The Paid table will have only 1 row for an invoice number.    This is an example from the billing table

Order_ID               PO_Number      Invoice_Date      Driver_ID      Revenue
12367483      4412251      2013-12-01 00:00:00.000      10622      159.87
12367483      4412251      2013-12-17 00:00:00.000      10622      35.00
12367483      4412251      2013-12-22 00:00:00.000      10622      43.88

This is an example from the paid table

Invoice_No      Invoice_Date      PaymentAmount
12367483                       113335      -15987

I am trying to get a query to return the following:

OrderId      Revenue      Invoice_Date          Invoice_NO          PaidDate          PaidAmount
12367483      159.87      1-Dec-13            12367483          20131201         -159.87
12367483      35             17-Dec-13                  
12367483      43.88      22-Dec-13                  


Is this possible?
0
Comment
Question by:sherbug1015
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39985919
you can do a plain "FROM billing LEFT JOIN payment ON ... " to return all rows of billing table even if there is no match on payment table
however, as you don't seem to have a unique link between the billing any payment table, this will be indeed quite tricky.
could you have more rows in payments than in invoice (like 1 invoice payed with 2 payments)?
0
 
LVL 6

Accepted Solution

by:
Dulton earned 500 total points
ID: 39985938
the join part is easy, but your invoice date data and formatting of payment amount in the [paid] table above are making me wonder if they are typos or indeed things to consider.

conceptually to get the output you want all you need is a left join from the billing table to the paid table like:

select b.order_id as OrderId, b.Revenue, Cast(b.Invoice_date AS DATE) AS invoice_date
, p.invoice_no, p.invoice_date, p.paymentamount
from billingtable as b
left join paidtable as p
on b.orderid = p.invoice_no
and b.invoice_date = p.invoice_date

Open in new window

but like I said, the formatting in your example make me wonder if this will work or if you just have a typo.
0
 

Author Comment

by:sherbug1015
ID: 39985942
Guy Hengel -

No there will only be one row in the paid table.  This is what this exercise is for is to find everything billed and what got paid.  Payments are made one time to one invoice and any subsequent billing on the same invoice gets ignored by the payment system and someone has to come in manually to reconcile.  This is an attempt at automating the manual process.
0
 

Author Comment

by:sherbug1015
ID: 39986154
Dulton - While the left join works when invoice number and invoice date are the same, there will be many occasions when the invoice date will not be the same.  

This is where I'm stuck.  Any ideas?
0
 
LVL 6

Expert Comment

by:Dulton
ID: 39986236
The only other thing I would know to do would be to order the billing table by date. Then just link the payments to the billings by the order_id and the first date.... since, as you indicated, the date won't always be the same.

;With InvOrder AS
(
select b.order_id, b.Revenue, Cast(b.Invoice_date AS DATE) AS invoice_date
,ROW_NUMBER() OVER(PARTITION BY b.order_id ORDER BY b.invoice_date) AS BillOrder
from billingtable as b)

SELECT iv.order_id, iv.Revenue, iv.invoice_date
, p.invoice_no, p.invoice_date, p.paymentamount
FROM InvOrder AS iv
left join paidtable as p
on iv.orderid = p.invoice_no
and iv.BillOrder = 1

Open in new window

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question