Solved

Tricky SQL Server Query

Posted on 2014-04-08
5
792 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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