简体   繁体   中英

How can I use the data returned from a sub-query?

I have a sub-query that just gets the sum of the number of items sold per itemID. I'd like to get the result of that sub-query and multiply it by the items price. But when I try to reference the column alias I used for the sub-query, Slq Server gives me an error saying the column I'm trying to use is invalid. Here's my sql...

SELECT     TOP (100) PERCENT Shop_1.ShopName, CustomerOrdersDtl_1.ItemID, ItemList_1.ItemName, SUM(CustomerOrdersDtl_1.Amount) AS tAmount
FROM         dbo.CustomerOrders AS CustomerOrders_1 INNER JOIN
                      dbo.CustomerOrdersDtl AS CustomerOrdersDtl_1 ON CustomerOrders_1.ID = CustomerOrdersDtl_1.CustomerOrdersID INNER JOIN
                      dbo.ItemList AS ItemList_1 ON CustomerOrdersDtl_1.ItemID = ItemList_1.ID INNER JOIN
                      dbo.Shop AS Shop_1 ON CustomerOrders_1.ShopID = Shop_1.ID
GROUP BY Shop_1.ShopName, CustomerOrdersDtl_1.ItemID, ItemList_1.ItemName, ItemList_1.Price
ORDER BY Shop_1.ShopName

The Column I'm trying to use is tAmount. I'd like to have another column after it, TotalCost, that shows the Price*tAmount. When I put (Itemlist_1.Price*tAmount) as TotalItemCost, I get an error saying tAmount is an invalid column.

Is it even possible to reference the data returned by a sub-query? Or is there another way to do this?

Thanks very much in advance for any and all help. :)

The tAmount column isn't available within the query. Something like SUM(CustomerOrdersDtl_1.Amount) * Itemlist_1.Price might work.

Otherwise try using a sub-select:

SELECT TOP (100) PERCENT sqry.ShopName, sqry.ItemID, sqry.ItemName, sqry.tAmount, sqry.Price*sqry.tAmount as TotalItemCost
FROM
(SELECT     Shop_1.ShopName AS ShopName, CustomerOrdersDtl_1.ItemID AS ItemID, ItemList_1.ItemName AS ItemName, SUM(CustomerOrdersDtl_1.Amount) AS tAmount, Itemlist_1.Price AS Price
FROM         dbo.CustomerOrders AS CustomerOrders_1 INNER JOIN
                      dbo.CustomerOrdersDtl AS CustomerOrdersDtl_1 ON CustomerOrders_1.ID = CustomerOrdersDtl_1.CustomerOrdersID INNER JOIN
                      dbo.ItemList AS ItemList_1 ON CustomerOrdersDtl_1.ItemID = ItemList_1.ID INNER JOIN
                      dbo.Shop AS Shop_1 ON CustomerOrders_1.ShopID = Shop_1.ID
GROUP BY Shop_1.ShopName, CustomerOrdersDtl_1.ItemID, ItemList_1.ItemName, ItemList_1.Price) as sqry
ORDER BY sqry.ShopName

You can't reference the column alias tAmount within the scope of the same query. You have to spell out the calculation in full again as you have it. If you want to use the column name tAmount, you have to use sub-queries:

This example breaks:

select 1 * 2 as tAmount, tAmount

This example works:

select tbl.tAmount, tbl.tAmount * 2 from 
( select 1 * 2 as tAmount ) as tbl

也许尝试:SUM(Itemlist_1.Price * CustomerOrdersDtl_1.Amount)AS TotalItemCost?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM