Solved

SQL Link SQL Statement to table to get a name

Posted on 2014-09-15
4
150 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

776 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