SQL Server - 按组表达式选择结果,但根据另一个组表达式过滤它们

时间:2018-03-20 16:55:12

标签: sql sql-server group-by in-subquery

我正在处理一个问题,我需要根据标准汇总数据。情况如下:

我有来自数据库应用程序的消费数据,我需要显示它显示在表格中。人们可以根据分钟,小时,日和月汇总数据。例如,当人们按分钟汇总数据时,数据将显示在表格中以显示最多10行(但这里是棘手的部分,在此示例中,行是基于小时的)。因此,我不得不说我想要前10行(分钟聚合),我必须找到相当于显示10个不同小时的结果的条目数。

我已尝试进行查询,此时我正在更好地说明问题(查询不会运行):

SELECT  count(*) as COUNT,
  year([CONSUMPTION_DATE]) as YEAR_BY_MINUTE, 
  month([CONSUMPTION_DATE]) as MONTH_BY_MINUTE, 
  day([CONSUMPTION_DATE]) as DAY_BY_MINUTE,
  DATEPART ( hour , [CONSUMPTION_DATE] ) as HOUR_BY_MINUTE,
  DATEPART ( minute , [CONSUMPTION_DATE]) as MINUTE_BY_MINUTE
  FROM [CONSUMPTION_TABLE] 
  where (YEAR_BY_MINUTE, MONTH_BY_MINUTE, DAY_BY_MINUTE, HOUR_BY_MINUTE) IN 

        (select top 10  year([CONSUMPTION_DATE]) as YEAR_BY_HOUR, 
            month([CONSUMPTION_DATE])as MONTH_BY_HOUR, 
            day([CONSUMPTION_DATE]) as DAY_BY_HOUR,
            DATEPART ( hour , [CONSUMPTION_DATE] ) as HOUR_BY_HOUR
            FROM [CONSUMPTION_TABLE]
            where [CONSUMPTION_DATE] between '2018-01-18' and '2018-03-20'
            GROUP BY year([CONSUMPTION_DATE]), 
                     month([CONSUMPTION_DATE]), 
                     day([CONSUMPTION_DATE]),
                     DATEPART ( hour , [CONSUMPTION_DATE])
        )

  GROUP BY  year([CONSUMPTION_DATE]), 
  month([CONSUMPTION_DATE]), 
  day([CONSUMPTION_DATE]),
  DATEPART ( hour , [CONSUMPTION_DATE] ),
  DATEPART ( minute , [CONSUMPTION_DATE] )

我也一直试图通过另一种方式,但查询运行时出现错误想法:

;with MinuteGroup as 
        (
            select  year([CONSUMPTION_DATE]) as YEAR_GROUP_MINUTE, 
                    month([CONSUMPTION_DATE])as MONTH_GROUP_MINUTE, 
                    day([CONSUMPTION_DATE]) as DAY_GROUP_MINUTE,
                    DATEPART ( hour , [CONSUMPTION_DATE] ) as HOUR_GROUP_MINUTE,
                    DATEPART ( minute , [CONSUMPTION_DATE] ) as MINUTE_GROUP_MINUTE
                FROM [CONSUMPTION_TABLE]
                where [CONSUMPTION_DATE] between '2018-01-18' and '2018-03-20'
                group by    year([CONSUMPTION_DATE]), 
                            month([CONSUMPTION_DATE]), 
                            day([CONSUMPTION_DATE]),
                            DATEPART ( hour , [CONSUMPTION_DATE] ),
                            DATEPART ( minute , [CONSUMPTION_DATE] )
        )
    select * from MinuteGroup where 
        (YEAR_GROUP_MINUTE, MONTH_GROUP_MINUTE, DAY_GROUP_MINUTE, HOUR_GROUP_MINUTE) 
            in (    select  year([CONSUMPTION_DATE]), 
                            month([CONSUMPTION_DATE]), 
                            day([CONSUMPTION_DATE]) ,
                            DATEPART ( hour , [CONSUMPTION_DATE] ) 
                    FROM [CONSUMPTION_TABLE]
                    where [CONSUMPTION_DATE] between '2018-01-18' and '2018-03-20'
                    group by year([CONSUMPTION_DATE]), 
                             month([CONSUMPTION_DATE]), 
                             day([CONSUMPTION_DATE]),
                             DATEPART ( hour , [CONSUMPTION_DATE])   
                )

另一种方法是更容易制作这个或我需要在查询中更改哪些才能使它们起作用?

PS:我必须把它转移到HQL,所以如果它更容易,我也会接受答案。

编辑: 由于难以想象我想要实现的目标是图像: enter image description here

(我聚合的单位时间将是一个简单的1,2,3,4,5,......)

1 个答案:

答案 0 :(得分:0)

经过一段时间的搜索并利用其他解决方案,我已经提出了一个SQL查询,我可以得到所需的结果,所以我认为将它发布在这里会很好,这样你就可以更好地理解问题和帮助如果你处于相同的情况。 最终查询保持为:

;with MinuteGroup as 
        (
            select  count(*) as COUNT,
                    year([CONSUMPTION_DATE]) as YEAR_GROUP_MINUTE, 
                    month([CONSUMPTION_DATE])as MONTH_GROUP_MINUTE, 
                    day([CONSUMPTION_DATE]) as DAY_GROUP_MINUTE,
                    DATEPART ( hour , [CONSUMPTION_DATE] ) as HOUR_GROUP_MINUTE,
                    DATEPART ( minute , [DATE_SELECTION_START] ) as MINUTE_GROUP_MINUTE
                FROM [CONSUMPTION_TABLE]
                where [CONSUMPTION_DATE] between '2018-01-18' and '2018-03-20'
                group by    year([CONSUMPTION_DATE]), 
                            month([CONSUMPTION_DATE]), 
                            day([CONSUMPTION_DATE]),
                            DATEPART ( hour , [CONSUMPTION_DATE] ),
                            DATEPART ( minute , [CONSUMPTION_DATE] )
        ),
        HourGroup as 
        (
            select top 10 year([CONSUMPTION_DATE]) as YEAR_GROUP_MINUTE, 
                    month([CONSUMPTION_DATE])as MONTH_GROUP_MINUTE, 
                    day([CONSUMPTION_DATE]) as DAY_GROUP_MINUTE,
                    DATEPART ( hour , [CONSUMPTION_DATE] ) as HOUR_GROUP_MINUTE
                FROM [CONSUMPTION_TABLE]
                where [CONSUMPTION_DATE] between '2018-01-18' and '2018-03-20'
                group by    year([CONSUMPTION_DATE]), 
                            month([CONSUMPTION_DATE]), 
                            day([CONSUMPTION_DATE]),
                            DATEPART ( hour , [CONSUMPTION_DATE] )
        )
    select  MinuteGroup.COUNT,
            MinuteGroup.YEAR_GROUP_MINUTE,
            MinuteGroup.MONTH_GROUP_MINUTE,
            MinuteGroup.DAY_GROUP_MINUTE,
            MinuteGroup.HOUR_GROUP_MINUTE,
            MinuteGroup.MINUTE_GROUP_MINUTE
    from MinuteGroup join HourGroup 
                on  HourGroup.YEAR_GROUP_MINUTE = MinuteGroup.YEAR_GROUP_MINUTE
                and HourGroup.MONTH_GROUP_MINUTE = MinuteGroup.MONTH_GROUP_MINUTE
                and HourGroup.DAY_GROUP_MINUTE = MinuteGroup.DAY_GROUP_MINUTE
                and HourGroup.HOUR_GROUP_MINUTE = MinuteGroup.HOUR_GROUP_MINUTE

并且像这样,而不是拥有此图像的前10行,我有20对应于10个disting小时:

enter image description here