Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

get the price of last buy of each sell

$
0
0
Hi I need to get the sellprice of each buy corrosponding to sell , below is the table and required resulset.CREATE TABLE #temptable ( [Key] varchar(32), [KeyID] int, [KeyDescription] varchar(200), [KeyAmount] float(8), [KeyVolume] float(8), [SellPrice] float(8), [ExecutionTime] datetime, [BuySell] varchar(1) )INSERT INTO #temptableVALUES( '1ABC', 211, 'Inc.', -15, -1639.2, 109.28, N'2016-08-19 14:39:33.653', 'S' ), ( '1ABC', 211, 'Inc.', -10, -1087.4, 108.74, N'2016-08-19 14:04:56.000', 'S' ), ( '1ABC', 211, 'Inc.', 1, 109.24, 109.24, N'2016-08-18 17:38:40.000', 'B'), ( '1ABC', 211, 'Inc.', 9, 984.87, 109.43, N'2016-08-18 16:10:30.000', 'B' ), ( '1ABC', 211, 'Inc.', 100, 10948.99, 109.4899, N'2016-08-18 15:49:36.547', 'B' ), ( '1ABC', 211, 'Inc.', 20, 2186.8, 109.34, N'2016-08-18 14:29:30.000', 'B'), ( '1ABC', 211, 'Inc.', 40, 4373.2, 109.33, N'2016-08-18 13:44:46.643', 'B' ), ( '1ABC', 211, 'Inc.', 25, 2722.75, 108.91, N'2016-08-17 19:12:31.000', 'B' ), ( '1ABC', 211, 'Inc.', 9, 977.22, 108.58, N'2016-08-17 15:44:46.190', 'B' ), ( '1ABC', 211, 'Inc.', 30, 3256.8, 108.56, N'2016-08-17 15:37:12.430', 'S'), ( '1ABC', 211, 'Inc.', 23, 2499.18, 108.66, N'2016-08-17 15:03:52.000', 'B' ), ( '1ABC', 211, 'Inc.', 9, 977.58, 108.62, N'2016-08-17 14:57:52.000', 'B' ), ( '1ABC', 211, 'Inc.', 120, 13035.588, 108.6299, N'2016-08-17 14:25:45.770', 'B' ), ( '1ABC', 211, 'Inc.', 25, 2713, 108.52, N'2016-08-17 14:09:23.000', 'B' ), ( '1ABC', 211, 'Inc.', 100, 10851.99, 108.5199, N'2016-08-17 14:09:10.310', 'B' ), ( '1ABC', 211, 'Inc.', 70, 7625.8, 108.94, N'2016-08-17 13:36:02.000', 'B' ), ( '1ABC', 211, 'Inc.', 85, 9284.55, 109.23, N'2016-08-17 13:30:16.087', 'B' )--DROP TABLE #temptableSELECT *FROM #temptable;Resultset :Key KeyID KeyDescription KeyAmount KeyVolume SellPrice ExecutionTime BuySell DerivedColumn 1ABC 211 Inc. -15 -1639.2 109.28 19-08-2016 14:39:34 S 108.581ABC 211 Inc. -10 -1087.4 108.74 20-08-2016 14:39:34 S 108.581ABC 211 Inc. 1 109.24 109.24 21-08-2016 14:39:34 B NA1ABC 211 Inc. 9 984.87 109.43 22-08-2016 14:39:34 B NA1ABC 211 Inc. 100 10948.99 109.4899 23-08-2016 14:39:34 B NA1ABC 211 Inc. 20 2186.8 109.34 24-08-2016 14:39:34 B NA1ABC 211 Inc. 40 4373.2 109.33 25-08-2016 14:39:34 B NA1ABC 211 Inc. 25 2722.75 108.91 26-08-2016 14:39:34 B NA1ABC 211 Inc. 9 977.22 108.58 27-08-2016 14:39:34 B NA1ABC 211 Inc. 30 3256.8 108.56 28-08-2016 14:39:34 S 109.231ABC 211 Inc. 23 2499.18 108.66 29-08-2016 14:39:34 B NA1ABC 211 Inc. 9 977.58 108.62 30-08-2016 14:39:34 B NA1ABC 211 Inc. 120 13035.59 108.6299 31-08-2016 14:39:34 B NA1ABC 211 Inc. 25 2713 108.52 01-09-2016 14:39:34 B NA1ABC 211 Inc. 100 10851.99 108.5199 02-09-2016 14:39:34 B NA1ABC 211 Inc. 70 7625.8 108.94 03-09-2016 14:39:34 B NA1ABC 211 Inc. 85 9284.55 109.23 04-09-2016 14:39:34 B NAIn result set we have to populate the value of last bu for each sell.as we have value of buy (108.58 27-08-2016 14:39:34 B) of 27th aug as last buy for first 2 sell. ans for sell of 28th Aug we have to populate price of last buy i'e od 4th sept.Thanks

Viewing all articles
Browse latest Browse all 3145

Latest Images

Trending Articles



Latest Images