????????SQL???????Top??Apply
???????????? ???????[ 2015/12/8 11:44:14 ] ?????????????
?????????????д?ЩTop??Apply???????÷???????????????壬??????????Щ?????????????????????????????е????????
????Top??Apply?????????????緵??????????3????????????????????????????????????????????????????????????????????????????????id????????????????????????????????????????????????????
????????????????3?????????
SELECT empid ??
orderid ??
custid ??
orderdate ??
requireddate
FROM sales.orders AS o1
WHERE orderid IN ( SELECT TOP 3
orderid
FROM sales.orders AS o2
WHERE o2.empid = o1.empid
ORDER BY orderdate DESC ??
orderid DESC )
????????APPLY?????
SELECT e.empid ??
a.orderid ??
a.custid ??
a.orderdate ??
a.requireddate
FROM hr.employees AS e
CROSS APPLY ( SELECT TOP 3
orderid ??
custid ??
orderdate ??
requireddate
FROM sales.orders AS o
WHERE o.empid = e.empid
ORDER BY orderdate DESC ??
orderid DESC
) AS a
?????????employees ???empid???????empid???orders???????? ?ù????3??????????????????????????
??????????????????????????????????APPLY??????????????????ROW_NUMBER?????????????????????к????empid???з?????????orderdate desc?? orderid desc ??????????????????У?????к?С??????3???С?
???????£?
????SELECT orderid ??
????custid ??
????orderdate ??
????requireddate
????FROM ( SELECT orderid ??
????custid ??
????orderdate ??
????requireddate ??
????ROW_NUMBER() OVER ( PARTITION BY empid ORDER BY orderdate DESC ?? orderid DESC ) AS rownum
????FROM sales.orders
????) AS d
????WHERE rownum 3
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11