【问题标题】:How do I change the below query to pull by day and not month?如何将以下查询更改为按天而不是按月拉?
【发布时间】:2020-10-13 23:48:32
【问题描述】:

如何将以下查询更改为按天而不是按月提取?

select CONVERT(CHAR(4), dCompletedDate, 100) + CONVERT(CHAR(4), dCompletedDate, 120) as MonthYear,
count(case when dCompletedDate is not null then 1 else 0 end) as 'Total Completes'
from TProfile a
left join TStudyTable b
on a.lhouseholdid=b.lhouseholdid
where
dcompleteddate >= '1/1/1990' and dcompleteddate < '1/1/2050'
group by CONVERT(CHAR(4), dCompletedDate, 100) + CONVERT(CHAR(4), dCompletedDate, 120)
order by MonthYear

【问题讨论】:

    标签: sql sql-server date select count


    【解决方案1】:

    假设dcompleteddatedatetime 数据类型,您可以将cast 转换为date

    select 
        cast(dcompleteddate as date) as dcompletedday,
        count(*) as total_completes
    from tprofile 
    left join tstudytable s on p.lhouseholdid = s.lhouseholdid
    where dcompleteddate >= '1990-01-01' and dcompleteddate < '2050-01-01'
    group by cast(dcompleteddate as date)
    order by dcompletedday
    

    旁注:

    • 我将条件 count() 更改为 count(*);您正在过滤dcompleteddate,因此可以保证它不是null(即使是,您当前的表达式仍然会计算null 值)

    • 我还更改了 where 子句以使用更标准的文字日期

    【讨论】:

      猜你喜欢
      • 2011-09-18
      • 2021-07-14
      • 1970-01-01
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多