use AdventureWorks
go
select avg(t.total)
from (
select sum(sh.TotalDue) total,sh.CustomerID
from Sales.SalesOrderHeader sh
where OrderDate between '20040101' and '20041231'
group by CustomerID
) as t
declare @range datetime
set @range = '20030101'
select AVG(a.total) averageCustSpend,
(select COUNT(*)
from Sales.SalesOrderHeader
where OrderDate between @range and DATEADD(year,1,@range)
)as count
from(
select SUM(sh.TotalDue) total,sh.CustomerID
from Sales.SalesOrderHeader sh
where OrderDate between @range and DATEADD(year,1,@range)
group by CustomerID
)a
Wednesday, March 30, 2011
SQL Subqueries on the from clause
Subqueries on the From clause
Labels:
sql,
subqueries
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment