本文介绍了如何在SQL Server中旋转表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有三列ticketid表,所有者,owendate as- ticketid所有者owndate 1001 LEWIS 2004-06-18 14:15:11 1001 WILSON 2004-06-18 14:16:54 1001 WILSON 2004-06-18 14:21:12 1001 (null)2004-09-01 09:56:11 1001 CALDONE 2005-02-02 08:38:28 1001 SINCLAIR 2005-02-02 08:54:02 1002 (null)2005-02-02 08:40:06 1002 REID 2015-01-16 15:18:05 1002 SMALL 2015-01-16 15:19:53 1002 WILSON 2004-06-18 14:33:47 1002(null)2004-08-31 15:12:46 1002(null)2004-09-24 10:03:09 1003 RAMSDALE 2004-09-24 10:04:24 1003 MOTIKA 2004-08-31 14:51:45 1003(null)2004-08-31 15:05:50 1003 MURTHY 2004-09-02 14:50:28 1004(null)2004-08-31 15:28:37 1004(null) 2004-09-24 09:24:21 1005 MOTIKA 2004-08-31 14:47:19 1005 MOTIKA 2004-08-31 17:20:33 1005(null) 2004-08-31 17:23:42 1005 WILSON 2004-09-02 16:29:43 1005 DALEY 2004-09-15 08:45:27 1005 SINCLAIR 2004- 09-15 08:34:34 我希望看到每张票的票价时差为...... ticketid owner1 owner2 ... 1001 datediff(owndate1-owndate2)datediff(owndate2-owndate3)... 解决方案 最后用我的要求解决了这个问题...... 选择 ticketid, owner1 = CONVERT( varchar ( 10 ),owner2-owner1 , 108 ), owner2 = CONVERT( varchar ( 10 ),owner3-owner2, 108 ), owner3 = CONVERT( varchar ( 10 ),owner4-owner3, 108 ), owner4 = CONVERT( varchar ( 10 ),owner4, 108 ) 来自 ( 选择 ticketid,owndate,' owner' + cast(ROW_NUMBER() over ( partition by ticketid order by owndate ) as varchar ( 10 )) as sno from tkownerhistory )tbl pivot ( max(owndat e) sno (owner1,owner2,owner3,owner4) )pvt I have table with three columns ticketid,owner,owendate as-ticketid owner owndate1001 LEWIS 2004-06-18 14:15:111001 WILSON 2004-06-18 14:16:541001 WILSON 2004-06-18 14:21:121001 (null) 2004-09-01 09:56:111001 CALDONE 2005-02-02 08:38:281001 SINCLAIR 2005-02-02 08:54:021002 (null) 2005-02-02 08:40:061002 REID 2015-01-16 15:18:051002 SMALL 2015-01-16 15:19:531002 WILSON 2004-06-18 14:33:471002 (null) 2004-08-31 15:12:461002 (null) 2004-09-24 10:03:091003 RAMSDALE 2004-09-24 10:04:241003 MOTIKA 2004-08-31 14:51:451003 (null) 2004-08-31 15:05:501003 MURTHY 2004-09-02 14:50:281004 (null) 2004-08-31 15:28:371004 (null) 2004-09-24 09:24:211005 MOTIKA 2004-08-31 14:47:191005 MOTIKA 2004-08-31 17:20:331005 (null) 2004-08-31 17:23:421005 WILSON 2004-09-02 16:29:431005 DALEY 2004-09-15 08:45:271005 SINCLAIR 2004-09-15 08:34:34And I want to see output as ticket time difference for each ticket as...ticketid owner1 owner2 ...1001 datediff(owndate1-owndate2) datediff(owndate2-owndate3) ... 解决方案 这篇关于如何在SQL Server中旋转表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 03:15