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

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 DB2 Query conversion to SQL Server

Author  Topic 

logeshwar
Starting Member

6 Posts

Posted - 2009-09-03 : 07:44:06
I have a DB2 query which needs to be converted to be compliant with SQL Server. The challenge is to work around the Aggregate functioning on subqueried which is not allowed in Sql Server.

i run this query and i get the error message as
cannot perform aggregate functions on subqueries..

Can u help me in the workaround

The DB2 Query is

select a.div,
a.subreg,
a.dis,
a.ter,

sum(coalesce(
(
select sum(b.finalcount)
from HomeBaseLink x
join pMW b
on b.level = 2
and b.weekno = a.weekno
and b.repcontractid = x.contractid
where x.homebasename = a.ter
and x.startweekno <= a.weekno
and (x.endweekno > a.weekno
or x.endweekno is null )

)
,0))
as finalcount,


sum(coalesce(
(
select count(distinct z.contractid)
from HBL z
where z.homebasename = a.ter
and z.startweekno <= a.weekno
and (z.endweekno >= a.weekno
or z.endweekno is null )
)
,0))
as accumulatedcount,
sum(coalesce(
(
select sum(coalesce((case when rp.contractenddate is null then 0 when rp.contractenddate >= w.startdate and rp.contractenddate < w.enddate then 1 else 0 end),0))
from HBL y
join rpt_weeknotodate w
on w.yearweekno = a.weekno
join rpContract rp
on y.contractid = rp.contractid
where y.homebasename = a.territory
and y.startweekno <= a.weekno
and (y.endweekno >= a.weekno
or y.endweekno is null )
)
,0)) as terminations


from maSales a

where a.weekno between 200901 and 200926


group by a.div,
a.subreg,
a.dis,
a.ter

order by 4
;

Ifor
Aged Yak Warrior

700 Posts

Posted - 2009-09-03 : 08:53:21
Try something along the following lines:


SELECT a.div
,a.subreg
,a.dis
,a.ter
,SUM(b.finalcount) AS finalcount
,COUNT(DISTINCT z.contractid) AS accumulatedcount
,COUNT(y.contractid) AS terminations
FROM maSales a
LEFT JOIN
(
HomeBaseLink x
JOIN pMW b
ON x.contractid = b.repcontractid
AND b.[level] = 2
AND x.startweekno <= b.weekno
-- should this be x.endweekno >= b.weekno ????
AND (x.endweekno > b.weekno OR x.endweekno IS NULL)
)
ON a.ter = x.homebasename
AND a.weekno = b.weekno
LEFT JOIN HBL z
ON a.ter = z.homebasename
AND z.startweekno <= a.weekno
AND (z.endweekno >= a.weekno OR z.endweekno IS NULL)
LEFT JOIN
(
HBL y
JOIN rpContract rp
ON y.contractid = rp.contractid
JOIN rpt_weeknotodate w
ON rp.contractenddate >= w.startdate
AND rp.contractenddate < w.enddate
)
-- is a.territory different from a.ter???
ON a.territory = y.homebasename
AND y.startweekno <= a.weekno
AND (y.endweekno >= a.weekno OR y.endweekno IS NULL)
AND a.weekno = w.yearweekno
WHERE a.weekno BETWEEN 200901 AND 200926
GROUP BY a.div, a.subreg, a.dis, a.ter
Go to Top of Page

logeshwar
Starting Member

6 Posts

Posted - 2009-09-03 : 11:15:05
Hi, for the last two columns apart from doing the count function we also have to perform sum function on the result.which would cause the aggregate function on subquery error
Go to Top of Page
   

- Advertisement -