简单子查询
select * from (select custid, companyname from Sales.Customers where country = N'USA') as USACusts

关联子查询
select custid, orderid, orderdate, empid
from Sales.Orders as o1
where orderid = (select max(o2.orderid)
                    from Sales.Orders as o2
                    where o2.custid = o1.custid)

select orderid, custid, val,
cast(100 * val / (select sum(o2.val)
                    from Sales.OrderValues as o2
                    where o2.custid = o1.custid)
                    as numeric(5,2))
as pct
from Sales.OrderValues as o1
order by custid, orderid;

select custid, companyname
from Sales.Customers as c
where country = N'Spain' and exists
(select * from Sales.Orders as o where o.custid = C.custid)

高级子查询
select orderid, orderdate, empid, custid,
(
    select max(o2.orderid)
    from Sales.Orders as o2
    where o2.orderid < o1.orderid
)
as prevorderid
from Sales.Orders as o1

posted on 2013-11-05 09:41  逝者如斯(乎)  阅读(186)  评论(0编辑  收藏  举报