【问题标题】:How to get data of current date using sum in SQL?如何在 SQL 中使用 sum 获取当前日期的数据?
【发布时间】:2020-12-06 22:02:57
【问题描述】:

我正在处理一个 SQL 查询,试图获取当前日期/日期的总和数据。任何人都可以查看我的查询并为我找到可行的解决方案吗?

SELECT SUM(amount) 
FROM tbl_expense_record 
WHERE dateonly = CAST(GETDATE() AS Date)

但我在提及特定日期时会得到数据,例如

SELECT SUM(amount) AS total 
FROM tbl_expense_record 
WHERE dateonly = '2020-06-12'

我希望代码自动选择当前日期。我还想获取整个星期和一个月等范围日期的总和!

【问题讨论】:

  • 运行select getdate()。我的猜测是它将输出2020-12-06 (yyyy-mm-dd) 而不是2020-06-12 (yyyy-dd-mm)。这将解释差异。另外,看看this question and answer
  • 顺序无关紧要,我已经解决了这个问题,并进行了转换和转换,但结果保持不变。我不明白为什么 getdate 会返回 null 值。
  • 请提供minimal, reproducable example 的示例数据。此外,如果 select cast(getdate() as date), cast('2020-06-12' as date) 没有产生相同的值,那么你的转换/转换是有缺陷的。
  • 请看第二张图片,显示表中存在数据,问题中提到了列名。通过尝试您建议的查询,我确实得到了相同的结果。

标签: sql-server


【解决方案1】:
select datename(month, '2020-06-12'), datename(month, getdate());


--1week
SELECT SUM(amount) AS total 
FROM tbl_expense_record 
WHERE dateonly >= dateadd(week, -1, cast(getdate() as date))
and dateonly <= cast(getdate() as date)

--1month
SELECT SUM(amount) AS total 
FROM tbl_expense_record 
WHERE dateonly >= dateadd(month, -1, cast(getdate() as date))
and dateonly <= cast(getdate() as date)

--build muscle memory (it is always safe to check for < date+1 instead of <= date)

--1month
SELECT SUM(amount) AS total 
FROM tbl_expense_record 
WHERE dateonly >= dateadd(month, -1, cast(getdate() as date))
and dateonly < dateadd(day, 1, cast(getdate() as date));


--6months
SELECT SUM(amount) AS total 
FROM tbl_expense_record 
WHERE dateonly >= dateadd(month, -6, cast(getdate() as date))
and dateonly < dateadd(day, 1, cast(getdate() as date));


if not exists
(
select *
FROM tbl_expense_record 
WHERE dateonly >= dateadd(month, -1, cast(getdate() as date))
and dateonly < dateadd(day, 1, cast(getdate() as date))
)
begin
    select 'no rows within the last month'
end
else
begin
    select 'there are rows within the last month';
end;

示例:

declare @tbl_expense_record table(dateonly date, amount decimal(9,2));

insert into @tbl_expense_record
values ('20200501', 10), ('20200612', 10), ('20200613', 11), ('20200614', 12),
('20200710', 5), ('20200720', 6), ('20200820', 20), ('20200825', 30),
('20201102', 1), ('20201110', 2), ('20201120', 3);

--aggregation per month, for all rows
select year(dateonly) as _year, month(dateonly) as _month, sum(amount) as sum_amount_per_month, count(*) as rows_per_month
from @tbl_expense_record
group by year(dateonly), month(dateonly);

--aggregation per iso-week
select year(dateonly) as _year, datepart(iso_week, dateonly) as _isoweek, sum(amount) as sum_amount_per_isoweek, count(*) as rows_per_isoweek
from @tbl_expense_record
group by year(dateonly), datepart(iso_week, dateonly);



--aggregation per month, for all rows with a dateonly that falls in the last month
--check the difference between aggregation per month earlier and this..
--filter rows first == where .... and then aggregate
--there are two rows with dateonly > 06 november (the row at 05 is filtered out by the where clause)
select year(dateonly) as _year, month(dateonly) as _month, sum(amount) as sum_amount_per_month, count(*) as rows_per_month
from @tbl_expense_record
where dateonly >= dateadd(month, -1, cast(getdate() as date))
and dateonly < dateadd(day, 1, cast(getdate() as date))
group by year(dateonly), month(dateonly);



--aggregate per week diff from today/getdate()
select 
datediff(week, getdate(), dateonly) as week_diff_from_today, 
dateadd(day,
        --datepart(weekday..) is used...account for @@datefirst setting / set datefirst
        1-(@@datefirst+datepart(weekday, dateadd(week, datediff(week, getdate(), dateonly), cast(getdate() as date))))%7, 
        dateadd(week, datediff(week, getdate(), dateonly), cast(getdate() as date)))
 as startofweek, 
dateadd(day, 6, --add 6 days to startofweek
dateadd(day, 
        --datepart(weekday..) is used...account for @@datefirst setting / set datefirst
        1-(@@datefirst+datepart(weekday, dateadd(week, datediff(week, getdate(), dateonly), cast(getdate() as date))))%7, 
        dateadd(week, datediff(week, getdate(), dateonly), cast(getdate() as date)))
) as endofweek,

sum(amount) as sum_amount, count(*) as rows_within_week
from @tbl_expense_record
group by datediff(week, getdate(), dateonly);

【讨论】:

  • 感谢你们付出了多少努力,在尝试了所有这些查询后我的问题仍然存在我仍然得到“空”结果:/
  • 太棒了!有效。你能帮我理解这里发生了什么吗?我已经完全疯了。
  • ...表格中没有属于上周或上个月的行...检查最后一个查询..编辑答案
  • 6months 确实有效,现在我的问题是如何分别获得一周记录和一个月记录?我的表包含 2 天的数据
  • 非常感谢,一切都很完美!另外还有很多新东西可以学习-竖起大拇指-
猜你喜欢
  • 2015-05-06
  • 2020-07-20
  • 1970-01-01
  • 2010-11-17
  • 2021-04-05
  • 1970-01-01
  • 1970-01-01
  • 2021-07-27
  • 1970-01-01
相关资源
最近更新 更多