简体   繁体   中英

Subtract nonconsecutive values in same row in t-SQL

I have a data table that has annual data points and quarterly data points. I want to subtract the quarterly data points from the corresponding prior annual entry, eg Annual 2014 - Q3 2014, using t-SQL. I have an id variable for each entry, plus a reconcile id variable that shows which quarterly entry corresponds to which annual entry. See below:

CurrentDate PreviousDate Value  Entry Id  Reconcile Id  Annual/Quarterly
9/30/2012   9/30/2011    112      2            3             Annual
9/30/2013   9/30/2012    123      1            2             Annual
9/30/2014   9/30/2013    123.5    9            1             Annual
12/31/2013  9/30/2014    124      4            1            Quarterly
3/31/2014   12/31/2013   124.5    5            1            Quarterly
6/30/2014   3/31/2014    125      6            1            Quarterly
9/30/2014   6/30/2014    125.5    7            1            Quarterly
12/31/2014  9/30/2014    126      10           9            Quarterly
3/31/2015   12/31/2014   126.5    11           9            Quarterly
6/30/2015   3/31/2015    127      12           9            Quarterly

For example, Reconcile ID 9 for the quarterly entries corresponds to Entry ID 9, which is an annual entry.

I have code to just subtract the prior entry from the current entry, but I cannot figure out how to subtract quarterly entries from annual entries where the Entry ID and Reconcile ID are the same.

Here is the code I am using, which is resulting in the right calculation, but increasing the number of results by many rows. I have also tried this as an inner join. I only want the original 10 rows, plus a new difference column:

 SELECT DISTINCT T1.[EntryID] 
    , [T1].[RECONCILEID] 
    , [T1].[CurrentDate] 
    , [T1].[Annual_Quarterly] 
    , [T1].[Value] 
    , [T1].[Value]-T2.[Value] AS Difference 
    FROM Table T1 
    LEFT JOIN Table T2 ON T2.EntryID = T1.RECONCILEID;

Your question is a bit vague as far as how you're wanting to subtract these values, but this should give you some idea.

Select      T1.*, T1.Value - Coalesce(T2.Value, 0) As Difference
From        Table   T1
Left Join   Table   T2  On  T2.[Entry Id] = T1.[Reconcile Id]

Your code should be fine, here's the results I'm getting:

EntryId Annual_Quarterly    CurrentDate ReconcileId Value   recVal  diff
2       Annual              9/30/2012   3           112     
1       Annual              9/30/2013   2           123     112     11
9       Annual              9/30/2014   1           123.5   123     0.5
4       Quarterly           12/31/2013  1           124     123     1
5       Quarterly           3/31/2014   1           124.5   123     1.5
6       Quarterly           6/30/2014   1           125     123     2
7       Quarterly           9/30/2014   1           125.5   123     2.5
10      Quarterly           12/31/2014  9           126     123.5   2.5
11      Quarterly           3/31/2015   9           126.5   123.5   3
12      Quarterly           6/30/2015   9           127     123.5   3.5

with your data and this SQL:

SELECT  
    tr.EntryId, 
    tr.Annual_Quarterly, 
    tr.CurrentDate,
    tr.ReconcileId, 
    tr.Value, 
    te.Value AS recVal, 
    tr.[VALUE]-te.[VALUE] AS diff
FROM 
    t AS tr LEFT JOIN 
    t AS te     ON 
    tr.ReconcileId = te.EntryId
ORDER BY 
    tr.Annual_Quarterly, 
    tr.CurrentDate;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM