简体   繁体   English

SQL查询联接一列的平均值,其中另一列在另一表的两列值之间

[英]Sql query join average of one column where another column is between two columns values of another table

I have two tables, one has two columns that have dates. 我有两个表,一个有两列有日期。 The other has one column with dates and another with numeric data. 另一列包含日期,另一列包含数字数据。 I would like to join the average of the second table's numeric column values where its date column is in between the values of the two date columns in the first table. 我想加入第二张表的数字列值的平均值,其中它的日期列在第一张表的两个日期列的值之间。 Something like the following: 类似于以下内容:

Table1: 表格1:

Date1        Date2 
6/28 2:00  6/30 4:00
7/1 4:00  7/4  7:00
...

Table2: 表2:

Date3    Value
6/29 1:00  6.5
6/30 3:00  2.5
7/1 5:00  3.0
7/3 9:00  5.0
...

FinalTable: FinalTable:

Date1        Date2   AvgValue
6/28 2:00  6/30 4:00  4.5
7/1 4:00  7/4  7:00  4.0

You can use the between operator in your join condition: 您可以在联接条件中使用between运算符:

SELECT   date1, date2, AVG(value)
FROM     table1
JOIN     table2 ON date3 BETWEEN date1 AND date2
GROUP BY date1, date2
SELECT   date1, date2, AVG(value)
FROM     table1 t1
JOIN     table2 t2 ON t3.date3 >= t1.date1 AND t3.date3 < t1.date2
GROUP BY t1.date1, t1.date2

OR 要么

SELECT   date1, date2, AVG(value)
FROM     table1 t1
JOIN     table2 t2 ON t3.date3 > t1.date1 AND t3.date3 <= t1.date2
GROUP BY t1.date1, t1.date2

Using BETWEEN is great, however it is inclusive of both sides of the range. 使用BETWEEN很棒,但是它涵盖了范围的两侧。 So if you have overlapping ranges in your dual date table eg 6/28 2:00 to 6/28:4:00 AND 6/28:4:00 to 6/28 6:00 then any value landing at 4 PM would be repeated and skew your average. 因此,如果双重日期表中的范围重叠,例如6/28 2:00 to 6/28:4:00 AND 6/28:4:00 to 6/28 6:006/28 2:00 to 6/28:4:00 AND 6/28:4:00 to 6/28 6:00 4点的任何价值重复并倾斜您的平均值。

You can fix this one of 2 ways. 您可以通过2种方法之一来解决此问题。

  • Make sure your BETWEEN doesn't cause an overlap 6/28 2:00 to 6/28 3:59:59.997 确保您的BETWEEN不会造成重叠6/28 2:00 to 6/28 3:59:59.997
  • not use BETWEEN and write out the greater than/equal to or less than to only be inclusive of 1 side of your range as in the examples above. 请勿使用BETWEEN并写出大于/等于或小于等于范围的1个边,如上面的示例所示。

Note for DATETIME for sql-server is accurate to 3 milliseconds so if you put in 3:59:59.999 or .998 it will be treated the same as 4:00:00.000, the way to solve is use .997. 注意sql-server的DATETIME精确到3毫秒,因此,如果您输入3:59:59.999或.998,则将其与4:00:00.000相同,解决方法是使用.997。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL连接-一列用作另一表中两列的ID - SQL join - one column serving as ID for two columns in another table SQL语句可查询表中一列的平均值,并从另一张表中联接并查询相应的列 - SQL Statement to query average of one column in a table, join and query corresponding column from another table sql 查询在一个表列上连接两个表等于另一个表中的某些列 - sql query join two tables on one table column equals to some columns in another table 如何将SQL表中的2列连接到另一个SQL表中的一列? - How to join 2 columns in a SQL table to one column in another SQL table? 将一个表中的两列连接到另一引用表中的列 - Join two columns in one table to a column in another reference table 将一个表的两列连接到另一表的同一列 - Join two columns of one table to the same column of another table 如何将一个表中的两个或更多列与另一个表中的一个列联接在一起,即使第一个表列中的值为NULL - How to join two or more columns from one table with one column from another table, even there are NULL values in first table columns sql两个列的一个表引用另一个表中的同一列 - sql two columns of one table reference same column in another table 将 SQL Query 中两列之间的百分比计算为另一列 - Calculate percentage between two columns in SQL Query as another column sql通过一个列值而不在另一个表的列中连接两个表 - sql join two tables by one column values not in another table's coumn
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM