选择按所有字段分组的结果

时间:2012-04-20 07:33:35

标签: sql sql-server tsql select

我想要按所有字段分组的结果。我是通过使用 - WITH 关键字实现的。 还有其他办法吗?

WITH    T AS ( SELECT   a.codeid AS VendorID ,
                                    a.hname ,
                                    a.hnamee ,
                                    CASE WHEN v.AudienceID = 0 THEN 1
                                         ELSE 0
                                    END AS HasAll
                           FROM     dbo.dtAny AS a
                                    LEFT JOIN VendorAudience AS v ON a.codeid = v.VendorID
                           WHERE    a.hrclvl = @level
                                    AND a.dcode = @dcode
                                    AND a.codeid = ISNULL(@VendorID, a.codeid)
                         )
    SELECT  VendorID ,
            hname ,
            hnamee ,
            HasAll
    FROM    T
    GROUP BY vendorid ,
            hname ,
            hnamee ,
            HasAll

我想要这样的事情:

  SELECT   a.codeid AS VendorID ,
                                    a.hname ,
                                    a.hnamee ,
                                    CASE WHEN v.AudienceID = 0 THEN 1
                                         ELSE 0
                                    END AS HasAll
                           FROM     dbo.dtAny AS a
                                    LEFT JOIN VendorAudience AS v ON a.codeid = v.VendorID
                           WHERE    a.hrclvl = @level
                                    AND a.dcode = @dcode
                                    AND a.codeid = ISNULL(@VendorID, a.codeid)
                                    GROUP BY VendorID ,a.hname ,a.hnamee ,HasAll

但是尝试按HasAll分组时出错。(我不需要按v.AudienceID分组)

4 个答案:

答案 0 :(得分:1)

查询应为

SELECT   a.codeid AS VendorID ,
                                    a.hname ,
                                    a.hnamee ,
                                    CASE WHEN v.AudienceID = 0 THEN 1
                                         ELSE 0
                                    END AS HasAll
                           FROM     dbo.dtAny AS a
                                    LEFT JOIN VendorAudience AS v ON a.codeid = v.VendorID
                           WHERE    a.hrclvl = @level
                                    AND a.dcode = @dcode
                                    AND a.codeid = ISNULL(@VendorID, a.codeid)
                                    GROUP BY VendorID ,a.hname ,a.hnamee ,
                                    CASE WHEN   v.AudienceID = 0 THEN 1
                                         ELSE 0
                                    END

答案 1 :(得分:0)

您可以将其用作派生表。

SELECT VendorID ,a.hname ,a.hnamee ,HasAll
FROM (SELECT a.codeid AS VendorID ,
        a.hname ,
        a.hnamee ,
        CASE WHEN v.AudienceID = 0 THEN 1
            ELSE 0
        END AS HasAll
    FROM     dbo.dtAny AS a
        LEFT JOIN VendorAudience AS v ON a.codeid = v.VendorID
    WHERE    a.hrclvl = @level
        AND a.dcode = @dcode
        AND a.codeid = ISNULL(@VendorID, a.codeid)) [Table]
GROUP BY VendorID ,a.hname ,a.hnamee ,HasAll

答案 2 :(得分:0)

这就是DISTINCT用于:

WITH    T AS ( SELECT   a.codeid AS VendorID ,
                        a.hname ,
                        a.hnamee ,
                        CASE WHEN v.AudienceID = 0 THEN 1
                             ELSE 0
                        END AS HasAll
               FROM     dbo.dtAny AS a
                        LEFT JOIN VendorAudience AS v ON a.codeid = v.VendorID
               WHERE    a.hrclvl = @level
                        AND a.dcode = @dcode
                        AND a.codeid = ISNULL(@VendorID, a.codeid)
             )
SELECT DISTINCT *
FROM T

答案 3 :(得分:0)

如果要按所有列进行分组,则不需要使用DISTINCT,因为无论如何输出都是唯一的。我倾向于同意上面的Miika,但没有理由你的原始方法不起作用。

的Mac