?
Solved

Tricky SQL Server Query

Posted on 2014-04-08
5
Medium Priority
?
845 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 2000 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

770 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