September 26, 2012 at 1:23 pm
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 ABC
(product_id INTEGER NOT NULL,
sales_seq INTEGER NOT NULL,
PRIMARY KEY (product_id, sales_seq),
sales_amt DECIMAL (12,2) NOT NULL);
GO
INSERT INTO ABC
VALUES (1, 58, -9207187),
(1, 62, -9207187), (1, 65, -1148599), (1, 66, -1127198),
(1, 67, -392911), (1, 68, -6290578), (1, 69, -72526),
..
(10, 88, -73700), (10, 63, -61008), (10, 64, -61008);
September 26, 2012 at 1:56 pm
Nice job posting ddl and sample data!!!!
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.
I am a little confused about what you want here. What do you mean by previous calcid? Is the logic there to order by calcid for the product? I am not sure what you want for output but based on the query you posted I am not sure you want a running total or if you want the difference from row to row. It will make a big difference in the query depending on how you want the totals.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
September 26, 2012 at 2:19 pm
Could you use something like this?
If it works, do you understand it?
;WITH CTE AS(
selectcalcid,
salesamount,
productid,
ROW_NUMBER() OVER( PARTITION BY productid ORDER BY calcid) rn
from #abc t1
--where productid IN (58, 62, 65)
)
SELECTcte1.productid,
cte1.calcid,
cte1.salesamount,
cte2.calcid AS prevcalcid,
cte2.salesamount AS prevsalesamount
FROM CTE cte1
LEFT
JOIN CTE cte2 ON cte1.productid = cte2.productid
AND cte1.rn = cte2.rn + 1
September 26, 2012 at 2:37 pm
I want to make a view from the query . will this CTE works for view.?
September 26, 2012 at 2:43 pm
scottichrosaviakosmos (9/26/2012)
I want to make a view from the query . will this CTE works for view.?
create view DidYouTryIt as
WITH CTE AS(
selectcalcid,
salesamount,
productid,
ROW_NUMBER() OVER( PARTITION BY productid ORDER BY calcid) rn
from abc t1
--where productid IN (58, 62, 65)
)
SELECTcte1.productid,
cte1.calcid,
cte1.salesamount,
cte2.calcid AS prevcalcid,
cte2.salesamount AS prevsalesamount
FROM CTE cte1
LEFT
JOIN CTE cte2 ON cte1.productid = cte2.productid
AND cte1.rn = cte2.rn + 1
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
September 26, 2012 at 10:44 pm
Thanks Mays, SS
Cheers 🙂
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply