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
Solved

SQL Link SQL Statement to table to get a name

Posted on 2014-09-15
4
151 Views
Last Modified: 2014-09-16
Hi
How do I alter the following SQL so that it links to a table called Customers to get [Customer Name]
(The Table Customers has CustomerID,[Customer Name]....)

select
 CustomerID, sum(amount) amount
 from
  (
    select [Link ID] CustomerID, Amount * -1 As Amount from payments
    union all
    select customerId, [inv amt] from invoices
 ) SubQuery
 group by CustomerID
0
Comment
Question by:murbro
  • 2
  • 2
4 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40323896
select
  SubQuery.CustomerID, c.[Customer Name], sum(SubQuery.amount) amount
  from
   (
     select [Link ID] CustomerID, Amount * -1 As Amount from payments
     union all
     select customerId, [inv amt] from invoices
  ) SubQuery
inner join customers c on
    c.CustomerID = SubQuery.CustomerID
group by SubQuery.CustomerID
0
 

Author Closing Comment

by:murbro
ID: 40323991
Thanks very much
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40324098
You're welcome.

D'OH, I didn't add the name to the GROUP BY (just to be clear to anyone else looking at this):

group by SubQuery.CustomerID, c.[Customer Name]
0
 

Author Comment

by:murbro
ID: 40325153
Thanks. Picked that up. appreciate the help
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

829 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