SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


previous figure


previous figure

Author
Message
scottichrosaviakosmos
scottichrosaviakosmos
Old Hand
Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)

Group: General Forum Members
Points: 336 Visits: 691
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);
Sean Lange
Sean Lange
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63634 Visits: 17966
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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42538 Visits: 19839
Could you use something like this?
If it works, do you understand it?

;WITH CTE AS(
select calcid,
salesamount,
productid,
ROW_NUMBER() OVER( PARTITION BY productid ORDER BY calcid) rn
from #abc t1
--where productid IN (58, 62, 65)
)
SELECT cte1.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




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
scottichrosaviakosmos
scottichrosaviakosmos
Old Hand
Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)

Group: General Forum Members
Points: 336 Visits: 691
I want to make a view from the query . will this CTE works for view.?
Sean Lange
Sean Lange
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63634 Visits: 17966
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(
select calcid,
salesamount,
productid,
ROW_NUMBER() OVER( PARTITION BY productid ORDER BY calcid) rn
from abc t1
--where productid IN (58, 62, 65)
)
SELECT cte1.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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
scottichrosaviakosmos
scottichrosaviakosmos
Old Hand
Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)Old Hand (336 reputation)

Group: General Forum Members
Points: 336 Visits: 691
Thanks Mays, SS

Cheers Smile
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search