日期:2014-05-18  浏览次数:20551 次

四表关联按分类汇总的SQL语句,急,解决结帖
Select  
Sum(designtaskvalue)   as   t1,
Sum(designbuildvalue)   as   t2,
Sum(OtherAndNewTaskValue)   as   t3,
Sum(OneYearTaskValue)   as   t4,
Sum(TwoYearTaskValue)   as   t5,
Sum(ThreeYearTaskValue)   as   t6,
Sum(FinishedTaskValue)   as   t7,
Sum(FinishedBuildValue)   as   t8    
From  
RollPlan   A   Left   join   DictSegment   B   on   A.SegmentId=B.SegmentId   Left   join   DictProject   C   on   B.ProjectId=C.ProjectId   Left   join   DictComp   D   on   D.CompId=C.CompId   Left   Join   DictNodeData   E   On   C.ProjectTypeTID=E.NodeCode  
Where  
Cast(A.PeriodValue   as   int)=2007
Group     By
C.ProjectTypeTID
现在这样子已经可以汇总了,但是不能输出类名
类名字段是在临时表E里(DictNodeData)的NodeName
急啊

------解决方案--------------------
这样呢?

Select
Sum(designtaskvalue) as t1,
Sum(designbuildvalue) as t2,
Sum(OtherAndNewTaskValue) as t3,
Sum(OneYearTaskValue) as t4,
Sum(TwoYearTaskValue) as t5,
Sum(ThreeYearTaskValue) as t6,
Sum(FinishedTaskValue) as t7,
Sum(FinishedBuildValue) as t8,
max(E.NodeName) as NodeName
From
RollPlan A
Left join DictSegment B on A.SegmentId=B.SegmentId
Left join DictProject C on B.ProjectId=C.ProjectId
Left join DictComp D on D.CompId=C.CompId
Left Join DictNodeData E On C.ProjectTypeTID=E.NodeCode
Where
Cast(A.PeriodValue as int)=2007
Group By
C.ProjectTypeTID
------解决方案--------------------
Select
E.NodeName,
Sum(designtaskvalue) as t1,
Sum(designbuildvalue) as t2,
Sum(OtherAndNewTaskValue) as t3,
Sum(OneYearTaskValue) as t4,
Sum(TwoYearTaskValue) as t5,
Sum(ThreeYearTaskValue) as t6,
Sum(FinishedTaskValue) as t7,
Sum(FinishedBuildValue) as t8
From
RollPlan A Left join
DictSegment B on A.SegmentId=B.SegmentId Left join
DictProject C on B.ProjectId=C.ProjectId Left join
DictComp D on D.CompId=C.CompId Left Join
DictNodeData E On C.ProjectTypeTID=E.NodeCode
Where
Cast(A.PeriodValue as int)=2007
Group By
C.ProjectTypeTID,E.NodeName