【问题标题】:Is there any way to count two times the same date column?有没有办法计算两次相同的日期列?
【发布时间】:2019-11-15 07:13:51
【问题描述】:
select CONVERT(VARCHAR(10),accounting.dates,103) as date_of_acc, numbers,
count(accounting.dates) as total from #a
left join accounting on #a.code = accounting.code
where numbers in (5,74)
group by accounting.dates,numbers
order by date_of_acc,numbers;




 date_of_acc    numbers  total
 01/07/2019        5      43
 01/07/2019        5      53
 01/10/2019        5      72
 01/10/2019        5      40
 01/10/2019        5      44
 01/10/2019        5      71
 01/10/2019        5      76
 01/10/2019        5      77

我需要再次计算列“date_of_acc”,因为他没有特别在一个日期将我全部计算在内, 我需要的是这个日期“01/07/2019”是一个值,并且该总数需要加一。 这也适用于其他日期值。

【问题讨论】:

  • MySQL?真的吗? MySQL 有 CONVERT(..., 103) 函数吗?

标签: sql sql-server group-by


【解决方案1】:

您可以在下面尝试 - 在 group by 中添加 CONVERT(VARCHAR(10),accounting.dates,103)

select CONVERT(VARCHAR(10),accounting.dates,103) as date_of_acc, numbers,
count(accounting.dates) as total from #a
left join accounting on #a.code = accounting.code
where numbers in (5,74)
group by CONVERT(VARCHAR(10),accounting.dates,103),numbers
order by date_of_acc,numbers

【讨论】:

  • 这就是我需要的,谢谢。我忘记了我可以将整个 CONVERT(VARCHAR(10),accounting.dates,103) 放在 GROUP BY 语句中。
【解决方案2】:

这个:

group by accounting.dates

将为相同的日期但不同的小时、分钟、秒和毫秒生成单独的组。您只需按日期部分分组:

select convert(varchar(10), cast(accounting.dates as date), 103) as date_of_acc
     , numbers
     , count(accounting.dates) as total
from #a
left join accounting on #a.code = accounting.code
where numbers in (5,74)
group by cast(accounting.dates as date), numbers
order by date_of_acc, numbers;

【讨论】:

    【解决方案3】:

    如果你把你的 sql 改成这个,你就会明白为什么会出现多行相同的日期:

    select 
      accounting.dates as date_of_acc,   
      numbers,
      count(accounting.dates) as total 
    from #a
    left join accounting on #a.code = accounting.code
    where numbers in (5,74)
    group by accounting.dates,numbers
    order by date_of_acc,numbers;
    

    accounting.dates 会有一些差异(可能像一个时间),其中多个日期具有相同的日期但不同的时间。分组是在包含时间的情况下完成的,然后在显示日期时去掉时间,让您想知道为什么同一天的日期会出现多次

    这在 sql 中是允许的(按值分组,然后在显示之前更改值)但不是你想要的

    要修复它,请在 select 和 GROUP BY 中执行 CONVERT。这意味着它将是被分组和计算的转换日期;没有必要“再做一次”——我们只需要确保数据在第一次分组时按照我们想要的方式形成

    【讨论】:

      【解决方案4】:

      我不知道我是否理解正确。在您表示希望收到的示例中:

      date_of_acc      numbers    total
      01/07/2019       10         96
      01/10/2019       30         380 (if I calculated correctly)
      

      如果是,那么您应该将查询更改为:

      select
          CONVERT(VARCHAR(10),accounting.dates,103) as date_of_acc,
          SUM(numbers), -- Here changed
          count(accounting.dates) as total
          from
              #a
              left join accounting
                  on #a.code = accounting.code
          where
              numbers in (5,74)
          group by
              CONVERT(VARCHAR(10),accounting.dates,103) -- Here another change
          order by
              date_of_acc -- and last one
      

      如果它不符合您的要求,请详细说明您的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-03
        • 2021-03-13
        • 1970-01-01
        相关资源
        最近更新 更多