Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

previous figure Expand / Collapse
Author
Message
Posted Wednesday, September 26, 2012 1:23 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 5:14 AM
Points: 105, Visits: 463
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);

Post #1364908
Posted Wednesday, September 26, 2012 1:56 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:44 AM
Points: 13,093, Visits: 12,574
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 Moden's 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)
Post #1364915
Posted Wednesday, September 26, 2012 2:19 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 12:29 PM
Points: 3,813, Visits: 8,581
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1364927
Posted Wednesday, September 26, 2012 2:37 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 5:14 AM
Points: 105, Visits: 463
I want to make a view from the query . will this CTE works for view.?
Post #1364932
Posted Wednesday, September 26, 2012 2:43 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:44 AM
Points: 13,093, Visits: 12,574
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 Moden's 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)
Post #1364935
Posted Wednesday, September 26, 2012 10:44 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 5:14 AM
Points: 105, Visits: 463
Thanks Mays, SS

Cheers :)
Post #1365031
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse