如何计算不同列中的当前月份和上个月值

时间:2017-06-21 07:36:58

标签: sql sql-server

我的数据如下

enter image description here

我需要获得输出 基本上它显示了当月和上个月当前月份的计划(价值)。 enter image description here

例如: 在20160年[201] 5月,计划在201605年作为价值      和扫管笏计划于201604年201605年作为上个月[LM]值

如果缺少一个月的值而没有返回正确的结果

,我尝试使用完全外部联接查询

2 个答案:

答案 0 :(得分:0)

#!/bin/bash
mypath=/a/b/c/d/e
parent=${mypath%/c/d/*}
suffix=${mypath#*/c/d/}
grandparent=${parent%/*}

newpath=$grandparent/newB/c/d/$suffix
echo $newpath

答案 1 :(得分:0)

您只需要通过Created_Month和Period来连接相同的表。

drop table if exists dbo.tMonth;

create table dbo.tMonth (
    Created_Month varchar(100)
    , Period varchar(100)
    , Value int
);

insert into dbo.tMonth (Created_Month, Period, Value)
values ('201604', '201604', 1641)
    , ('201604', '201605', 2458)
    , ('201605', '201604', 6772)
    , ('201605', '201605', 9861);


with cteMonth as (
    select
        convert(date, m.Created_Month + '01', 112) as Created_Month_Date
        , *
    from dbo.tMonth m
)
select
    cm.Created_Month, cm.Period, cm.Value, cmp.Value as LM
from cteMonth cm
    left join cteMonth cmP on cm.Created_Month_Date = dateadd(month, 1, cmp.Created_Month_Date)
            and cm.Period = cmP.Period
order by cm.Created_Month, cm.Period