Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sunriser
Starting Member

14 Posts

Posted - 12/19/2012 :  06:05:29  Show Profile  Reply with Quote
Hi All,

I have a query in oracle , we have used ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW in the query . now the problem is we cannot use the same function in sqlserver 2008 so please help us in overcoming this issue.

SELECT
cast(ROUND(READING_VALUE /
(
SELECT
MAX(READING_VALUE)
FROM
MACHINE_READING
WHERE
MACHINE=INST.MACHINE
AND LEFT(READ_DATE,4)='2012') AS "%PEAK",
cast(ROUND(SUM(COUNT(READING_VALUE)) OVER (ORDER BY READING_VALUE DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)/
(
SELECT COUNT(*) AS CNT FROM MACHINE_READING WHERE MACHINE=INST.MACHINE AND LEFT(READ_DATE,4)='2012')
,4)*100 as decimal(20,4)) AS "%DUR"
FROM
MACHINE_READING INST,
MACHINE_AREA_ NET,
DATE_DIMENSION DM
WHERE
INST.AREA_ID=NET.ID AND
INST.READ_DATE=DM.DATE_ID
AND NET.MACHINE='${MtrNumber}'
AND DM.YEAR='${ReadYear}' AND
GROUP BY
READING_VALUE ,INST.MACHINE
ORDER BY READING_VALUE

sunriser

bandi
Flowing Fount of Yak Knowledge

India
2242 Posts

Posted - 12/19/2012 :  06:28:16  Show Profile  Reply with Quote
See this once..

create table #t ( ord int primary key, total int, running_total int)

insert #t(ord,total) values (2,20)
-- notice the malicious re-ordering
insert #t(ord,total) values (1,10)
insert #t(ord,total) values (3,10)
insert #t(ord,total) values (4,1)

declare @total int
set @total = 0
update #t set running_total = @total, @total = @total + total

select * from #t
order by ord


--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 12/19/2012 :  07:51:30  Show Profile  Reply with Quote
SUM(COUNT(READING_VALUE)) OVER (ORDER BY READING_VALUE DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)

can be replaced with a subquery as

yourquery
..
CROSS APPLY (SELECT SUM(Cnt) AS Total
             FROM(SELECT COUNT(READING_VALUE) AS Cnt FROM ..... WHERE READING_VALUE <= maintable.READING_VALUE )t
            )r

then you can use Total in select



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000