TSQL根据Month的名称计算运行数据

时间:2016-09-13 18:45:00

标签: sql-server tsql

我有这样的数据

mis       mnth            IPTV
------------------------------------
0         January         0.2026856
0         February        0.4860267
0         March           0.2650674
0         April           0.1904701
1         January         0.266809
1         February        0.310241
1         March           0.2083876
1         April           0.3401039
2         January         0.307787
2         February        0.5276488
2         March           0.3037852
2         April           0.1822988
3         January         0.8107423
3         February        0.2430134
3         March           0.1988006
3         April           0.2539602

我想根据月份添加累计IPTV。所以“一月”& “二月”看起来应该是这样的,而且本月剩下的时间也应该完全相同。

mis        mnth           IPTV
-------------------------------------------------------------------
0          January        0.2026856
0          February       0.4860267
1          January        0.2026856+ 0.266809
1          February       0.4860267+0.310241
2          January        0.2026856+ 0.266809+0.307787
2          February       0.4860267+0.310241+0.5276488
3          January        0.2026856+ 0.266809+0.307787+0.8107423 
2          February       0.4860267+0.310241+0.5276488+0.2430134 

以及其他几个月。有人可以帮忙吗?

谢谢

3 个答案:

答案 0 :(得分:0)

未经测试

  select mis,mnth,
    stuff((select '+' +cast(iptv as varchar(100))
    from table t2 
    where t1.id<=t2.id and t1.mnth=t2.mnth
    for xml path('')),1,1,'') as val
    from
    table t1

答案 1 :(得分:0)

不确定您是否在寻找TheGameiswar答案或实际运行总数

您可能会注意到我添加月份表只是为了保持序列正确。

label

返回

Declare @YourTable table (mis int,mnth varchar(25),IPTV decimal(18,9)) 
Insert Into @YourTable values (0,'January',0.2026856),(0,'February',0.4860267),(0,'March',0.2650674),(0,'April',  0.1904701),(1,'January',0.266809),(1,'February',0.310241),(1,'March',  0.2083876),(1,'April', 0.3401039),(2,'January',0.307787),(2,'February',0.5276488),(2,'March',0.3037852),(2,'April',0.1822988),(3,'January',0.8107423),(3,'February',0.2430134),(3,'March',0.1988006),(3,'April',0.2539602)

Declare @Months table (Seq int,MnthName varchar(25)) 
Insert into @Months values(1,'January'),(2,'February'),(3,'March'),(4,'April'),(5,'May'),(6,'June'),(7,'July'),(8,'August'),(9,'September'),(10,'October'),(11,'November'),(12,'December')

Select A.mis
      ,A.mnth
      ,IPTV = sum(B.IPTV) 
 From  @YourTable A
 Join  @YourTable B on (A.mnth=B.mnth and B.mis<=A.mis)
 Join  @Months    C on (A.mnth=C.MnthName)
 Group By C.Seq,A.mis,A.mnth
 Order By A.mis,C.Seq

答案 2 :(得分:0)

感谢@John Cappelletti,我的查询在下面列出,基本上这是一个运行总体情景。

#!/bin/bash
# Usage: ./getOld "bucketname" "30 days"
s3cmd ls s3://$1 | while read -r line;  do

createDate=`echo $line|awk {'print $1" "$2'}`
createDate=`date -d"$createDate" +%s`
olderThan=`date -d"-$2" +%s`
if [[ $createDate -lt $olderThan ]]
  then 
    fileName=`echo $line|awk {'print $4'}`
    echo $fileName
    if [[ $fileName != "" ]]
      then
        s3cmd get "$fileName"
    fi
fi
done;

结果如下:

enter image description here