【问题标题】:Select timestamp for one date and for a period time on the same field in one query在一个查询的同一字段中选择一个日期和一段时间的时间戳
【发布时间】:2019-05-28 03:32:34
【问题描述】:

我对表格的查询需要以下条件:

表:

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,start_date_of_month = 2019-04-01,end_date_of_month = 2019-04-30

user_id || amount_of_date || date       || amount_of_month
1       || 25             || 2019-04-02 || 60

【问题讨论】:

    标签: mysql sql postgresql


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      相关资源
      最近更新 更多