Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:

SQL Link SQL Statement to table to get a name

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
Murray Brown
Asked:
Murray Brown
  • 2
  • 2
1 Solution
 
Scott PletcherSenior DBACommented:
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
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
0
 
Scott PletcherSenior DBACommented:
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
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks. Picked that up. appreciate the help
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now