Alejandro Mesa
3/31/2007 5:40:00 PM
steventhrasher42@hotmail.com,
You are right.
;with cte_1
(
select
D.DepartmentID, sum(B.Amount) as B_Amount
from
dbo.tblDepartment as D
left join
dbo.tblBudget as B
on B.DepartmentID = D.DepartmentID
where
...some criteria based upon D and B...
group by
D.DepartmentID
),
cte_2
(
select
D.DepartmentID, sum(S.Amount) as S_Amount
from
dbo.tblDepartment as D
left join
dbo.tblSpending as S
on S.DepartmentID = D.DepartmentID
where
...some criteria based upon D and S...
group by
D.DepartmentID
)
select
D.DepartmentID,
D.Name,
b.B_Amount,
s.S_Amount
from
dbo.tblDepartment as D
left join
cte_1 as b
on D.DepartmentID = b.DepartmentID
left join
cte_2 as s
on D.DepartmentID = s.DepartmentID
where
b.B_Amount > 1000000 or s.S_Amount > 1000000;
go
AMB
"steventhrasher42@hotmail.com" wrote:
> That'll result in a cross join between Spending and Budget amounts
> causing them to be counted more than once, or in my example they come
> out to 9,000,000 instead of 3,000,000.
>
>
> On Mar 31, 11:29 am, "Alejandro Mesa" <mesaalejan...@msn.com> wrote:
> > steventhrashe...@hotmail.com,
> >
> > Can you try:
> >
> > select
> > D.DepartmentID, D.Name, sum(B.Amount) as B_Amount, sum(S.Amount) as
> > S_Amount
> > from
> > dbo.tblDepartment as D
> > left join
> > dbo.tblBudget as B
> > on B.DepartmentID = D.DepartmentID
> > and ...some criteria based upon D and B...
> > left join
> > dbo.tblSpending as S
> > on S.DepartmentID = D.DepartmentID
> > and ...some criteria based upon D and S...
> > group by
> > D.DepartmentID, D.Name
> > having
> > sum(B.Amount) > 1000000 or sum(S.Amount) > 1000000
> > go
> >
> > Compare execution plans.
> >
> > AMB
> >
> > <steventhrashe...@hotmail.com> wrote in message
> >
> > news:1175315056.052104.315000@p15g2000hsd.googlegroups.com...
> >
> >
> >
> > >I have a query that references a costly view as well as two derived
> > > tables that also need to reference the same costly view. The question
> > > is - is there a way to get this information back in one query while
> > > avoiding having to use the view multiple times? Perhaps SQL does not
> > > support this, and I just need to resort to denormalizing some data or
> > > such.
> >
> > > Sample of problem SQL below, plus schema and data. Any ideas
> > > appreciated. Thanks.
> >
> > > /* THE PROBLEM - Complex/costly view vwDepartment must be repeated
> > > multiple times. */
> > > select D.Name, B.Amount, S.Amount
> > > from vwDepartment D
> > > left join (
> > > select D.DepartmentID, sum(B.Amount) as Amount
> > > from vwDepartment D
> > > join tblBudget B on B.DepartmentID = D.DepartmentID
> > > --where ...some criteria based upon D and B...
> > > group by D.DepartmentID
> > > ) B on B.DepartmentID = D.DepartmentID
> > > left join (
> > > select D.DepartmentID, sum(S.Amount) as Amount
> > > from vwDepartment D
> > > join tblSpending S on S.DepartmentID = D.DepartmentID
> > > --where ...some criteria based upon D and S...
> > > group by D.DepartmentID
> > > ) S on S.DepartmentID = D.DepartmentID
> > > where (B.Amount > 1000000 or S.Amount > 1000000)
> > > --and ...some criteria based upon D...
> >
> > > /* Wish this would work - ability to reference outer columns in
> > > derived table. */
> > > select D.Name, B.Amount, S.Amount
> > > from vwDepartment D
> > > left join (
> > > select sum(B.Amount) as Amount
> > > from tblBudget B
> > > where B.DepartmentID = D.DepartmentID
> > > --and ...some criteria based upon D and B...
> > > ) B on 1 = 1
> > > left join (
> > > select sum(S.Amount) as Amount
> > > from tblSpending S
> > > where S.DepartmentID = D.DepartmentID
> > > --and ...some criteria based upon D and S...
> > > ) S on 1 = 1
> > > where B.Amount > 1000000 or S.Amount > 1000000
> > > --and ...some criteria based upon D...
> >
> > > /* Create schema */
> > > create table tblDepartment (
> > > DepartmentID int not null, Name varchar(100) not null,
> > > constraint pkDepartment primary key (DepartmentID))
> > > create table tblBudget (
> > > DepartmentID int not null, Year int not null, Amount int not
> > > null,
> > > constraint pkBudget primary key (DepartmentID, Year))
> > > create table tblSpending (
> > > DepartmentID int not null, Year int not null, Amount int not
> > > null,
> > > constraint pkSpending primary key (DepartmentID, Year))
> > > go
> >
> > > /* Pretend this view is very complex/costly */
> > > create view vwDepartment as select * from tblDepartment
> > > go
> >
> > > /* Create data */
> > > insert into tblDepartment values (1, 'A')
> > > insert into tblDepartment values (2, 'B')
> > > insert into tblBudget values (1, 2000, 1000000)
> > > insert into tblBudget values (1, 2001, 1000000)
> > > insert into tblBudget values (1, 2002, 1000000)
> > > insert into tblBudget values (2, 2000, 1000000)
> > > insert into tblBudget values (2, 2001, 1000000)
> > > insert into tblBudget values (2, 2002, 1000000)
> > > insert into tblSpending values (1, 2000, 1000000)
> > > insert into tblSpending values (1, 2001, 1000000)
> > > insert into tblSpending values (1, 2002, 1000000)
> > > insert into tblSpending values (2, 2000, 1000000)
> > > insert into tblSpending values (2, 2001, 1000000)
> > > insert into tblSpending values (2, 2002, 1000000)
> >
> > > /* Clean up */
> > > drop table tblDepartment
> > > drop table tblBudget
> > > drop table tblSpending
> > > drop view vwDepartment- Hide quoted text -
> >
> > - Show quoted text -
>
>
>