sql,将没有聚合和多个表的查询分组

时间:2014-07-16 15:44:41

标签: sql-server-2008-r2 group-by inner-join

我需要通过dda.LA对下面的查询进行分组,并且需要显示select中列出的所有列,但几乎没有列聚合。我不知道解决这个问题的语法是什么,我找不到显示这种语法的帖子(大多数例子只有一个表,两个在顶部)。

    Select  dda.a,
    dda.b,
    dda.c,
    dda.d,
    dda.e,
    dda.f,
    dda.g,
    dda.h,
    dda.i,
    dda.j,
    dda.k,
    dda.l,
    dda.m,
    dda.n,
    dda.o,
    dda.p,
    dda.r,
    dda.u,
    dda.LA,
    dd.aa,
    coalesce(apn.apn,Pt.z) as abc,
    coalesce(apn.v,Pt.y) as def,
    'RFN'                       RowFocusIndicator   ,
    'SRI'                       SelectRowIndicator  ,
    'Y'                         Expanded        ,
    Convert(Int, Null)              SortColumn

    From    dda (NoLock)
    Inner Join dd (NoLock) On dda.d = dd.q and dda.e = dd.e
    Left Outer Join apn (nolock) on dda.r = apn.r 
    Left Outer Join Pt (nolock) on dda.s = Pt.t

    Where   1 = 1
        And dda.u =     (Select Min(c.w)
                        From    c (NoLock)
                        Where   c.x = dda.s)

谢谢!

1 个答案:

答案 0 :(得分:0)

只需将其添加到您要通过以下方式汇总的任何列:

AggregatedColumnName = Aggregation(fieldToAggregate) Over (Partition By dda.LA)

离。

aCount = Count(dda.a) Over (Partition By dda.LA)