SqlServer_临时表查询和内联子查询的使用;

来源:互联网 发布:淘宝退货和退款的区别 编辑:程序博客网 时间:2024/05/18 00:16
<span style="font-size:18px;">功能:查询订单表中每个顾客第一次下订单的订单号和时间;(1)、临时表使用USE AdventureWorks2008GOSELECT soh.CustomerID, MIN(soh.OrderDate) AS OrderDateINTO #MinOrderDatesFROM Sales.SalesOrderHeader sohGROUP BY soh.CustomerID;SELECT soh.CustomerID, soh.SalesOrderID, soh.OrderDateFROM Sales.SalesOrderHeader sohJOIN #MinOrderDates tON soh.CustomerID = t.CustomerIDAND soh.OrderDate = t.OrderDateGROUP BY soh.CustomerIDDROP TABLE #MinOrderDates(2)、内联子查询SELECT soh1.CustomerID, soh1.SalesOrderID, soh1.OrderDateFROM Sales.SalesOrderHeader soh1WHERE soh1.OrderDate  = (SELECT Min(soh2.OrderDate)FROM Sales.SalesOrderHeader soh2WHERE soh2.CustomerID = soh1.customerID)ORDER BY CustomerID;</span>

0 0