如何在sql中执行分组

时间:2017-04-27 01:43:04

标签: sql function group-by grouping sql-server-2016

Displayed record in my crystal Report

   SELECT [BarDetailId]
    ,[BarId]
    ,[Member]
    ,[MemberNo]
    ,[BarMark]
    ,[TypeSize]
    ,[BarLength]
    ,[BarNo]
    ,[BarTotal]
    ,[ShapeCode]
    ,[ShapeImage]
    ,(SELECT CASE WHEN DimA <> 0 THEN 'A=' + CONVERT(VARCHAR, CAST(DimA AS 
   Decimal(18, 0))) 
    ELSE '' END + CASE WHEN DimB <> 0 THEN 
        ', B=' + CONVERT(VARCHAR, CAST(DimB AS Decimal(18, 0)))
    ELSE '' END + CASE WHEN DimC <> 0 THEN 
        ', C=' + CONVERT(VARCHAR, CAST(DimC AS Decimal(18, 0))) 
    ELSE '' END + CASE WHEN DimD <> 0 THEN 
        ', D=' + CONVERT(VARCHAR, CAST(DimD AS Decimal(18, 0))) 
    ELSE '' END + CASE WHEN DimE <> 0 THEN 
        ', E=' + CONVERT(VARCHAR, CAST(DimE AS Decimal(18, 0))) 
    ELSE '' END + CASE WHEN DimF <> 0 THEN 
        ', F=' + CONVERT(VARCHAR, CAST(DimF AS Decimal(18, 0))) 
    ELSE '' END + CASE WHEN DimG <> 0 THEN 
        ', G=' + CONVERT(VARCHAR, CAST(DimG AS Decimal(18, 0))) 
    ELSE '' END + CASE WHEN DimN <> 0 THEN 
        ', N=' + CONVERT(VARCHAR, CAST(DimN AS Decimal(18, 0))) 
    ELSE '' END) AS Dimension
    ,[DimA]
    ,[DimB]
    ,[DimC]
    ,[DimD]
    ,[DimE]
    ,[DimF]
    ,[DimG]
    ,[DimN]
    ,[Remark]
    ,[Active]
    ,[CreatedDtTm]
    ,[CreatedBy]
    ,[CreatedIp]
    ,[UpdatedDtTm]
    ,[UpdatedBy]
    ,[UpdatedIp]
   FROM BAR_SCHEDULE_DETAIL WHERE Active = 1 AND BarId = @BarId 

如何根据TypeSize,Shape Code对它们进行分组。我想包括group by语句但收到此错误消息。 专栏&#39; BAR_SCHEDULE_DETAIL.BarDetailId&#39;在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句

1 个答案:

答案 0 :(得分:1)

GROUP BY

在聚合函数中使用,如

sum(), count(), min(), max(), avg()

如果要将记录组合​​在一起,则每列都必须是

的一部分

GROUP BY

子句,或使用上述函数之一以某种方式聚合。更多信息可以在这里找到:

http://www.sqlcourse2.com/agg_functions.html

我怀疑你可能意味着你想要订购的结果,以便记录组在一起,你可能正在寻找

ORDER BY

子句。

如果您只想要之前评论的所有列的组合,则查询将如下所示:

SELECT DISTINCT , 
    [TypeSize] , 
    [ShapeCode] , 
    CASE 
        WHEN dima <> 0 THEN 
             'A=' + CONVERT(varchar, cast(dima AS decimal(18, 0)))
        ELSE '' 
      END + 
        CASE 
            WHEN dimb <> 0 THEN 
               ', B=' + CONVERT(varchar, cast(dimb AS decimal(18, 0)))
            ELSE '' 
        END + 
        CASE 
            WHEN dimc <> 0 THEN 
               ', C=' + CONVERT(varchar, cast(dimc AS decimal(18, 0)))
            ELSE '' 
        END + 
        CASE 
            WHEN dimd <> 0 THEN 
               ', D=' + CONVERT(varchar, cast(dimd AS decimal(18, 0)))
            ELSE '' 
        END + 
        CASE 
            WHEN dime <> 0 THEN 
               ', E=' + CONVERT(varchar, cast(dime AS decimal(18, 0)))
            ELSE '' 
        END + 
        CASE 
            WHEN dimf <> 0 THEN 
               ', F=' + CONVERT(varchar, cast(dimf AS decimal(18, 0)))
            ELSE '' 
        END + 
        CASE 
            WHEN dimg <> 0 THEN 
               ', G=' + CONVERT(varchar, cast(dimg AS decimal(18, 0)))
            ELSE '' 
        END + 
        CASE 
            WHEN dimn <> 0 THEN 
               ', N=' + CONVERT(varchar, cast(dimn AS decimal(18, 0)))
            ELSE '' 
        END
      AS dimension 
FROM            bar_schedule_detail 
WHERE           active = 1 
AND             barid = @BarId    

为了包含&#34;最高评论&#34;,如果这意味着发布了第一条评论 - 那么这样的事情会这样做:

select a.*, b.remark from 
(
    SELECT
        [TypeSize] , 
        [ShapeCode] , 
        CASE 
            WHEN c.dima <> 0 THEN 
                 'A=' + CONVERT(varchar, cast(c.dima AS decimal(18, 0)))
            ELSE '' 
          END + 
            CASE 
                WHEN c.dimb <> 0 THEN 
                   ', B=' + CONVERT(varchar, cast(c.dimb AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN c.dimc <> 0 THEN 
                   ', C=' + CONVERT(varchar, cast(c.dimc AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN c.dimd <> 0 THEN 
                   ', D=' + CONVERT(varchar, cast(c.dimd AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN c.dime <> 0 THEN 
                   ', E=' + CONVERT(varchar, cast(c.dime AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN c.dimf <> 0 THEN 
                   ', F=' + CONVERT(varchar, cast(c.dimf AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN c.dimg <> 0 THEN 
                   ', G=' + CONVERT(varchar, cast(c.dimg AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN c.dimn <> 0 THEN 
                   ', N=' + CONVERT(varchar, cast(c.dimn AS decimal(18, 0)))
                ELSE '' 
            END
          AS dimension,
          min(c.CreatedDtTm) as CreatedDtTm
    FROM            bar_schedule_detail c
    WHERE           active = 1 
    AND             barid = @BarId    
    GROUP BY
        [TypeSize] , 
        [ShapeCode] , 
        CASE 
            WHEN dima <> 0 THEN 
                 'A=' + CONVERT(varchar, cast(dima AS decimal(18, 0)))
            ELSE '' 
          END + 
            CASE 
                WHEN dimb <> 0 THEN 
                   ', B=' + CONVERT(varchar, cast(dimb AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN dimc <> 0 THEN 
                   ', C=' + CONVERT(varchar, cast(dimc AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN dimd <> 0 THEN 
                   ', D=' + CONVERT(varchar, cast(dimd AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN dime <> 0 THEN 
                   ', E=' + CONVERT(varchar, cast(dime AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN dimf <> 0 THEN 
                   ', F=' + CONVERT(varchar, cast(dimf AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN dimg <> 0 THEN 
                   ', G=' + CONVERT(varchar, cast(dimg AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN dimn <> 0 THEN 
                   ', N=' + CONVERT(varchar, cast(dimn AS decimal(18, 0)))
                ELSE '' 
            END
) as a, bar_schedule_detail b
where a.typesize = b.typesize
and a.shapecode = b.shapecode
and 
(
   CASE 
     WHEN b.dima <> 0 THEN 
          'A=' + CONVERT(varchar, cast(b.dima AS decimal(18, 0)))
     ELSE '' 
   END + 
     CASE 
         WHEN b.dimb <> 0 THEN 
            ', B=' + CONVERT(varchar, cast(b.dimb AS decimal(18, 0)))
         ELSE '' 
     END + 
     CASE 
         WHEN b.dimc <> 0 THEN 
            ', C=' + CONVERT(varchar, cast(b.dimc AS decimal(18, 0)))
         ELSE '' 
     END + 
     CASE 
         WHEN b.dimd <> 0 THEN 
            ', D=' + CONVERT(varchar, cast(b.dimd AS decimal(18, 0)))
         ELSE '' 
     END + 
     CASE 
         WHEN b.dime <> 0 THEN 
            ', E=' + CONVERT(varchar, cast(b.dime AS decimal(18, 0)))
         ELSE '' 
     END + 
     CASE 
         WHEN b.dimf <> 0 THEN 
            ', F=' + CONVERT(varchar, cast(b.dimf AS decimal(18, 0)))
         ELSE '' 
     END + 
     CASE 
         WHEN b.dimg <> 0 THEN 
            ', G=' + CONVERT(varchar, cast(b.dimg AS decimal(18, 0)))
         ELSE '' 
     END + 
     CASE 
         WHEN b.dimn <> 0 THEN 
            ', N=' + CONVERT(varchar, cast(b.dimn AS decimal(18, 0)))
         ELSE '' 
     END
) = a.dimension
and a.CreatedDtTm = b.CreatedDtTm
and b.active = 1 
and b.barid = @BarId    

从这里开始,如果没有DDL的副本和一些行来测试这个答案会变得越来越困难,但是如果你想通过一些任意的属性来命令,你可以试试这个:

SELECT DISTINCT , 
    [TypeSize] , 
    [ShapeCode] , 
    CASE 
        WHEN dima <> 0 THEN 
             'A=' + CONVERT(varchar, cast(dima AS decimal(18, 0)))
        ELSE '' 
      END + 
        CASE 
            WHEN dimb <> 0 THEN 
               ', B=' + CONVERT(varchar, cast(dimb AS decimal(18, 0)))
            ELSE '' 
        END + 
        CASE 
            WHEN dimc <> 0 THEN 
               ', C=' + CONVERT(varchar, cast(dimc AS decimal(18, 0)))
            ELSE '' 
        END + 
        CASE 
            WHEN dimd <> 0 THEN 
               ', D=' + CONVERT(varchar, cast(dimd AS decimal(18, 0)))
            ELSE '' 
        END + 
        CASE 
            WHEN dime <> 0 THEN 
               ', E=' + CONVERT(varchar, cast(dime AS decimal(18, 0)))
            ELSE '' 
        END + 
        CASE 
            WHEN dimf <> 0 THEN 
               ', F=' + CONVERT(varchar, cast(dimf AS decimal(18, 0)))
            ELSE '' 
        END + 
        CASE 
            WHEN dimg <> 0 THEN 
               ', G=' + CONVERT(varchar, cast(dimg AS decimal(18, 0)))
            ELSE '' 
        END + 
        CASE 
            WHEN dimn <> 0 THEN 
               ', N=' + CONVERT(varchar, cast(dimn AS decimal(18, 0)))
            ELSE '' 
        END
      AS dimension 
FROM            bar_schedule_detail 
WHERE           active = 1 
AND             barid = @BarId    

为了包含&#34;最高评论&#34;,如果这意味着发布了第一条评论 - 那么这样的事情会这样做:

select a.*, b.remark from 
(
    SELECT
        [TypeSize] , 
        [ShapeCode] , 
        CASE 
            WHEN c.dima <> 0 THEN 
                 'A=' + CONVERT(varchar, cast(c.dima AS decimal(18, 0)))
            ELSE '' 
          END + 
            CASE 
                WHEN c.dimb <> 0 THEN 
                   ', B=' + CONVERT(varchar, cast(c.dimb AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN c.dimc <> 0 THEN 
                   ', C=' + CONVERT(varchar, cast(c.dimc AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN c.dimd <> 0 THEN 
                   ', D=' + CONVERT(varchar, cast(c.dimd AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN c.dime <> 0 THEN 
                   ', E=' + CONVERT(varchar, cast(c.dime AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN c.dimf <> 0 THEN 
                   ', F=' + CONVERT(varchar, cast(c.dimf AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN c.dimg <> 0 THEN 
                   ', G=' + CONVERT(varchar, cast(c.dimg AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN c.dimn <> 0 THEN 
                   ', N=' + CONVERT(varchar, cast(c.dimn AS decimal(18, 0)))
                ELSE '' 
            END
          AS dimension,
          min(c.CreatedDtTm) as CreatedDtTm
    FROM            bar_schedule_detail c
    WHERE           active = 1 
    AND             barid = @BarId    
    GROUP BY
        [TypeSize] , 
        [ShapeCode] , 
        CASE 
            WHEN dima <> 0 THEN 
                 'A=' + CONVERT(varchar, cast(dima AS decimal(18, 0)))
            ELSE '' 
          END + 
            CASE 
                WHEN dimb <> 0 THEN 
                   ', B=' + CONVERT(varchar, cast(dimb AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN dimc <> 0 THEN 
                   ', C=' + CONVERT(varchar, cast(dimc AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN dimd <> 0 THEN 
                   ', D=' + CONVERT(varchar, cast(dimd AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN dime <> 0 THEN 
                   ', E=' + CONVERT(varchar, cast(dime AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN dimf <> 0 THEN 
                   ', F=' + CONVERT(varchar, cast(dimf AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN dimg <> 0 THEN 
                   ', G=' + CONVERT(varchar, cast(dimg AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN dimn <> 0 THEN 
                   ', N=' + CONVERT(varchar, cast(dimn AS decimal(18, 0)))
                ELSE '' 
            END
) as a, bar_schedule_detail b
where a.typesize = b.typesize
and a.shapecode = b.shapecode
and 
(
   CASE 
     WHEN b.dima <> 0 THEN 
          'A=' + CONVERT(varchar, cast(b.dima AS decimal(18, 0)))
     ELSE '' 
   END + 
     CASE 
         WHEN b.dimb <> 0 THEN 
            ', B=' + CONVERT(varchar, cast(b.dimb AS decimal(18, 0)))
         ELSE '' 
     END + 
     CASE 
         WHEN b.dimc <> 0 THEN 
            ', C=' + CONVERT(varchar, cast(b.dimc AS decimal(18, 0)))
         ELSE '' 
     END + 
     CASE 
         WHEN b.dimd <> 0 THEN 
            ', D=' + CONVERT(varchar, cast(b.dimd AS decimal(18, 0)))
         ELSE '' 
     END + 
     CASE 
         WHEN b.dime <> 0 THEN 
            ', E=' + CONVERT(varchar, cast(b.dime AS decimal(18, 0)))
         ELSE '' 
     END + 
     CASE 
         WHEN b.dimf <> 0 THEN 
            ', F=' + CONVERT(varchar, cast(b.dimf AS decimal(18, 0)))
         ELSE '' 
     END + 
     CASE 
         WHEN b.dimg <> 0 THEN 
            ', G=' + CONVERT(varchar, cast(b.dimg AS decimal(18, 0)))
         ELSE '' 
     END + 
     CASE 
         WHEN b.dimn <> 0 THEN 
            ', N=' + CONVERT(varchar, cast(b.dimn AS decimal(18, 0)))
         ELSE '' 
     END
) = a.dimension
and a.CreatedDtTm = b.CreatedDtTm
and b.active = 1 
and b.barid = @BarId    
ORDER BY
    substring(a.TypeSize,1,1) desc, 
    substring(a.TypeSize,2,len(a.TypeSize)-1)