加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

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

发布时间:2020-12-12 13:15:17 所属栏目:MsSql教程 来源:网络整理
导读:span style="font-size:18px;"功能:查询订单表中每个顾客第一次下订单的订单号和时间;(1)、临时表使用USE AdventureWorks2008GOSELECT soh.CustomerID,MIN(soh.OrderDate) AS OrderDateINTO #MinOrderDatesFROM Sales.SalesOrderHeader sohGROUP BY soh.Cu
<span style="font-size:18px;">功能:查询订单表中每个顾客第一次下订单的订单号和时间;
(1)、临时表使用
USE AdventureWorks2008
GO
SELECT soh.CustomerID,MIN(soh.OrderDate) AS OrderDate
	INTO #MinOrderDates
	FROM Sales.SalesOrderHeader soh
	GROUP BY soh.CustomerID;
SELECT soh.CustomerID,soh.SalesOrderID,soh.OrderDate
	FROM Sales.SalesOrderHeader soh
	JOIN #MinOrderDates t
	ON soh.CustomerID = t.CustomerID
	AND soh.OrderDate = t.OrderDate
	GROUP BY soh.CustomerID
DROP TABLE #MinOrderDates
(2)、内联子查询
SELECT soh1.CustomerID,soh1.SalesOrderID,soh1.OrderDate
	FROM Sales.SalesOrderHeader soh1
	WHERE soh1.OrderDate  = (SELECT Min(soh2.OrderDate)
				FROM Sales.SalesOrderHeader soh2
				WHERE soh2.CustomerID = soh1.customerID)
	ORDER BY CustomerID;</span>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读