有一个表A ,有字段date,值是 2023-01-31,2023-02-28,2023-03-06, 还有一个表B,有金额和日期两个字段,值是2023-03-09 00:00:00.000000 47260 2022-12-29 00:00:00.000000 28566 2022-08-01 00:00:00.000000 3378.7 2023-03-29 00:00:00.000000 4296 2023-03-06 00:00:00.000000 2369 2022-12-26 00:00:00.000000 51100 2023-02-07 00:00:00.000000 33057 2021-08-11 00:00:00.000000 11500 2022-04-14 00:00:00.000000 23219.8 2022-02-09 00:00:00.000000 100010 2021-12-31 00:00:00.000000 59100 写一个oracle sql 计算B表的日期分别在表A中的每个日期的前一年到该日期时间的B表的金额的总和
时间: 2023-08-21 19:06:34 浏览: 162
可以使用Oracle的日期函数和子查询来解决这个问题。以下是一个可能的SQL语句:
```
SELECT A.date, SUM(B.amount) AS total_amount
FROM A, B
WHERE B.date BETWEEN ADD_MONTHS(A.date, -12) AND A.date
GROUP BY A.date;
```
这个SQL语句首先使用一个笛卡尔积将A表和B表组合在一起,然后使用WHERE子句筛选出B表中日期在A表中每个日期的前一年到该日期时间范围内的记录。接着,使用GROUP BY子句将结果按A表中的日期分组,并使用SUM函数计算每个日期范围内的B表金额总和。最终结果将会是每个日期和对应的B表金额总和。
相关问题
userInfo表结构为:{ "_id": { "$numberLong": "1672939910539612162" }, "region": "region2", "registerTime": { "$numberLong": "1687608531051" }, "lastLoginTime": { "$numberLong": "1688990931052" }, "liftDays": 30, "uniqId": "14fe5f1a-bdff-49b0-ab35-3b7ead6b7b90", "openId": "0d087060-c80a-48dd-ace3-3df981cba4ea", "deviceModel": "xiaomi", "country": "de", "channel": "huawei", "nick": "nick", "vip": "0", "propsMap": { "levelIn": "20", "levelFinish": "20", "dailyChallengeFinish": "7", "town": "10", "townBuild": "{1:[1,2,3,4,5],2:[1],3:[1,2,3],4:[1]}", "dailyChallengeIn": "8", "lv": "3" }, "_class": "com.dobest.model.mongo.UserInfo", "registerTimeDate": "2023-06-24 20:08:51", "lastLoginTimeDate": "2023-07-10 20:08:51" } event表结构为:{ "_id": { "$oid": "64a2b6bf1f5d3943f2e1a4e2" }, "userId": { "$numberLong": "1672939935722213448" }, "eventType": 0, "registerTime": { "$numberLong": "1687176537065" }, "date": "2023-06-21", "cnt": 4, "updateTime": { "$numberLong": "1687349337065" }, "onlineTime": { "$numberLong": "18" }, "_class": "com.dobest.model.mongo.Event", "registerTimeDate": "2023-06-19 20:08:57", "updateTimeDate": "2023-06-21 20:08:57" } 求eventType=0且2023-04-12<=date<=2023-07-12且2023-06-01 00:00:00<=registerTimeDate<2023-06-02 00:00:00求字段townBuild字段key为1对应的值包含3的_id总数
根据给出的userInfo和event表的结构,您可以使用以下MongoDB查询语句来获取满足条件的_id总数:
```python
db.event.count({
"eventType": 0,
"date": {
"$gte": "2023-04-12",
"$lte": "2023-07-12"
},
"registerTimeDate": {
"$gte": "2023-06-01 00:00:00",
"$lt": "2023-06-02 00:00:00"
},
"townBuild.1": "3"
})
```
请注意,这里假设您的数据库名称为`db`,而且您需要将查询语句中的集合名称进行相应的更改。
userInfo表结构为:{ "_id": { "$numberLong": "1672939910539612162" }, "region": "region2", "registerTime": { "$numberLong": "1687608531051" }, "lastLoginTime": { "$numberLong": "1688990931052" }, "liftDays": 30, "uniqId": "14fe5f1a-bdff-49b0-ab35-3b7ead6b7b90", "openId": "0d087060-c80a-48dd-ace3-3df981cba4ea", "deviceModel": "xiaomi", "country": "de", "channel": "huawei", "nick": "nick", "vip": "0", "propsMap": { "levelIn": "20", "levelFinish": "20", "dailyChallengeFinish": "7", "town": "10", "townBuild": "{1:[1,2,3,4,5],2:[1],3:[1,2,3],4:[1]}", "dailyChallengeIn": "8", "lv": "3" }, "_class": "com.dobest.model.mongo.UserInfo", "registerTimeDate": "2023-06-24 20:08:51", "lastLoginTimeDate": "2023-07-10 20:08:51" } event表结构为:{ "_id": { "$oid": "64a2b6bf1f5d3943f2e1a4e2" }, "userId": { "$numberLong": "1672939935722213448" }, "eventType": 0, "registerTime": { "$numberLong": "1687176537065" }, "date": "2023-06-21", "cnt": 4, "updateTime": { "$numberLong": "1687349337065" }, "onlineTime": { "$numberLong": "18" }, "_class": "com.dobest.model.mongo.Event", "registerTimeDate": "2023-06-19 20:08:57", "updateTimeDate": "2023-06-21 20:08:57" } 求eventType=0且2023-04-12<=date<=2023-07-12且2023-06-01 00:00:00<=registerTimeDate<2023-06-02 00:00:00且townBuild字段key为1对应的值包含3的_id总数
根据给出的userInfo和event表的结构,您可以使用以下MongoDB查询语句来获取满足条件的_id总数:
```python
db.event.count({
"eventType": 0,
"date": {
"$gte": "2023-04-12",
"$lte": "2023-07-12"
},
"registerTimeDate": {
"$gte": "2023-06-01 00:00:00",
"$lt": "2023-06-02 00:00:00"
},
"townBuild.1": {
"$in": ["3"]
}
})
```
请注意,这里假设您的数据库名称为`db`,而且您需要将查询语句中的集合名称进行相应的更改。
阅读全文