我在表上的查询需要满足以下条件:
表格:
user_id || amount || date
1 || 10 || 2019-04-01
1 || 25 || 2019-04-02
3 || 25 || 2019-04-04
1 || 25 || 2019-04-03
预期结果:获取user_id,日期,日期数量,月份数量(将user_id的所有日期金额总计为月份)
输入参数:user_id,日期,月份的开始日期,月份的结束日期。
例如:user_id = 1,日期= 2019-04-02,开始日期=月份= 2019-04-01,结束日期=月份= 2019-04-30
user_id || amount_of_date || date || amount_of_month
1 || 25 || 2019-04-02 || 60
答案 0 :(得分:1)
您可以在下面尝试-
select user_id,
max(case when date=@inputdate then amount end) as amount_of_Date,
min(case when date=@inputdate then date end) as date,
sum(amount) as amount_of_month
from tablename
where user_id=1 and date between '2019-04-01' and '2019-04-30'
group by user_id