scottichrosaviakosmos
Yak Posting Veteran
66 Posts |
Posted - 2012-09-26 : 15:24:11
|
i have a table with 3 columns. i want to get the salesamount for previous calcid for a perticular productid.below is script for table and data for it and also have query which is not giving desired result. this query is not giving desired ouput as the previous salesamount is repeated for an id in different calcid. below query is used against id 58. Query: select calcid,salesamount,productid,(select max(t2.calcid) from abc t2 where t2.productid=t1.productid and t2.calcid<t1.calcid )prevcalcid, (select max(salesamount) from abc t2 where t2.productid=t1.productid and t2.calcid<t1.calcid ) prevSalesamount from abc t1 where productid=58 order by calcid--Create table and its columnsCREATE TABLE [dbo].[abc] ( [calcid] [int] NULL, [productid] [int] NULL, [salesamount] [varchar](50) NULL);GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 58, N'-9207187')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 62, N'-9207187')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 65, N'-1148599')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 66, N'-1127198')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 67, N'-392911')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 68, N'-6290578')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 69, N'-72526')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 70, N'-4902075')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 71, N'-701707')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 72, N'-1446659')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 73, N'-1883630')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 74, N'-3401360')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 75, N'-2407507')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 79, N'-809867')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 81, N'-757')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 80, N'-809330')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 83, N'-518030')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 84, N'-28371')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 85, N'-33291')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 86, N'-526470')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 87, N'-80237')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 88, N'-80237')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 63, N'-8440')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (1, 64, N'-8440')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 58, N'-7538177')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 62, N'-7538177')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 65, N'-1137709')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 66, N'-1102358')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 67, N'-369878')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 68, N'-6355274')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 69, N'-70425')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 70, N'-4914166')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 71, N'-661270')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 72, N'-1436315')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 73, N'-135760')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 74, N'-3925984')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 75, N'-4552012')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 79, N'-772962')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 81, N'-35')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 80, N'-772954')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 83, N'-563600')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 84, N'-90062')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 85, N'-12628')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 86, N'-505996')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 87, N'-79895')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 88, N'-79895')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 63, N'-8856')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (2, 64, N'-8856')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 58, N'-6340329')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 62, N'-6340329')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 65, N'-1094336')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 66, N'-1071349')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 67, N'-288315')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 68, N'-5846006')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 69, N'-62770')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 70, N'-4859025')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 71, N'-596240')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 72, N'-1117019')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 73, N'-127491')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 74, N'-3707276')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 75, N'-4469250')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 79, N'-742116')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 81, N'-2123')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 80, N'-741933')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 83, N'-405355')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 84, N'-367485')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 85, N'-42498')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 86, N'-395009')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 87, N'-78250')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 88, N'-78250')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 63, N'-61379')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (5, 64, N'-61379')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 58, N'-6933361')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 62, N'-6933361')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 65, N'-969248')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 66, N'-962703')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 67, N'-231015')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 68, N'-5870152')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 69, N'-64377')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 70, N'-4946998')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 71, N'-627723')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 72, N'-1072956')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 73, N'-97881')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 74, N'-3762813')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 75, N'-4363952')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 79, N'-764101')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 81, N'-138')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 80, N'-764196')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 83, N'-590916')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 84, N'-310860')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 85, N'-52108')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 86, N'-381477')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 87, N'-150094')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 88, N'-150094')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 63, N'-59596')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (7, 64, N'-59596')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 58, N'-7517594')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 62, N'-7517594')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 65, N'-940830')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 66, N'-946147')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 67, N'-185058')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 68, N'-6630619')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 69, N'-66291')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 70, N'-5717000')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 71, N'-644450')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 72, N'-1094581')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 73, N'-97113')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 74, N'-3797032')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 75, N'-4380745')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 79, N'-796907')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 80, N'-796907')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 83, N'-379521')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 84, N'-122031')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 85, N'-64705')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 86, N'-327515')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 87, N'-73268')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 88, N'-73268')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 63, N'-58389')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (8, 64, N'-58389')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 58, N'-7065395')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 62, N'-7065395')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 65, N'-1062060')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 66, N'-1000453')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 67, N'-346499')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 68, N'-5790818')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 69, N'-69021')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 70, N'-4993425')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 71, N'-621522')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 72, N'-1085681')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 73, N'-106259')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 74, N'-3852474')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 75, N'-4450259')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 79, N'-848539')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 81, N'-257')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 80, N'-848780')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 83, N'-964698')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 84, N'-420407')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 85, N'-59808')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 86, N'-710498')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 87, N'-72271')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 88, N'-72271')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 63, N'-58162')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (9, 64, N'-58162')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 58, N'-6211485')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 62, N'-6211485')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 65, N'-987297')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 66, N'-925428')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 67, N'-307806')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 68, N'-5206992')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 69, N'-67108')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 70, N'-4457389')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 71, N'-609933')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 72, N'-989455')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 73, N'-97815')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 74, N'-3670286')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 75, N'-4329744')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 79, N'-846304')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 81, N'-211')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 80, N'-846268')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 83, N'-670479')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 84, N'-118663')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 85, N'-47346')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 86, N'-591875')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 87, N'-73700')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 88, N'-73700')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 63, N'-61008')GOINSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount]) VALUES (10, 64, N'-61008')scoo |
|