hi all,
I have a simple question: having a table where I have multiple orders for any number of vendors and a table with orders and amounts,
how do I write the query that sums up the amounts by vendor.
I tried:
select vnd.VENDOR,
sum(ord.Amount) as sumAmount
from TBL_Vendor vnd
inner join TBL_Orders ord
on vnd.ORDER_ID = ord.ID
group by VENDOR
order by VENDOR
but I am told " ... ord.Amount is invalid in the select list because it is not contained in either an aggregate function or the group by clause."
Again, the tables are as follows:
ID Amount Requester
=== ====== =======
123 $200.00 Jack
456 $300.00 John
789 $400.00 Jesse
OrderID Vendor VendorLocation
====== ===== ===========
456 VND1 LOC1
789 VND1 LOC1
123 VND2 LOC2
The result of the query should be:
Vendor sumAmount
===== =========
VND1 700.00
VND2 200.00
Thanks,
elsvieta
April 4, 2023 at 4:56 pm
In the following, you have an underscore in the code that does not exist in the TBL_Vendor table.
on vnd.ORDER_ID = ord.ID
--Jeff Moden
Change is inevitable... Change for the better is not.
April 4, 2023 at 5:03 pm
That would happen if you did not have ord.Amount wrapped in the aggregate SUM function.
It should work w/ the query listed.
If(OBJECT_ID('tempdb..#TBL_Orders ') Is Not Null)
Begin
Drop Table #TBL_Orders
End
CREATE TABLE #TBL_Orders
(
ID INT,
AMOUNT DECIMAL(19,2),
Requester NVARCHAR(100)
)
INSERT INTO #TBL_Orders
VALUES('123','200.00','Jack'),
('456','300.00','John'),
('789','400.00','Jesse')
If(OBJECT_ID('tempdb..#TBL_Vendor') Is Not Null)
Begin
Drop Table #TBL_Vendor
End
CREATE TABLE #TBL_Vendor
(
OrderID INT,
Vendor NVARCHAR(100),
VendorLocation NVARCHAR(100)
)
INSERT INTO #TBL_Vendor
VALUES('456','VND1','LOC1'),
('789','VND1','LOC1'),
('123','VND2','LOC2')
select vnd.VENDOR,
sum(ord.Amount) as sumAmount
from #TBL_Vendor vnd
inner join #TBL_Orders ord
on vnd.ORDERID = ord.ID
group by VENDOR
order by VENDOR
Allah bize yeter, O ne güzel vekildir.
vedatoozer@gmail.com
April 4, 2023 at 6:23 pm
Instead of the wordy
If(OBJECT_ID('tempdb..#TBL_Orders ') Is Not Null)
Begin
Drop Table #TBL_Orders
End
I recommend switching to the shorter and more intuitive
DROP TABLE IF EXISTS #TBL_Orders
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
April 4, 2023 at 6:41 pm
That would happen if you did not have ord.Amount wrapped in the aggregate SUM function.
It should work w/ the query listed.
It can't... there's no ORDER_ID column in the Vendor table. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
April 4, 2023 at 7:17 pm
ratbak wrote:That would happen if you did not have ord.Amount wrapped in the aggregate SUM function.
It should work w/ the query listed.
It can't... there's no ORDER_ID column in the Vendor table. 😀
The error the OP posted is "... ord.Amount is invalid in the select list because it is not contained in either an aggregate function or the group by clause."
April 4, 2023 at 7:21 pm
Jeff Moden wrote:ratbak wrote:That would happen if you did not have ord.Amount wrapped in the aggregate SUM function.
It should work w/ the query listed.
It can't... there's no ORDER_ID column in the Vendor table. 😀
The error the OP posted is "... ord.Amount is invalid in the select list because it is not contained in either an aggregate function or the group by clause."
Yep... I know that. The posted code will not produce that error.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply