SQL Server Forums
Profile | Register | 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
 New Topic  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
2218 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
52317 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  
 New 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.05 seconds. Powered By: Snitz Forums 2000