Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with a sub-query

Status
Not open for further replies.

Sh4venwookiee

Technical User
Sep 6, 2007
1
US
Hi,

I need to run a query that has several columns that calculate the sum of that column, but filtered down.

Example:

select
Stations.StationName as "Station Name",
sum(itembalance.balanceonhand * Itemmaster.standardcost) as 'station total value',
sum(txcost)
from transactionhistory
join itemmaster on (itemmaster.itemnumber = transactionhistory.itemnumber)
join Itembalance on (ItemMaster.ItemID = ItemBalance.ItemID)
join Stations on (Stations.StationID = ItemBalance.StationID)
group by Stations.StationName

Now this works, but the second field needs to be filtered down as follows:

(select sum(txcost) from transactionhistory where txtype in (select txtype from transactionhistory where txtype='is') group by sourcestationname)

when I do this, i get an error "Subquery cannot return more than one row"

If i do not put the group by in the subquery, then i get the sum of all entries listed on every row. I need this to still be grouped by sourcestationname.

Any thoughts?

sorry if this is not explained very well...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top