我按日期进行sql统计。
这意味着按日期划分的访客总数。
select sum(r.count) from (SELECT DATE_FORMAT(reg_date,'%Y-%m-%d') m, COUNT(distinct client_ip) as count FROM request_log GROUP BY m) as r;
以下是此sql的结果[在此处输入图像描述][1]
所以,我想把这个换成 QueryDSL
. 我阅读了以下文件并尝试了这些。http://www.querydsl.com/static/querydsl/3.6.3/reference/ko-kr/html_single/#alias
@Transactional
@SpringBootTest
public class QueryDslTest {
@PersistenceContext
EntityManager em;
@Test
@DisplayName("QueryDSL_TEST")
public void testQuerydsl() throws Exception {
JPAQueryFactory queryFactory = new JPAQueryFactory(em);
QRequestLog requestLog = new QRequestLog("requestLog");
StringTemplate dateFormat = Expressions.stringTemplate(
"DATE_FORMAT({0}, {1})"
, requestLog.regDate
, ConstantImpl.create("%Y-%m-%d"));
JPQLQuery<Long> countDistinct = JPAExpressions.select(requestLog.clientIp.countDistinct());
QueryResults<Tuple> count = queryFactory
.select(requestLog.regDate, requestLog.clientIp.countDistinct().as("count"))
.from(requestLog)
.groupBy(dateFormat, requestLog.regDate)
.fetchResults();
// Long fetch = queryFactory
// .select(Projections.fields(Long.class, ExpressionUtils.as(
// select(requestLog.regDate, requestLog.clientIp.countDistinct().as("c"))
// .from(requestLog)
// .groupBy(dateFormat, requestLog.regDate), "r")
// ))
// .from(requestLog)
// .fetchOne();
// JPAQuery<Tuple> tupleJPAQuery = queryFactory
// .select(dateFormat, countDistinct)
// .from(requestLog)
// .groupBy(dateFormat);
//
// queryFactory
// .select(tupleJPAQuery)
System.out.println("count = " + count);
// queryFactory
// .select(requestLog)
// .from(
// select(requestLog.regDate.as("d"), requestLog.clientIp.countDistinct().as("c"))
// .from(requestLog)
// .groupBy(requestLog.as("d"))
// )
}
}```
I don't know how to use an subquery alias.
Can you give me a hint?
[1]: https://i.stack.imgur.com/WUMkl.jpg
1条答案
按热度按时间j13ufse21#
我使用了mysql的date函数。这不是日期格式函数!
但我仍然不知道如何解释子查询。
所以,我使用了select查询和java流求和。
我还是觉得我需要多学习。