Author |
Topic |
akpaga
Constraint Violating Yak Guru
331 Posts |
Posted - 2012-12-06 : 18:22:07
|
Hi friends I have a record in the following mannerobjectid,Price,Customer_Date,TableDateStamp,State(GetDate() when record is inserted)15,2.00,11/09/2012,11/09/2012 1:00:45,TX15,4.00,11/10/2012,11/10/2012 9:00:45,TX15,5.00,11/13/2012,11/13/2012 8:00:45,TX16, 7.00, 11/13/2012,11/13/2012 8:00:45,CAI want to write a query when the a user passes a date range say11/12/2012-11/14/2012.Then the objectid 15 has a customer date 11/13/2012hence i want to subtract that price of 5.00 from the most recent price that falls out side the date range which is 11 /10/2012 price of 4.00 . hence my result should be like state wiseTX ,CA1, 7(since CA customer has only one record)Hope i am clear. Please excuse me if i am notThank you |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-12-07 : 01:32:22
|
[code]DECLARE @tab TABLE(objectid int, Price dec(5,2),Customer_Date date, TableDateStamp datetime2, State varchar(4))--(GetDate() when record is inserted)INSERT INTO @tabSELECT 15,2.00,'11/09/2012','11/09/2012 1:00:45','TX' union all SELECT 15,4.00,'11/10/2012','11/10/2012 9:00:45','TX' union all SELECT 15,5.00,'11/13/2012','11/13/2012 8:00:45','TX' union all SELECT 16,7.00, '11/13/2012','11/13/2012 8:00:45','CA'--I want to write a query when the a user passes a date range sayDECLARE @FromDate date = '11/12/2012', @ToDate date ='11/14/2012'SELECT c.objectid, c.State, c.Customer_Date, c.Price - COALESCE(t.Price,0) PriceFROM (SELECT objectid, [state], customer_date, Price FROM @tab WHERE Customer_Date >= @FromDate AND Customer_Date <= @ToDate ) cLEFT JOIN (SELECT *, ROW_NUMBER() OVER (PARTITION BY objectid, [state] ORDER BY Customer_Date DESC) rn FROM @tab WHERE Customer_Date NOT BETWEEN @FromDate AND @ToDate ) tON t.rn = 1 AND c.objectid = t.objectid[/code]--Chandu |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-12-07 : 08:09:57
|
[code]SELECT [TX],[CA],...FROM(SELECT t1.State,t1.Price - COALESCE(t2.Price,0) AS DiffFROM table t1OUTER APPLY (SELECT TOP 1 Price FROM Table WHERE State = t1.State AND Customer_Date < t1.Customer_Date ORDER BY Customer_Date DESC )t2WHERE t1.Customer_Date > = @StartDateAND t1.Customer_Date < @EndDate+1)mPIVOT(MAX(Diff) FOR State IN ([TX],[CA],..))n[/code]for dynamic states seehttp://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2012-12-07 : 09:00:11
|
quote: Originally posted by bandi
DECLARE @tab TABLE(objectid int, Price dec(5,2),Customer_Date date, TableDateStamp datetime2, State varchar(4))--(GetDate() when record is inserted)INSERT INTO @tabSELECT 15,2.00,'11/09/2012','11/09/2012 1:00:45','TX' union all SELECT 15,4.00,'11/10/2012','11/10/2012 9:00:45','TX' union all SELECT 15,5.00,'11/13/2012','11/13/2012 8:00:45','TX' union all SELECT 16,7.00, '11/13/2012','11/13/2012 8:00:45','CA'--I want to write a query when the a user passes a date range sayDECLARE @FromDate date = '11/12/2012', @ToDate date ='11/14/2012'SELECT c.objectid, c.State, c.Customer_Date, c.Price - COALESCE(t.Price,0) PriceFROM (SELECT objectid, [state], customer_date, Price FROM @tab WHERE Customer_Date >= @FromDate AND Customer_Date <= @ToDate ) cLEFT JOIN (SELECT *, ROW_NUMBER() OVER (PARTITION BY objectid, [state] ORDER BY Customer_Date DESC) rn FROM @tab WHERE Customer_Date NOT BETWEEN @FromDate AND @ToDate ) tON t.rn = 1 AND c.objectid = t.objectid --Chandu
This will not work ifDECLARE @FromDate datetime, @ToDate datetimeSet @FromDate = '11/10/2012'Set @ToDate ='11/12/2012' |
|
|
|
|
|