在SQL Server存储过程中根据财务年度找出季度

时间:2019-04-04 04:55:52

标签: sql-server stored-procedures

我有两个表,一个是Period,另一个是UserTarget

Period表是:

QuarterNo      StartDate    EndDate
-----------------------------------------
   1           2018-04-01   2018-06-30
   2           2018-07-01   2018-09-30
   3           2018-10-01   2018-12-31
   4           2019-01-01   2019-03-31

UserTarget表是:

USERID    YEAR    QTR    AMOUNT
---------------------------------
akshay    2019     1      200

现在我正在从周期表中获取qtr no。现在,我不需要从周期表中获取qtr no。我想从基于年份的存储过程中将它输入到usertarget

现有存储过程:

ALTER PROCEDURE [dbo].[GetQuarterlyTargetData]
    @Userid VARCHAR(50)
AS
BEGIN
    DECLARE @QuarterNumber VARCHAR(50)
    DECLARE @SetTarget DECIMAL(10);
    DECLARE @StartDate DATE
    DECLARE @EndDate DATE

    SELECT
        @QuarterNumber = p.QuarterNo,
        @SetTarget = AMOUNT 
    FROM 
        PERIOD p 
    LEFT OUTER JOIN 
        USERTARGETS s ON p.QuarterNo = s.QTR  
    WHERE
        StartDate <= GETDATE() AND EndDate >= GETDATE() 

    SELECT
        @StartDate = StartDate,
        @EndDate = EndDate 
    FROM 
        PERIOD 
    WHERE
        QuarterNo = @QuarterNumber

通过此过程,我可以获得季度的开始日期和结束日期,但是当我要检查前几年的数据时,不想每次都在期间表中进行修改。

2 个答案:

答案 0 :(得分:0)

我相信您要查找的术语是会计年度。这是公司年度与日历年度不同的地方。

请注意,许多人建议使用查找表而不是计算它。日期匹配对于SQL来说可能难以优化。

这是一种方法。查找财务年度和季度可能会很好地放入表函数中。

DECLARE @userTarget TABLE (UserId VARCHAR(20), Year INT, Quarter INT, Amount INT)
INSERT INTO @userTarget
VALUES
    ('akshay', 2018, 4, 150)
    ,('akshay', 2019, 1, 200)

SELECT
    s.UserId
    ,s.Amount
    ,FY.FiscalYear
    ,FQ.FiscalQuarter
FROM
    (
        SELECT
            --DATEFROMPARTS(2019, 2, 23) 
            GETDATE()
                AS reportDate
    ) AS ReportDate
    CROSS APPLY (
        SELECT
            CASE WHEN MONTH(ReportDate.reportDate) < 4 THEN YEAR(ReportDate.reportDate) - 1 -- Fiscal Year begins in April
                ELSE YEAR(ReportDate.reportDate)
            END AS FiscalYear
    ) AS FY
    CROSS APPLY (
        SELECT
            DATEDIFF(QUARTER, DATEFROMPARTS(FY.FiscalYear, 4, 1), ReportDate.reportDate) + 1 AS FiscalQuarter
    ) AS FQ
    INNER JOIN @userTarget s
        ON s.Year = FY.FiscalYear
            AND s.Quarter = FQ.FiscalQuarter

此外,请注意结束日期。 last_day >= GETDATE()不包括最后一天。以上季度末为例,它将计算为'2019-03-31 00:00' >= '2019-03-31 08:20',如果您希望它为true,则为false。

答案 1 :(得分:0)

考虑之后,我想到了这个解决方案

  <select
        #ethnicity
        [(ngModel)]="patient.Ethnicity"
        [class.text-dimmed]="!patient.Ethnicity"
        name="ethnicity"
        id="ethnicity"
        class="form-control input-underline input-lg ml-0">
          <option [ngValue]="null" disabled> Select </option>
          <option [ngValue]="null">Clear ethnicity</option>
          <option  value="Alaska-Native">Alaska Native</option>
          <option value="American-Indian">American-Indian</option>
          <option value="Asian">Asian</option>
          <option value="African-Americans">Black or African American</option>
          <option value="Hispanic">Hispanic or Latino</option>
          <option value="Jewish-Ashkenazi">Jewish - Ashkenazi</option>
          <option value="Jewish-Other">Jewish - Other</option>
          <option value="Native-Hawaiians">Native Hawaiian or Other Pacific Islander </option>
          <option value="Other">Other </option>
          <option value="Unknown">Unknown </option>
          <option value="White-Caucasian">White or Caucasian</option>
      </select>