YTD Sale

  • Hi

    I have below Stored Procedure . I want YTd column value also.

    If user enters @frDate = 01/01/2021 and @toDate = 31/01/2021 then YTD value should be from 01/04/2020 to 31/01/2021

    If user enters @frDate = 01/12/2020 and @toDate = 31/12/2020 then YTD value should be from 01/04/2020 to 31/12/2020

    Create PROCEDURE [dbo].[SpBpGroupWiseSalePurchase]

    @frDate date,

    @toDate date,

    @CType nvarchar(1)

    AS

    BEGIN

    SELECT code,name

    (Select sum(INvTotal) from Invoice where dt between @frDate and @ToDate ) as "Total Sales"

    from InvoiceHeader T0

    End

    Thanks

  • Can you provide some consumable data, please? That is, some CREATE TABLE and INSERT statements which we can paste into SSMS and execute?

    Also, please explain the link between Invoice and InvoiceHeader, as your query above does not show one.

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • How do you determine that 2021-01-04 (January 4th, 2021) is the first day of the year?  Is that valid for every year - or do other years start on a different day?  I am guessing, but it looks like you want the year to start on day 1 of ISO week 1 (which is the Monday prior to the first Thursday of the year).

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply