Re: SQL help
Posted in 1998
Rick Walrath wrote: > > Douglas Wilson wrote in message <6a7n0e$ggf$1@gte2.gte.net>... > >Rick Walrath wrote: > >> > >> I am a v5.1 trying to run a query that will contain a subquery utilizing > the > >> sum(field) function. It goes a little something like this... > >> > >> select > >> cust_name, > >> (select sum(sold_amt) from invoices > >> where MONTH(inv_date)=1 and YEAR(inv_date)=1998), > >> (select sum(profit_amt) from invoices > >> where MONTH(inv_date)=1 and YEAR(inv_date)=1998)) > >> from > >> invoices > >> group by 1 > >try: > >select > >cust_name, > >sum(sold_amt), > >sum(profit_amt) > >from > >invoices > >where MONTH(inv_date)=1 and YEAR(inv_date)=1998 > >group by 1 > What if I want to take the query another step further, to return both MTD > and YTD information......... Then you need something like your original query, but you need to join the subquery to the main query, but I think I'd do it this way (you can add profit_amt yourself): select cust_name, (select sum(sold_amt) from invoices t2 where MONTH(inv_date)=1 and YEAR(inv_date)=1998 and t1.cust_name=t2.cust_name), sum(sold_amt) where YEAR(inv_date)=1998 from invoices t1 group by 1 Hope that helps, Douglas Wilson