|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Friday, February 04, 2011 7:20 AM
Points: 977,
Visits: 1,499
|
|
jordonpilling (3/13/2008) a very nice article indeed, however i recently had the same issue on SQL Server 8.0, Is their a related atricle/method for SQL 8?
You should be able to do the same thing by making the PriceCompare a temporary table with an identity column instead of the ROW_NUMBER() for rownum.
Tom Garth Vertical Solutions
"There are three kinds of men. The one that learns by reading. The few who learn by observation. The rest of them have to pee on the electric fence for themselves." -- Will Rogers
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, August 30, 2012 9:04 AM
Points: 10,
Visits: 74
|
|
I've had to link on a previous row in the past. I've just used self joins, i.e. joining a table on itself. In the join clause, I just put my criteria. For example:
select t1.price as 'oldprice', t2.price as 'newprice' from table1 t1 join table1 t2 on t1.colnum +1= t2.colnum
Is there anything wrong with doing that? Is it less efficient or buggy?
Thanks,
Seth
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Friday, May 17, 2013 12:22 PM
Points: 10,571,
Visits: 11,871
|
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: 2 days ago @ 1:55 PM
Points: 15,442,
Visits: 9,571
|
|
Seth (3/13/2008) I've had to link on a previous row in the past. I've just used self joins, i.e. joining a table on itself. In the join clause, I just put my criteria. For example:
select t1.price as 'oldprice', t2.price as 'newprice' from table1 t1 join table1 t2 on t1.colnum +1= t2.colnum
Is there anything wrong with doing that? Is it less efficient or buggy?
Thanks,
Seth
That method works just fine so long as you are linking to the immediately prior ID. If, for example, you want the prior order for the same customer, your ID number is not so easily derived.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Friday, February 04, 2011 7:20 AM
Points: 977,
Visits: 1,499
|
|
CREATE TABLE #PriceCompare ( ITEMNMBR varchar(31), ITEMDESC varchar(101), DOCDATE datetime, UNITCOST numeric(19, 5), rownum int identity ) INSERT INTO #PriceCompare SELECT RTRIM(PH.ITEMNMBR) [ITEMNMBR], RTRIM(i.ITEMDESC) [ITEMDESC], ph.DOCDATE, ph.UNITCOST FROM IV00101 i INNER JOIN IV30300 ph ON i.ITEMNMBR = ph.ITEMNMBR ORDER BY i.ITEMNMBR, ph.DOCDATE
SELECT currow.ITEMDESC, ISNULL(prevrow.UNITCOST, 0.00) AS OldCost, currow.UNITCOST AS RangeCost, CONVERT(varchar(10), currow.DOCDATE, 110) AS StartDate, ISNULL(CONVERT(varchar(10), nextrow.DOCDATE, 110), 'CURRENT') AS EndDate FROM #PriceCompare currow LEFT JOIN #PriceCompare nextrow ON currow.rownum = nextrow.rownum - 1 AND currow.ITEMNMBR = nextrow.ITEMNMBR LEFT JOIN #PriceCompare prevrow ON currow.rownum = prevrow.rownum + 1 AND currow.ITEMNMBR = prevrow.ITEMNMBR
DROP TABLE #PriceCompare
Tom Garth Vertical Solutions
"There are three kinds of men. The one that learns by reading. The few who learn by observation. The rest of them have to pee on the electric fence for themselves." -- Will Rogers
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, April 24, 2013 12:11 PM
Points: 143,
Visits: 183
|
|
Great article. Thanks. -Mike
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 7:34 AM
Points: 2,379,
Visits: 708
|
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, April 18, 2013 6:47 AM
Points: 159,
Visits: 256
|
|
Is it strictly necessary to use the OVER/Partition clause in the CTE? Isn't it solely to restart the row numbering? Or what am I missing?
-- Please upgrade to .sig 2.0
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Tuesday, June 21, 2011 10:03 AM
Points: 577,
Visits: 102
|
|
Nice article...very interesting solution! I like the RowNumber with partition functionality, seems useful for a number of things.
My only complaint is that the SQL Code is very difficult to read in those little itty boxes.
Signature is NULL
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 3:00 AM
Points: 1,151,
Visits: 879
|
|
Hi David,
:P:P Thanks for a very nice article. I am still a bit new on SQL 2005 so I had a bit of difficulty to understand the code. I come from a MSSql 7 background and must say I have learned a lot from this forum. I immediately tested your code and after battling just a bit I got it right. In the past I solely used views for the purpose of quickly listing some data but this way is very nice.
Thanks a span.
Confusius said: "Ask a question and be a foll for a moment, keep quiet and remain a fool forever"
Manie Verster Developer Johannesburg South Africa
Life is about choices.... I choose to be happy today
|
|
|
|