Solved

SQL Join query

Posted on 2014-03-12
2
253 Views
Last Modified: 2014-03-12
hello

can anyone help solve this t-sql query I have. I have 1 table with invoices for costs and another with invoices for revenue. I have a project code which is common to both tables. you might have different number of cost invoices to revenue

What I need is to show the costs invoices and revenue invoices in the same view, but not repeat any thing. I know I could use a GROUP BY and SUM, but the users need to see the invoice numbers.

See attached t-sql script to create a table to highlight my issue.

SQL resultt-sql-join-problem.sql
0
Comment
Question by:Graham_Highlander
[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 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 total points
ID: 39923819
First, thanks for providing the code to create the tables and test data.  Awesome!

How about something like this ?
SELECT     isnull(ic.Project,ir.Project) as ProjectCode , 
           ic.InvoiceNumber as CostInvoiceNumber,  
           ic.InvoiceAmount as CostAmount,  
           ir.InvoiceNumber AS RevInvoiceNumber , 
           ir.InvoiceAmount AS RevAmount
FROM       (select InvoiceNumber, Project, row_number() over(partition by Project order by InvoiceNumber) as InvItem, InvoiceAmount from Invoices_Cost) as ic
full outer join (select InvoiceNumber, Project, row_number() over(partition by Project order by InvoiceNumber) as InvItem, InvoiceAmount from Invoices_Rev) as ir
ON ic.Project = ir.Project and ic.InvItem = ir.InvItem

Open in new window


Produces:

ProjectCode CostInvoiceNumber CostAmount            RevInvoiceNumber RevAmount
----------- ----------------- --------------------- ---------------- ---------------------
aaa123      800               100.00                900              200.00
abc123      801               100.00                901              200.00
abc123      802               20.00                 NULL             NULL
ccc123      803               90.00                 902              20.00
ccc123      NULL              NULL                  903              200.00

(5 row(s) affected)

Open in new window


You may need some handling in there for the NULL's in the numerics.  I tried wrapping them in ISNULL functions, spitting out spaces in place of nulls but since they were numbers, they appeared as 0 instead.

Anyway, that should get you started.
0
 

Author Closing Comment

by:Graham_Highlander
ID: 39923850
steve - fantastic, this is exactly what I needed. I will find a way to resolve the nulls

regards, graham
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

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