按SQL 2005中的相关子查询的结果进行分组

时间:2010-11-17 16:42:10

标签: sql-server-2005 group-by correlated-subquery

这是我第一次发布Stack Overflow,但已经发现它是一个非常宝贵的资源。

我的问题与SQL语句中的相关子查询有关,我理解这是不可能的。我希望通过解释查询的意图,我可以找到一些帮助。在最简单的形式中,我试图通过主键和组通过相关子查询的结果进行聚合。用于关联的字段对于每个主键记录是唯一的。如果我按相关字段分组,则不会提供预期的结果。我需要能够按相关子查询的结果进行分组,但我不确定如何重构查询。下面提供了一个示例查询。

由于

-John

SELECT DISTINCT 
    (SELECT substring(CommaDelimitedTranslatedList, 0, len(CommaDelimitedTranslatedList)) 
    FROM 
        ( 
            SELECT 
                cd.choiceValue + ',' 
            from SelectedChoiceTable sct 
                join ChoiceDescription cd on 
                    cd.ChoiceID = sct.ChoiceID 
            where 
                sct.ChoiceSet = TargetJoinTable.ChoiceKey  FOR XML PATH('')
        ) D ( CommaDelimitedTranslatedList ))  AS           [TargetJoinTableCommaDelimitedTranslatedList1] ,
    count(BaseTable.BaseKey)
 FROM BaseTable
    join TargetJoinTable on 
        BaseTable.BaseKey = TargetJoinTable.BaseKey
Group By 
    [TargetJoinTableCommaDelimitedTranslatedList1]
Order By 
    [TargetJoinTableCommaDelimitedTranslatedList1] ASC

1 个答案:

答案 0 :(得分:2)

乍一看,尝试将子查询更改为CROSS(或OUTER)APPLY。这会将“列”从SELECT子句移动到FROM子句,从而允许分组。

SELECT
    foo.TargetJoinTableCommaDelimitedTranslatedList1 ,
    count(BaseTable.BaseKey)
 FROM
    BaseTable
    join
    TargetJoinTable on BaseTable.BaseKey = TargetJoinTable.BaseKey
    CROSS APPLY
    (
    SELECT
        substring(CommaDelimitedTranslatedList, 0, 2000000000)
               AS TargetJoinTableCommaDelimitedTranslatedList1
    FROM 
        ( 
        SELECT 
            cd.choiceValue + ',' 
        from
            SelectedChoiceTable sct 
            join
            ChoiceDescription cd on cd.ChoiceID = sct.ChoiceID 
        where 
            sct.ChoiceSet = TargetJoinTable.ChoiceKey
        FOR XML PATH('')
        ) D ( CommaDelimitedTranslatedList )
      ) foo
Group By 
    foo.TargetJoinTableCommaDelimitedTranslatedList1
Order By 
    foo.TargetJoinTableCommaDelimitedTranslatedList1 ASC

或者将聚合移到派生表上

SELECT
    COUNT(foo.BaseKey),
    TargetJoinTableCommaDelimitedTranslatedList1
FROM
    (SELECT DISTINCT 
        (SELECT substring(CommaDelimitedTranslatedList, 0, len(CommaDelimitedTranslatedList)) 
        FROM 
            ( 
                SELECT 
                    cd.choiceValue + ',' 
                from SelectedChoiceTable sct 
                    join ChoiceDescription cd on 
                        cd.ChoiceID = sct.ChoiceID 
                where 
                    sct.ChoiceSet = TargetJoinTable.ChoiceKey  FOR XML PATH('')
            ) D ( CommaDelimitedTranslatedList ))  AS           [TargetJoinTableCommaDelimitedTranslatedList1] ,
     BaseTable.BaseKey

     FROM BaseTable
        join TargetJoinTable on 
            BaseTable.BaseKey = TargetJoinTable.BaseKey
    ) foo
Group By 
    [TargetJoinTableCommaDelimitedTranslatedList1]
Order By 
    [TargetJoinTableCommaDelimitedTranslatedList1] ASC

这样做的一个问题是CSV TargetJoinTableCommaDelimitedTranslatedList1可能会多次生成:如果我正确读取,则每个BaseTable.BaseKey一次。哪个会慢。我的感觉是,CSV生成应该是最后的:您实际上是在TargetJoinTable.ChoiceKey上进行分组,而不是它生成的CSV。