如何计算多列值的总和?

时间:2016-07-16 08:56:36

标签: sql sql-server select

我正在训练计算新的" Total"我在查询中计算过的三个新列中的列,请参见下文:

    import sys
    words_count_map_dict = {}
    fobj = open(sys.argv[1],"r")
    words = fobj.read().split('\n')[:-1]

    def alphabet_count_mapper(word):
        alpha_count_dict = dict(zip('abcdefghijklmnopqrstuvwxyz',[0]*26))
        for alpha in word:
            if alpha in alpha_count_dict.keys():
                alpha_count_dict[alpha] += 1
            else:
                alpha_count_dict.update(dict(alpha=0))
        return tuple(alpha_count_dict.values())

    def anagram_counter(words):
        anagram_count = 0
        for word in words:
            temp_mapper = alphabet_count_mapper(word)
            if temp_mapper in words_count_map_dict.keys():
                words_count_map_dict[temp_mapper] += 1
            else:
                words_count_map_dict.update({temp_mapper:1})
        for val in words_count_map_dict.values():
            if val > 1:
                anagram_count += val
        return anagram_count


    print anagram_counter(words)

全新" Total"列应具有下一个数据:空闲+分配+代理

4 个答案:

答案 0 :(得分:1)

当然,您可以重复生成IdleAssignDelegate的计算,但是,好吧..哎呀。另一种选择是将此查询用作子查询,以避免重复计算:

SELECT *, Idle + Assign + Delegate AS Total
FROM   (SELECT TOP 500 request_key,
                       request_id,
                       CreatedTime,
                       AssignTime,
                       IssueTime,
                       CloseTime,
                       RequestDetails,
                       request_type,
                       Product,
                       SiteName,
                       DATEDIFF(MINUTE, createdtime, AssignTime + 1) AS Idle,
                       DATEDIFF(MINUTE, AssignTime, IssueTime + 1) AS Assign,
                       DATEDIFF(MINUTE, IssueTime, CloseTime +1 ) AS Delegate
        FROM [wombat].[dbo].[VwWombatCrossSiteCorrelationReport]) t

答案 1 :(得分:0)

(DATEDIFF(MINUTE, createdtime, AssignTime + 1) AS Idle
      + DATEDIFF(MINUTE, AssignTime, IssueTime + 1) AS Assign
        + DATEDIFF(MINUTE, IssueTime, CloseTime +1 ) AS Delegate) AS total

答案 2 :(得分:0)

我只是简单地添加另一个计算列:

SELECT TOP 500 request_key
      ,request_id
      ,CreatedTime
      ,AssignTime
      ,IssueTime
      ,CloseTime
      ,RequestDetails
      ,request_type
      ,Product
      ,SiteName
      ,DATEDIFF(MINUTE, createdtime, AssignTime + 1) AS Idle
      ,DATEDIFF(MINUTE, AssignTime, IssueTime + 1) AS Assign
      ,DATEDIFF(MINUTE, IssueTime, CloseTime +1 ) AS Delegate
     , Total = DATEDIFF(MINUTE, createdtime, AssignTime + 1) + DATEDIFF(MINUTE, AssignTime, IssueTime + 1) + DATEDIFF(MINUTE, IssueTime, CloseTime +1 )
 FROM [wombat].[dbo].[VwWombatCrossSiteCorrelationReport]

答案 3 :(得分:0)

有趣的方法是使用outer apply

SELECT TOP 500 cr.*, v.idle, v.assign, v.delegate,
       (v.idle + v.assign + v.delegate) as Total
FROM [wombat].[dbo].[VwWombatCrossSiteCorrelationReport] cr outer apply
     (select DATEDIFF(MINUTE, createdtime, AssignTime + 1) as Idle,
             DATEDIFF(MINUTE, AssignTime, IssueTime + 1) as Assign,
             DATEDIFF(MINUTE, IssueTime, CloseTime +1 ) as Delegate
     ) v;