通过汇总分组以在SQL Server中获取记录?

时间:2019-02-21 19:19:45

标签: sql sql-server group-by aggregate-functions

我需要对SP列进行分组,并过滤​​Min的{​​{1}}和Max的值,并在Select Query中获取结果。

TABLE An

T

我尝试使用小型查询,如果我在查询中添加了列RawS RawI SS SP T 14299 52 14299 105 1 14299 53 14299 105 2 14299 54 14299 105 3 14299 56 14299 106 2 14299 57 14299 106 3 14299 58 14299 107 1 14299 59 14299 107 2 14299 60 14299 107 3 ,那么我在使用聚合函数时会遇到SQL错误。

Raws

需要输出格式。

SELECT 
     ar1.SP,
     MIN(ar1.T) AS T1,
     MAX(ar1.T) AS T2 FROM An ar1
     Group by  ar1.SP

3 个答案:

答案 0 :(得分:1)

这是您的选择。子查询以获取每个SP的最小和最大T。然后2个联接以获取其他数据。

DECLARE @TestData TABLE
    (
        [RawS] INT
      , [RawI] INT
      , [SS] INT
      , [SP] INT
      , [T] INT
    );

INSERT INTO @TestData (
                          [RawS]
                        , [RawI]
                        , [SS]
                        , [SP]
                        , [T]
                      )
VALUES ( 14299, 52, 14299, 105, 1 )
     , ( 14299, 53, 14299, 105, 2 )
     , ( 14299, 54, 14299, 105, 3 )
     , ( 14299, 56, 14299, 106, 2 )
     , ( 14299, 57, 14299, 106, 3 )
     , ( 14299, 58, 14299, 107, 1 )
     , ( 14299, 59, 14299, 107, 2 )
     , ( 14299, 60, 14299, 107, 3 );


SELECT     [a1].[RawS] AS [RawS1]
         , [a1].[RawI] AS [RawI1]
         , [a2].[RawS] AS [RawS1]
         , [a2].[RawI] AS [RawI2]
         , [a].*
FROM       (
               SELECT   [SP]
                      , MIN([T]) AS [T1]
                      , MAX([T]) AS [T2]
               FROM     @TestData
               GROUP BY [SP]
           ) AS [a]
INNER JOIN @TestData [a1]
    ON [a1].[SP] = [a].[SP]
       AND [a1].[T] = [a].[T1]
INNER JOIN @TestData [a2]
    ON [a2].[SP] = [a].[SP]
       AND [a2].[T] = [a].[T2];

答案 1 :(得分:0)

出现 可获得您想要的结果:

WITH VTE AS(
    SELECT *
    FROM (VALUES (14299,52,14299,105,1),
                 (14299,53,14299,105,2),
                 (14299,54,14299,105,3),
                 (14299,56,14299,106,2),
                 (14299,57,14299,106,3),
                 (14299,58,14299,107,1),
                 (14299,59,14299,107,2),
                 (14299,60,14299,107,3))V(RawS,RawI,SS,SP,T))
SELECT RawS,
       Min(RawI) AS RawI1,
       SS AS RawS2,
       MAX(RawI) AS RawI2,
       SP,
       MIN(T) AS T1,
       MAX(T) AS T2
FROM VTE
GROUP BY RawS,
         SS,
         SP;

不知道为什么会出错,因为该查询运行良好db<>fiddle,这意味着您不是正在运行的查询。

答案 2 :(得分:0)

这不是MIN和MAX函数,而是按SP和SS分组(不在所需的输出中)吗?

with An as (
      select 14299 as RawS ,52 as RawI ,14299 as SS ,105 as SP ,1 as T
union select 14299 as RawS ,53 as RawI ,14299 as SS ,105 as SP ,2 as T
union select 14299 as RawS ,54 as RawI ,14299 as SS ,105 as SP ,3 as T
union select 14299 as RawS ,56 as RawI ,14299 as SS ,106 as SP ,2 as T
union select 14299 as RawS ,57 as RawI ,14299 as SS ,106 as SP ,3 as T
union select 14299 as RawS ,58 as RawI ,14299 as SS ,107 as SP ,1 as T
union select 14299 as RawS ,59 as RawI ,14299 as SS ,107 as SP ,2 as T
union select 14299 as RawS ,60 as RawI ,14299 as SS ,107 as SP ,3 as T
)
select min(RawS) as RawS1
     , min(RawI) as RawI1
     , max(RawS) as RawS2
     , max(RawI) as RawI2
     , SP
     , min(T) as T1
     , max(T) as T2
  from An
group by SP, SS