Solved

Tricky SQL Server Query

Posted on 2014-04-08
5
758 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
  • 2
  • 2
5 Comments
 
LVL 142

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
STDEVP in SQL 2 38
codeigniter query  returning too many results 3 32
Distinct values from two tables 14 20
Log Backup 2 13
Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now