用SQL查询计算平均值

时间:2018-07-04 08:04:13

标签: sql date average

我想知道从2018年3月开始每个用户的平均小时工资。我想用SQL查询来计算。

我有这种数据:

enter image description here

开始日期指示小时工资何时开始以及何时有适用小时工资的新开始日期等。

结果应该是:

2 €10,65
4 €9,90

如何在SQL中执行此操作?

2 个答案:

答案 0 :(得分:1)

请尝试以下查询。创建日历表,然后创建cte以添加缺少日期的丢失数据。一旦我们有了丢失日期的数据,就可以在WHERE子句和GROUPBY中进行简单过滤。

IF OBJECT_ID ('tempdb..#temp') IS NOT NULL
DROP TABLE #temp

IF OBJECT_ID('tempdb..#calendar') IS NOT NULL
    DROP TABLE #calendar;

CREATE TABLE #temp
(
UserId INT,
StartDate DATE,
HourlyWage decimal(10,2)
)

DECLARE @date DATE = '20180305'
INSERT INTO #temp VALUES 
(2,'20180205', 10.3),
(2,@date, 10.3),
(2,DATEADD(DAY,1,@date), 10.3),
(2,DATEADD(DAY,2,@date), 10.3),
(2,DATEADD(DAY,3,@date), 10.3),
(2,DATEADD(DAY,4,@date), 10.3),
(2,DATEADD(DAY,5,@date), 10.3),
(2,DATEADD(DAY,6,@date), 10.3),
(2,DATEADD(DAY,7,@date), 10.3),
(2,DATEADD(DAY,8,@date), 10.3),
(2,DATEADD(DAY,9,@date), 10.3),
(2,DATEADD(DAY,10,@date), 10.3),
(2,DATEADD(DAY,11,@date), 10.3),
(2,DATEADD(DAY,12,@date), 10.3),
(2,DATEADD(DAY,13,@date), 10.3),
(2,DATEADD(DAY,14,@date), 10.3),
(2,DATEADD(DAY,15,@date), 10.3),
(2,DATEADD(DAY,16,@date), 10.3),
(2,DATEADD(DAY,17,@date), 10.3),
(2,DATEADD(DAY,18,@date), 10.3),
(2,DATEADD(DAY,19,@date), 10.3),
(2,DATEADD(DAY,20,@date), 10.3),
(2,DATEADD(DAY,21,@date), 10.3),
(2,DATEADD(DAY,22,@date), 12.5),
(2,DATEADD(DAY,23,@date), 12.5),
(2,DATEADD(DAY,24,@date), 12.5),
(2,DATEADD(DAY,25,@date), 12.5),
(2,DATEADD(DAY,26,@date), 12.5),
(4,'20170221', 9.5),
(4,'20170301', 9.7),
(4,'20180227', 9.9)

DECLARE @FromDate DATETIME ,
        @ToDate DATETIME;

SELECT @FromDate = MIN(StartDate) ,
       @ToDate = MAX(StartDate)
FROM   #Temp;


SELECT   TOP ( DATEDIFF(day, @FromDate, @ToDate) + 1 ) calendarDate = CAST(DATEADD(
                                                                                 DAY ,
                                                                                 number ,
                                                                                 @FromDate) AS DATE) 
INTO     #calendar
FROM     [master].dbo.spt_values
WHERE    [type] = N'P'
ORDER BY number;

;WITH tempCTE
AS ( SELECT  DISTINCT t.userid ,
                      cal.calendarDate
     FROM    (   SELECT   calendarDate
                 FROM     #calendar c
                 ) cal
             CROSS JOIN (   SELECT UserId ,
                                   MIN(StartDate) OVER ( PARTITION BY UserId ) AS Mindate ,
                                   MAX(StartDate) OVER ( PARTITION BY UserId ) AS Maxdate 
                            FROM   #Temp ) t
     WHERE   cal.calendarDate
     BETWEEN t.Mindate AND t.Maxdate )

         SELECT   cal.UserId ,
         AVG(x.HourlyWage ) AS Avg_Wage
FROM     tempCTE cal
         CROSS APPLY (   SELECT   TOP 1 t.HourlyWage
                         FROM     #Temp t
                         WHERE    t.UserId = cal.UserId
                                  AND t.StartDate <= cal.calendarDate
                         ORDER BY t.StartDate DESC ) AS x
WHERE CalendarDate BETWEEN '20180301' AND '20180331' --your date filter here
GROUP BY cal.UserId

结果:

+--------+-----------+
| UserId | Avg_Wage  |
+--------+-----------+
|      2 | 10.654838 |
+--------+-----------+

答案 1 :(得分:0)

以下查询将每个web.xml的所有记录分组,然后计算平均值。对于分组,我们使用了userid子句。

此外,MONTH函数还返回一个整数,该整数表示指定日期的月份。由于您对3月份感兴趣,因此我们将其与3进行比较。

group by

请记住使用实际表名更改Select UserId, avg(HourlyWage) from <<table>> where month(StartDate) = 3 group by UserId