?
Solved

Need help with a "Join" script

Posted on 2008-06-25
8
Medium Priority
?
215 Views
Last Modified: 2012-05-05
Trying to get info from from two tables using a join. Tables are Billing Header and Billing Detail. From billing header I want "billing" and "primary code" from Billing Detail I want "Service Date 1". Problem is.. in Billing Header there is one record per bill, in Billing Detail there can me multiple records per bill depending on how many line items were on the bill but the service date is the same for the entire bill. How do I script the query? There are two common colunms in each table, Chart and Billing.
0
Comment
Question by:wdabbs
[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
  • 4
  • 4
8 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 21868043
try this:

select b.billing, b.primarycode, bh.servicedate1 from billingheader b
join billingdetail bh on b.chart = bh.chart and b.billing = bh.billing
0
 

Author Comment

by:wdabbs
ID: 21868275
select Billing Header.Billing, Billing Header.Primary Code, Billing Detail.Service Date 1 from b
join Billing Detail bh on b.chart = bh.chart and Billing Detail.billing = Billing Header.billing   <- is this how it should look?
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21868301
no quite.  you'll need brackets around the table names if they are two separate words:

select b.Billing, b.Primary Code, bhService Date 1
from [Billing Header] b
join [Billing Detail] bh on b.chart = bh.chart and bh.billing = b.billing  
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:wdabbs
ID: 21868408
Getting select b.Billing, b.Primary Code, bhService Date 1
from [Billing Header] b
join [Billing Detail] bh on b.chart = bh.chart and bh.billing = b.billing
*** ERROR: Incorrect syntax near the keyword 'Primary'. ***

0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21868418
whoops...missed that one

select b.Billing, b.[Primary Code], bh.[Service Date 1]
from [Billing Header] b
join [Billing Detail] bh on b.chart = bh.chart and bh.billing = b.billing  
0
 

Author Comment

by:wdabbs
ID: 21868593
Thank you sir! You are most kind and generous with your knowledge :) So the need to "Billing Header" b and "Billing Detail" bh? is that just to adreviate?? and what is operator "on", what's is saying??
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 2000 total points
ID: 21868621
On is part of the ANSI join statement I used in the query.  As for b and bh, I used them as table aliases.
0
 

Author Closing Comment

by:wdabbs
ID: 31470675
Thanks!
0

Featured Post

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 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