简体   繁体   English

如何在 SQL 服务器存储过程中将当前月份作为参数传递给前一个月的总数?

[英]How to get the total of a previous month having passed the current month as a parameter in a SQL Server stored procedure?

Through the following query, I am able to get the total for the current month from a data.table.通过以下查询,我能够从 data.table 中获取当月的总数。

SELECT SUM(MONTODEBITO) - SUM(MONTOCREDITO)
FROM PRUEBAOPEX
WHERE MONTH(FECHA) = 1 AND YEAR(FECHA) = 2021

Since I am working inside a stored procedure, I have declared a variable that is equal to the following query.因为我在存储过程中工作,所以我声明了一个等于以下查询的变量。

CREATE PROCEDURE SP_VALORS
    (@NIVEL VARCHAR(15),
     @MES INT,
     @AÑO INT)
AS
BEGIN
    DECLARE @TOTALMESACTUAL FLOAT, @TOTALMESPASADO FLOAT

    SET @TOTALMESACTUAL = (SELECT SUM(MONTODEBITO) - SUM(MONTOCREDITO) 
                           FROM PRUEBAOPEX 
                           WHERE MONTH(FECHA) = @MES 
                             AND YEAR(FECHA) = @AÑO)
END

When executing the procedure, it returns the value correctly.执行该过程时,它会正确返回值。

在此处输入图像描述

I also need to declare another variable that displays the total for the month before the month I pass as a parameter in my procedure.我还需要声明另一个变量,该变量显示我在过程中作为参数传递的那个月之前的那个月的总数。

I have the following example:我有以下示例:

SELECT SUM(MONTODEBITO) - SUM(MONTOCREDITO)
FROM PRUEBAOPEX
WHERE MONTH(FECHA) = 2-1 AND YEAR(FECHA) = 2021

When I send 2 (February) to 12 (December) as a parameter, it shows me correctly.当我发送 2(二月)到 12(十二月)作为参数时,它正确地显示了我。

More than all my doubt is in the case of sending 1 (January) as a parameter.我最怀疑的是发送 1(一月)作为参数的情况。 It should show me the total for December of the previous year.它应该显示上一年 12 月的总数。 How could I declare this variable?我怎么能声明这个变量?

Thank you for any help you may receive.感谢您提供的任何帮助。

You will get better performance from the current code by changing it like this:通过像这样更改当前代码,您将获得更好的性能:

DECLARE @Month DateTime = DatefromParts(@AÑO, @MES, 1)

SET @TOTALMESACTUAL = (SELECT SUM(MONTODEBITO) - SUM(MONTOCREDITO) 
                       FROM PRUEBAOPEX 
                       WHERE @Month <= FECHA and FECHA < Dateadd(month, 1, @Month)
)

The improved performance is because the FECHA column now remains unaltered for the query, and will therefore work better (at all) with any indexes you have.改进的性能是因为FECHA列现在对于查询保持不变,因此将更好地(完全)与您拥有的任何索引一起工作。

More importantly, this is also very easy to convert to get the previous month:更重要的是,这也很容易转换为上个月:

DECLARE @Month DateTime = DatefromParts(@AÑO, @MES, 1)

Set @Month = DATEADD(month, -1, @Month)

SET @TOTALMESACTUAL = (SELECT SUM(MONTODEBITO) - SUM(MONTOCREDITO) 
                       FROM PRUEBAOPEX 
                       WHERE @Month <= FECHA and FECHA < Dateadd(month, 1, @Month)
)

You should be using date boundaries, not using syntax like WHERE MONTH(SomeDate) = @SomeMonthNumber .您应该使用日期边界,而不是使用WHERE MONTH(SomeDate) = @SomeMonthNumber类的语法。 This means you can make a SARGable query, and you can easily apply better logic to the date boundary you want.这意味着您可以进行 SARGable 查询,并且可以轻松地将更好的逻辑应用于所需的日期边界。

An easy way to get a date from a month and year value is to use DATEFROMPARTS .从月份和年份值中获取日期的一种简单方法是使用DATEFROMPARTS As you want the whole of a specific month, you can use 1 for the day of the month.当您想要整个特定月份时,您可以使用1作为该月的第几天。

After making some other changes, as there are some odd choices in your procedure (variables that are declared and not used, a lack of the procedure returning anything), I suspect you want something like this:进行一些其他更改后,由于您的过程中有一些奇怪的选择(已声明但未使用的变量,缺少返回任何内容的过程),我怀疑您想要这样的东西:

--I have dropped the prefix as I recommend in the comments of your prior question.
CREATE PROCEDURE dbo.VALORS @NIVEL varchar(15), @MES int, @AÑO int, @TOTALMESACTUAL float = NULL OUTPUT AS 
BEGIN

    SELECT @TOTALMESACTUAL = SUM(MONTODEBITO - MONTOCREDITO) --There's no need for a subquery here
    FROM dbo.PRUEBAOPEX 
    WHERE FECHA >= DATEFROMPARTS(@AÑO, @MES, 1)
      AND FECHA < DATEADD(DAY, 1, DATEFROMPARTS(@AÑO, @MES, 1));

END;

If you wanted to get the prior month, you can easily apply a further DATEADD to subtract a month from the expression ( DATEADD(MONTH, -1, <Expression>) ).如果您想获得一个月,您可以轻松地应用进一步的DATEADD从表达式中减去一个月 ( DATEADD(MONTH, -1, <Expression>) )。

Also, I suspect that float is not the right choice for your data type, and that a base-10 data type would be better;另外,我怀疑float不是您数据类型的正确选择,以 10 为基数的数据类型会更好; though without knowing what SUM(MONTODEBITO - MONTOCREDITO) represents, I haven't changed the data type.虽然不知道SUM(MONTODEBITO - MONTOCREDITO)代表什么,但我没有更改数据类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Server:获取当前月份和前几个月 - SQL Server: get current month and previous months 如何通过 SQL 查询显示当前和上个月的名称以及当前每月收入总额和以前每月收入总额? - How to display Current & Previous Month Name along with Total of Current Revenue per month & Total of Previous Revenue per month through SQL Query? 如何获取上一年(12月)和月份显示在SQL中的当前月份 - how to get previous year (month december) and month showing current month in sql SQL Server:如何在每个日历月末运行存储过程 - SQL Server : how to run a stored procedure at the end of each calendar month 根据用户选择获取当前日期、当前月份、当前年份 - SQL 存储过程 - Get Current date, current month, current year depending on user selection - SQL stored procedure 如何在 SQL Server 中获取上一季度的月份和年份 - How to get previous -previous quarters month and year in SQL Server 如何获取上个月和本月的累计总数? - How to get cumulative total for previous month and upto this month? 如何在SQL查询中求和前一个月的值和当前一个月的值 - How to sum previous month values and current month values in SQL query 如何使用 Cognos SQL 将当前月份和上个月实施为计算 - How to implement Current Month and Previous Month as a Calculation using Cognos SQL 如何获取前 6 个月的数据,从 oracle sql 中的当前月份开始按月分组? - How to get previous 6 months data, group by each month starting with current month in oracle sql?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM