SQL Server 如何将3个查询合并为一个查询?

4

我希望将以下三个查询合并为一个查询,并将总数输出到3列中。哦,如何使它不必声明日期。我希望它能“知道”当前的日期、月份和年份。

DECLARE @myDate as datetime
SET @myDate = '2015-01-1'
select SUM(Amount) as 'Day Total' 
from [Accounting].[dbo].[HandPay] 
where AccountingDate>=@myDate and AccountingDate<dateadd(day,1,@myDate)

select SUM(Amount) as 'Month Total' 
from [Accounting].[dbo].[HandPay] 
where AccountingDate>=@myDate and AccountingDate<dateadd(MONTH,1,@myDate)

select SUM(Amount) as 'Day Total' 
from [Accounting].[dbo].[HandPay] 
where AccountingDate>=@myDate and AccountingDate<dateadd(year,1,@myDate)

什么是最佳实践?感谢所有超快的反应!问题已经解决。
3个回答

3

如果我正确理解了问题,那么像这样的东西应该可以解决:

declare @today date = convert(date, getdate());
select
    [Day Total]   = sum(case when [AccountingDate] >= @today and [AccountingDate] < dateadd(day,   1, @today) then [Amount] else 0 end),
    [Month Total] = sum(case when [AccountingDate] >= @today and [AccountingDate] < dateadd(month, 1, @today) then [Amount] else 0 end),
    [Year Total]  = sum(case when [AccountingDate] >= @today and [AccountingDate] < dateadd(year,  1, @today) then [Amount] else 0 end)
from
    [Accounting].[dbo].[HandPay];

注意,[Month Total][Year Total]不是给出当前月/年内发生的条目总数,而是给出离今天日期一个月/一年内发生的条目总数。我不确定这是否符合您的要求,但这似乎与原始查询一致。
更新:如D Stanley所建议的那样,您可以简化一下,因为您知道组成[Day Total][Month Total]总数的日期范围完全包含在组成[Year Total]总数的日期范围内。这是可能会长成这个样子:
declare @today date = convert(date, getdate());
select
    [Day Total]   = sum(case when [AccountingDate] < dateadd(day,   1, @today) then [Amount] else 0 end),
    [Month Total] = sum(case when [AccountingDate] < dateadd(month, 1, @today) then [Amount] else 0 end),
    [Year Total]  = sum([Amount])
from
    [Accounting].[dbo].[HandPay]
where
    [AccountingDate] >= @today and [AccountingDate] < dateadd(year, 1, @today);

1
很好的回答,但你还可以添加一个通用的“where”子句来限制搜索到最近一年的内容,否则编译器可能会使用表扫描。 - D Stanley
好主意,@DStanley。我已经更新了答案,包括您的建议。 - Joe Farrell
我真的很喜欢这个答案,但在测试结果之前需要访问实时数据库。谢谢! - Brian
我修改了这个程序,以便可以根据设定的日期测试单独的结果。数据库中最新的可用日期是2015年4月15日。然而,在这一天,我得到了NULL作为结果,而2015年4月14日给出了相同的日期、月份和年份的返回值,而我期望它将最近30天添加到该月,然后将最近365天添加到该年。 - Brian

1
将它们全部作为“超级”选择的一部分。
  DECLARE @myDate as datetime
  SET @myDate = '2015-01-1'
SELECT
(select SUM(Amount) 
from [Accounting].[dbo].[HandPay] 
where AccountingDate>=@myDate and AccountingDate<dateadd(day,1,@myDate) ) as 'Day Total',

(select SUM(Amount) 
from [Accounting].[dbo].[HandPay] 
where AccountingDate>=@myDate and AccountingDate<dateadd(MONTH,1,@myDate) )  as 'Month Total',

(select SUM(Amount)
from [Accounting].[dbo].[HandPay] 
where AccountingDate>=@myDate and AccountingDate<dateadd(year,1,@myDate) )  as 'Day Total' 

非常准确!非常感谢! - Brian

1
一种方法是将每个子查询作为一个包含的选择语句编写。我经常发现这很有用,因为它允许我逐步组合查询。当没有记录时,这将在列值中给出null - 这可能不是您想要的。请参考@JoeFarrell的问题,因为他提供了使用case将null转换为0的示例。
(请原谅奇怪的布局,感觉值得强调逗号)
DECLARE @myDate as datetime
SET @myDate = '2015-01-1'
select 
    (select SUM(sale_total)
    from [Accounting].[dbo].[HandPay]
    where AccountingDate>=@myDate and AccountingDate<dateadd(day,1,@myDate))  as 'Day Total' 
    ,
    (select SUM(sale_total) 
    from [Accounting].[dbo].[HandPay]
    where AccountingDate>=@myDate and AccountingDate<dateadd(MONTH,1,@myDate)) as 'Month Total' 
    ,
    (select SUM(sale_total) 
    from [Accounting].[dbo].[HandPay]
    where AccountingDate>=@myDate and AccountingDate<dateadd(year,1,@myDate)) as 'Day Total' 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接