Tôi có bảng sau:SQL Server xoay ... Tôi có thể hiển thị các lăm đơn đặt hàng cuối cùng cho một loạt các loại lệnh
create table myorders(ordertype char(1), orderdate datetime, orderid int)
bảng này có các dữ liệu sau:
insert into myorders(ordertype, orderdate, orderid) values('P', '2013-02-14 20:04:48:287', 11082360)
insert into myorders(ordertype, orderdate, orderid) values('P', '2013-02-14 20:02:40:407', 40087130)
insert into myorders(ordertype, orderdate, orderid) values('P', '2013-02-14 20:02:07:277', 7990558)
insert into myorders(ordertype, orderdate, orderid) values('C', '2013-02-14 19:58:46:097', 8225181)
insert into myorders(ordertype, orderdate, orderid) values('P', '2013-02-14 19:58:39:740', 40087129)
insert into myorders(ordertype, orderdate, orderid) values('C', '2013-02-14 19:57:33:063', 8225235)
insert into myorders(ordertype, orderdate, orderid) values('C', '2013-02-14 19:56:17:207', 8225233)
insert into myorders(ordertype, orderdate, orderid) values('C', '2013-02-14 19:54:50:630', 8225232)
insert into myorders(ordertype, orderdate, orderid) values('P', '2013-02-14 19:48:07:300', 11082337)
insert into myorders(ordertype, orderdate, orderid) values('P', '2013-02-14 19:47:49:997', 40087128)
insert into myorders(ordertype, orderdate, orderid) values('P', '2013-02-14 19:46:40:667', 40087127)
insert into myorders(ordertype, orderdate, orderid) values('C', '2013-02-14 19:45:32:550', 8225231)
insert into myorders(ordertype, orderdate, orderid) values('P', '2013-02-14 19:45:11:203', 11082326)
insert into myorders(ordertype, orderdate, orderid) values('C', '2013-02-14 19:44:57:990', 8225230)
insert into myorders(ordertype, orderdate, orderid) values('P', '2013-02-14 19:43:52:953', 40087126)
insert into myorders(ordertype, orderdate, orderid) values('C', '2013-02-14 19:43:20:853', 8225229)
insert into myorders(ordertype, orderdate, orderid) values('P', '2013-02-14 19:41:33:740', 11082319)
insert into myorders(ordertype, orderdate, orderid) values('C', '2013-02-14 19:41:19:853', 8225228)
insert into myorders(ordertype, orderdate, orderid) values('P', '2013-02-14 19:40:33:127', 40087125)
insert into myorders(ordertype, orderdate, orderid) values('P', '2013-02-14 19:40:25:537', 40087124)
Các dữ liệu trông như thế này:
OrderType OrderDate OrderId
-------- ----------------------- ---------
P 2013-02-14 20:04:48.287 11082360
P 2013-02-14 20:02:40.407 40087130
P 2013-02-14 20:02:07.277 7990558
C 2013-02-14 19:58:46.097 8225181
P 2013-02-14 19:58:39.740 40087129
C 2013-02-14 19:57:33.063 8225235
C 2013-02-14 19:56:17.207 8225233
C 2013-02-14 19:54:50.630 8225232
P 2013-02-14 19:48:07.300 11082337
P 2013-02-14 19:47:49.997 40087128
P 2013-02-14 19:46:40.667 40087127
C 2013-02-14 19:45:32.550 8225231
P 2013-02-14 19:45:11.203 11082326
C 2013-02-14 19:44:57.990 8225230
P 2013-02-14 19:43:52.953 40087126
C 2013-02-14 19:43:20.853 8225229
P 2013-02-14 19:41:33.740 11082319
C 2013-02-14 19:41:19.853 8225228
P 2013-02-14 19:40:33.127 40087125
P 2013-02-14 19:40:25.537 40087124
tôi muốn trục dữ liệu để nó trông như thế này:
C_LastFiveOrders C_OrderDate P_LastFiveOrders P_OrderDate
---------------- ----------------------- ---------------- -----------------------
8225181 2013-02-14 19:58:46.097 11082360 2013-02-14 20:04:48.287
8225235 2013-02-14 19:57:33.063 40087130 2013-02-14 20:02:40.407
8225233 2013-02-14 19:56:17.207 7990558 2013-02-14 20:02:07.277
8225232 2013-02-14 19:54:50.630 40087129 2013-02-14 19:58:39.740
8225231 2013-02-14 19:45:32.550 11082337 2013-02-14 19:48:07.300
Lưu ý rằng các đơn đặt hàng được sắp xếp theo thứ tự thứ tự trong thứ tự quyết định.
Tôi muốn có thể thêm các OrderTypes bổ sung. Tôi cũng muốn sự linh hoạt để xem xét các đơn đặt hàng x cuối cùng. Trong mẫu, tôi đang xem năm lệnh cuối cùng. Tôi muốn có thể xem 10 hoặc 20 đơn đặt hàng cuối cùng.
+1 Đối với bảng thử nghiệm và thiết lập dữ liệu! – Bryan
-1 vì không thử bất cứ điều gì! – Kermit
Tôi xứng đáng với điều đó.Tôi sẽ trở thành một guru Pivot như bluefeet. Họ sẽ gọi tôi là Pivot Ken LOL. – codingguy3000