Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the below query which works fine in 2012 as it is using the 2012 features. Can anyone make the exact same query work in 2008 with same out put?

SQL
declare @Buckets table (ID char(2), FullCapacity int)
declare @Filler table (ID char(2), Filler int)

insert into @Buckets values
('B1', 100),
('B2', 50),
('B3', 70)

insert into @Filler values
('F1', 90),
('F2', 70),
('F3', 40),
('F4', 20)

;with fillerCte as
(
    select      
        ID,
        Filler,
        sum(Filler) over (order by ID) as TotalFill
    from @Filler
), 
BucketCte as
(
    select 
        ID,
        FullCapacity,
        sum(FullCapacity) over (order by ID) - FullCapacity as RunningTotalCapacity
    from @Buckets
)
select 
    b.ID, 
    b.FullCapacity,
    case 
        when f.TotalFill < b.RunningTotalCapacity then 0
        when f.TotalFill > b.RunningTotalCapacity + b.FullCapacity then b.FullCapacity
        else f.TotalFill - b.RunningTotalCapacity
    end as CurrentAmount
from fillerCte as f
cross join BucketCte as b
order by f.ID, b.ID


What I have tried:

SQL
declare @Buckets table (ID char(2), FullCapacity int)
declare @Filler table (ID char(2), Filler int)

insert into @Buckets values
('B1', 100),
('B2', 50),
('B3', 70)

insert into @Filler values
('F1', 90),
('F2', 70),
('F3', 40),
('F4', 20)

;with fillerCte as
(
    select      
        ID,
        Filler,
        sum(Filler) over (order by ID) as TotalFill
    from @Filler
), 
BucketCte as
(
    select 
        ID,
        FullCapacity,
        sum(FullCapacity) over (order by ID) - FullCapacity as RunningTotalCapacity
    from @Buckets
)
select 
    b.ID, 
    b.FullCapacity,
    case 
        when f.TotalFill < b.RunningTotalCapacity then 0
        when f.TotalFill > b.RunningTotalCapacity + b.FullCapacity then b.FullCapacity
        else f.TotalFill - b.RunningTotalCapacity
    end as CurrentAmount
from fillerCte as f
cross join BucketCte as b
order by f.ID, b.ID
Posted
Updated 8-Sep-16 2:58am
v2
Comments
Herman<T>.Instance 8-Sep-16 5:42am    
What is the exact problem?
ashishkumarrai 8-Sep-16 5:47am    
I get error when executing it in SQL 2008.The Parallel Data Warehouse (PDW) features are not enabled
ashishkumarrai 8-Sep-16 5:47am    
output I am after.
ID | FullCapacity | CurrentAmount
---+--------------+--------------
B1 | 100 | 90
B2 | 50 | 0
B3 | 70 | 0
---+--------------+--------------
B1 | 100 | 100
B2 | 50 | 50
B3 | 70 | 10
---+--------------+--------------
B1 | 100 | 100
B2 | 50 | 50
B3 | 70 | 50
---+--------------+--------------
B1 | 100 | 100
B2 | 50 | 50
B3 | 70 | 70

1 solution

I found this :
Quote:
This is a parser bug that exists only in SQL Server 2008. Non-PDW versions of SQL Server before 2012 do not support the ORDER BY clause with aggregate functions like MIN


more details

You don't have a SQL 2008 here, but considering the above mentioned, i think you can try this:


SQL
declare @Buckets table (ID char(2), FullCapacity int)
declare @Filler table (ID char(2), Filler int)
 
insert into @Buckets values
('B1', 100),
('B2', 50),
('B3', 70)
 
insert into @Filler values
('F1', 90),
('F2', 70),
('F3', 40),
('F4', 20)
 
;with fillerCte as
(
    select      
        ID,
        Filler,
        sum(Filler) over (order by ID) as TotalFill
    from @Filler
), 
BucketCte as
(
    select 
        ID,
        FullCapacity,
        sum(FullCapacity) over (order by ID) - FullCapacity as RunningTotalCapacity
    from @Buckets
)
select 
    b.ID as b_ID,
    f.ID as f_ID, 
    b.FullCapacity,
    case 
        when f.TotalFill < b.RunningTotalCapacity then 0
        when f.TotalFill > b.RunningTotalCapacity + b.FullCapacity then b.FullCapacity
        else f.TotalFill - b.RunningTotalCapacity
    end as CurrentAmount
    into #tmp
from fillerCte as f
cross join BucketCte as b

SELECT f_ID as ID, FullCapacity,CurrentAmount
FROM 
 #tmp
ORDER BY f_ID, b_ID
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900