Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Tricky SQL Server Query

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Detach & Attach 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.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

604 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