数据库每条数据有商户id字段、数量number字段、时间createtime字段 现在要查询近七日每个商户的number总数 根据日期分组 如果数据不满7天 需补齐剩余日期数据 mysql java spring boot、
时间: 2024-02-13 09:02:37 浏览: 128
MySQL查询语句如下:
```sql
SELECT
merchant_id,
DATE_FORMAT(date, '%Y-%m-%d') as date,
COALESCE(SUM(number), 0) as total_number
FROM (
SELECT
merchant_id,
DATE(c.createtime) as date,
0 as number
FROM
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7) d
LEFT JOIN
(SELECT DISTINCT merchant_id FROM your_table_name) m
ON 1=1
LEFT JOIN
your_table_name c
ON
m.merchant_id=c.merchant_id AND DATE(c.createtime) = DATE_SUB(CURRENT_DATE, INTERVAL d.d DAY)
WHERE
d.d BETWEEN 0 AND 6
UNION ALL
SELECT
merchant_id,
DATE(c.createtime) as date,
SUM(number) as number
FROM
your_table_name c
WHERE
c.createtime >= DATE_SUB(CURRENT_DATE, INTERVAL 6 DAY)
GROUP BY
merchant_id, DATE(c.createtime)
) t
GROUP BY
merchant_id, date
ORDER BY
merchant_id, date;
```
上面的查询语句会返回每个商户在过去七天内每天的number总数,如果数据不满7天,会自动补齐剩余日期的数据。
Java Spring Boot代码实现如下:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class TotalNumberByMerchantService {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<TotalNumberByMerchantDTO> getTotalNumberByMerchant() {
String sql = "SELECT merchant_id, DATE_FORMAT(date, '%Y-%m-%d') as date, COALESCE(SUM(number), 0) as total_number "
+ "FROM ( "
+ " SELECT merchant_id, DATE(c.createtime) as date, 0 as number "
+ " FROM (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7) d "
+ " LEFT JOIN (SELECT DISTINCT merchant_id FROM your_table_name) m ON 1=1 "
+ " LEFT JOIN your_table_name c ON m.merchant_id=c.merchant_id AND DATE(c.createtime) = DATE_SUB(CURRENT_DATE, INTERVAL d.d DAY) "
+ " WHERE d.d BETWEEN 0 AND 6 "
+ " UNION ALL "
+ " SELECT merchant_id, DATE(c.createtime) as date, SUM(number) as number "
+ " FROM your_table_name c "
+ " WHERE c.createtime >= DATE_SUB(CURRENT_DATE, INTERVAL 6 DAY) "
+ " GROUP BY merchant_id, DATE(c.createtime) "
+ ") t "
+ "GROUP BY merchant_id, date "
+ "ORDER BY merchant_id, date";
List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
List<TotalNumberByMerchantDTO> list = new ArrayList<>();
for (Map<String, Object> row : rows) {
TotalNumberByMerchantDTO dto = new TotalNumberByMerchantDTO();
dto.setMerchantId((int) row.get("merchant_id"));
dto.setDate((String) row.get("date"));
dto.setTotalNumber((int) row.get("total_number"));
list.add(dto);
}
return list;
}
public static void main(String[] args) {
TotalNumberByMerchantService service = new TotalNumberByMerchantService();
List<TotalNumberByMerchantDTO> list = service.getTotalNumberByMerchant();
System.out.println(list);
}
}
class TotalNumberByMerchantDTO {
private int merchantId;
private String date;
private int totalNumber;
public int getMerchantId() {
return merchantId;
}
public void setMerchantId(int merchantId) {
this.merchantId = merchantId;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public int getTotalNumber() {
return totalNumber;
}
public void setTotalNumber(int totalNumber) {
this.totalNumber = totalNumber;
}
@Override
public String toString() {
return "TotalNumberByMerchantDTO [merchantId=" + merchantId + ", date=" + date + ", totalNumber=" + totalNumber
+ "]";
}
}
```
上面的Java代码使用了Spring Boot的JdbcTemplate来连接到MySQL数据库并执行上述查询语句,将结果封装成DTO并返回。请将代码中的`your_table_name`替换为实际的表名。
阅读全文