CHAPTER 4
Subqueries
141
This query produces the following output, shown here in abbreviated form.
orderid orderdate empid custid nextorderid
----------- --------------------------- ----------- ----------- -----------
10248 2006-07-04 00:00:00.000 5 85 10249
10249 2006-07-05 00:00:00.000 6 79 10250
10250 2006-07-08 00:00:00.000 4 34 10251
10251 2006-07-08 00:00:00.000 3 84 10252
10252 2006-07-09 00:00:00.000 4 76 10253
...
11073 2008-05-05 00:00:00.000 2 58 11074
11074 2008-05-06 00:00:00.000 7 73 11075
11075 2008-05-06 00:00:00.000 8 68 11076
11076 2008-05-06 00:00:00.000 4 9 11077
11077 2008-05-06 00:00:00.000 1 65 NULL
(830 row(s) affected)
Notice that because there’s no order after the last, the subquery returned a NULL for the last
order.
Note that SQL Server 2012 introduces new window functions called LAG and LEAD that allow the
return of an element from a “previous” or “next” row based on specified ordering. I will cover these
and other window functions in Chapter 7.
Do'stlaringiz bilan baham: |