[英]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.