Relatively new to SQL querying. I can successfully get results from a simple query that shows a customer number and a total dollars invoiced sorted highest dollar amount to lowest. I want to also display the customer name. The customer name, [Name]
, is in another table along with the customer number but the column name for customer number is different, ie. Table 1
is [Bill-to Customer No_]
and Table 2
is just [No_]
. How would I get the information from Table 2
to display in the same row with the customer number?
SELECT
[bill-to Customer No_]
,customer_name
FROM table1 AS a
INNER JOIN table2 AS b on a.[bill-to Customer No_]=b.No_
SELECT [Bill-to Customer No_], [Invoice Amount] AS amt, [Name]
FROM Table1 t1 JOIN Table2 t2
ON t1.[Bill-to Customer No_] = t2.[No_]
ORDER BY amt DESC;
I haven't grasped your column names yet, but hope you get the idea.
EDIT : (as per your new query)
SELECT [Sell-to Customer No_], [Name], SUM([Amount]) as "Total Dollars Spent"
FROM [Table 1 - LIVE$Sales Invoice Line] a JOIN [Table 2 - LIVE$Customer] b
ON a.[Sell-to Customer No_] = b.[No_]
WHERE [Source Code] = 'RENTAL' and [Sell-to Customer No_] != 'GOLF'
GROUP BY [Sell-to Customer No_], [Name]
ORDER BY SUM([Amount]) DESC;
You need to add [Name]
to the GROUP BY
clause as well. Remember you cannot SELECT
a column that's not a part of GROUP BY
unless it's being processed by a group function like [Amount]
is being processed by SUM()
.
select Bill-to, CustomerNo_ ,customer_name
from Table1 a
join Table2 b on a.CustomerNo_ = b.No_
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.