Solved

Query MS Sql database with full outer join

Posted on 2014-02-18
5
198 Views
Last Modified: 2014-09-30
We have a table in a MS Sql database named GLDetail.  I want to be able to select and total the billing and/or cost for any one job_no or group of job_no's for any particular post_prd and post_yr or group of post_prd's and post_yr's from the table.  The billing and cost for each job should display on the same line of the resulting output.  I would like to do this in a View in the database.  I have attached a sample of the data.  This data only shows the year 2014 but there are multiple years I want to select from.  Cost is made up of the following acct_no's-- Is Null , '606' , '609' , '620' , '621' , '623' , '630' , '684' , '642' , '678' , '604' , '627' , '758' , '764' , '786' , '780' , '628' and the billing is made up of following acct_no's-- Is Null Or Between '500' And '539'.  I have used Access to create queries after linking the table from MS Sql in the past, but some jobs have billing and no cost or vice versa so I believe I need full outer join which is not available in Access.  I have limited experience working in MS Sql directly.  I have attempted to create a View in MS Sql database to do this but have been unsuccessful.  I would appreciate any suggestions on how to accomplish this.
SampleData.xlsx
0
Comment
Question by:dur2348
  • 3
  • 2
5 Comments
 
LVL 34

Expert Comment

by:PatHartman
Comment Utility
To simulate a full outer join when the database engine doesn't support it explicitly, use a union  query that unions a left join query, a right join query, and an inner join query.

The union query will not be updateable so this won't work for a form.  It only works for a report.

Select * From InnerJoinQuery
Union Select * From RightJoinQuery
Union Select * From LeftJoinQuery;

Unions are sensitive to column order and data type so make sure the three queries select the identical columns in the identical order.
0
 

Author Comment

by:dur2348
Comment Utility
Since MS Sql does support full outer join, I was hoping to be able to create View in MS Sql database that would accomplish this.
0
 
LVL 34

Expert Comment

by:PatHartman
Comment Utility
Sorry, I thought this was an Access question since that is the topic I found it under.  I don't know what MS SQL is.  Are you talking about SQL Server?


Here's a link to the help entry for Full Outer Join in SQL Server

ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.en/s10de_1devconc/html/58855df5-6620-479d-b662-611e94ce8e2b.htm
0
 

Author Comment

by:dur2348
Comment Utility
Yes, I should have said MS SQL Server.  I would like to create View that displays one row per job with summed cost and summed billing for that job.  Cut and pasted link came back not matching any documents.
0
 
LVL 34

Accepted Solution

by:
PatHartman earned 500 total points
Comment Utility
Here is a link to a different article with syntax and examples for all the join types.

http://www.mssqltips.com/sqlservertip/1667/sql-server-join-example/

If you are working with Access, you can use the method I recommended earlier.  If you are working in some other product, you'll need to use T-SQL to create the query.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

772 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

10 Experts available now in Live!

Get 1:1 Help Now