SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 previous or last value
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

scottichrosaviakosmos
Yak Posting Veteran

Russia
66 Posts

Posted - 09/26/2012 :  15:24:11  Show Profile  Reply with Quote
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 columns
CREATE TABLE [dbo].[abc] (
[calcid] [int] NULL,
[productid] [int] NULL,
[salesamount] [varchar](50) NULL);
GO

INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 58, N'-9207187')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 62, N'-9207187')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 65, N'-1148599')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 66, N'-1127198')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 67, N'-392911')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 68, N'-6290578')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 69, N'-72526')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 70, N'-4902075')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 71, N'-701707')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 72, N'-1446659')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 73, N'-1883630')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 74, N'-3401360')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 75, N'-2407507')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 79, N'-809867')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 81, N'-757')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 80, N'-809330')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 83, N'-518030')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 84, N'-28371')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 85, N'-33291')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 86, N'-526470')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 87, N'-80237')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 88, N'-80237')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 63, N'-8440')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (1, 64, N'-8440')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 58, N'-7538177')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 62, N'-7538177')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 65, N'-1137709')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 66, N'-1102358')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 67, N'-369878')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 68, N'-6355274')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 69, N'-70425')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 70, N'-4914166')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 71, N'-661270')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 72, N'-1436315')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 73, N'-135760')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 74, N'-3925984')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 75, N'-4552012')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 79, N'-772962')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 81, N'-35')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 80, N'-772954')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 83, N'-563600')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 84, N'-90062')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 85, N'-12628')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 86, N'-505996')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 87, N'-79895')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 88, N'-79895')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 63, N'-8856')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (2, 64, N'-8856')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 58, N'-6340329')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 62, N'-6340329')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 65, N'-1094336')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 66, N'-1071349')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 67, N'-288315')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 68, N'-5846006')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 69, N'-62770')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 70, N'-4859025')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 71, N'-596240')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 72, N'-1117019')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 73, N'-127491')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 74, N'-3707276')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 75, N'-4469250')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 79, N'-742116')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 81, N'-2123')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 80, N'-741933')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 83, N'-405355')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 84, N'-367485')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 85, N'-42498')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 86, N'-395009')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 87, N'-78250')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 88, N'-78250')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 63, N'-61379')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (5, 64, N'-61379')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 58, N'-6933361')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 62, N'-6933361')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 65, N'-969248')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 66, N'-962703')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 67, N'-231015')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 68, N'-5870152')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 69, N'-64377')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 70, N'-4946998')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 71, N'-627723')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 72, N'-1072956')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 73, N'-97881')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 74, N'-3762813')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 75, N'-4363952')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 79, N'-764101')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 81, N'-138')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 80, N'-764196')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 83, N'-590916')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 84, N'-310860')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 85, N'-52108')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 86, N'-381477')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 87, N'-150094')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 88, N'-150094')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 63, N'-59596')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (7, 64, N'-59596')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 58, N'-7517594')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 62, N'-7517594')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 65, N'-940830')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 66, N'-946147')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 67, N'-185058')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 68, N'-6630619')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 69, N'-66291')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 70, N'-5717000')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 71, N'-644450')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 72, N'-1094581')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 73, N'-97113')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 74, N'-3797032')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 75, N'-4380745')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 79, N'-796907')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 80, N'-796907')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 83, N'-379521')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 84, N'-122031')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 85, N'-64705')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 86, N'-327515')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 87, N'-73268')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 88, N'-73268')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 63, N'-58389')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (8, 64, N'-58389')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 58, N'-7065395')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 62, N'-7065395')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 65, N'-1062060')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 66, N'-1000453')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 67, N'-346499')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 68, N'-5790818')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 69, N'-69021')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 70, N'-4993425')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 71, N'-621522')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 72, N'-1085681')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 73, N'-106259')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 74, N'-3852474')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 75, N'-4450259')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 79, N'-848539')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 81, N'-257')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 80, N'-848780')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 83, N'-964698')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 84, N'-420407')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 85, N'-59808')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 86, N'-710498')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 87, N'-72271')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 88, N'-72271')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 63, N'-58162')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (9, 64, N'-58162')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 58, N'-6211485')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 62, N'-6211485')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 65, N'-987297')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 66, N'-925428')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 67, N'-307806')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 68, N'-5206992')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 69, N'-67108')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 70, N'-4457389')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 71, N'-609933')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 72, N'-989455')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 73, N'-97815')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 74, N'-3670286')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 75, N'-4329744')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 79, N'-846304')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 81, N'-211')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 80, N'-846268')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 83, N'-670479')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 84, N'-118663')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 85, N'-47346')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 86, N'-591875')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 87, N'-73700')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 88, N'-73700')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 63, N'-61008')

GO
INSERT INTO [dbo].[abc] ([calcid], [productid], [salesamount])
VALUES (10, 64, N'-61008')


scoo

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 09/27/2012 :  10:39:53  Show Profile  Reply with Quote
see scenario 2

http://visakhm.blogspot.com/2010/01/multipurpose-apply-operator.html



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.22 seconds. Powered By: Snitz Forums 2000