CTE是否在sybase中使用?

时间:2011-12-13 00:20:30

标签: sql sybase common-table-expression

我只是想知道如果cte也用于sybase ......我知道cte用于sql server ..但不知道它们是否用于sybase ......

如果Cte不用于sybase,sybase中是否有任何其他方法在sql server中作为CTE执行... ??

我有一个像::

这样的查询
select CONVERT(VARCHAR(7)
  , [startdatetime],111) AS [year-month]
  , nm.nameLine1 AS CompanyName
  , sum(datediff(hour, startdatetime, enddatetime)) as total 
  from  srl 
  inner join  sr on srl.ServiceRequestId = sr.ServiceRequestId
  inner join Name nm
          on (sr.clientCustomerId = nm.customerId 
         and nm.nameTypeId = 'OFICE')
  where (startdatetime >= '08-01-2011 00:00:00.000'
    and enddatetime <= '10-31-2011 00:00:00.000')
  group by  CompanyName, [year-month]
  order by  CompanyName, [year-month]

以上查询的输出:

year-month CompanyName        total       
---------- -----------     ----------- 
2011/08    B                    4 
2011/09    B                    7 
2011/10    B                    0 
2011/08    E                   167 
2011/09    E                   212 
2011/10    E                   131 
2011/08    L                    14 
2011/09    L                    23 
2011/10    L                     3 
2011/08    O                    18 
2011/09    O                     8 
2011/10    O                     7 
2011/08    S                    43 
2011/09    S                    60 
2011/10    S                    60 

我希望总计在输出中显示为:: (我希望输出如下)

year-month CompanyName        total        companytotals   
---------- -----------     -----------    --------------- 
2011/08    B                    4 
2011/09    B                    7 
2011/10    B                    0            11
2011/08    E                   167 
2011/09    E                   212 
2011/10    E                   131           510
2011/08    L                    14 
2011/09    L                    23 
2011/10    L                     3          40
2011/08    O                    18 
2011/09    O                     8 
2011/10    O                     7           33
2011/08    S                    43 
2011/09    S                    60 
2011/10    S                    60          163

有没有办法做到这一点......?

提前致谢..

1 个答案:

答案 0 :(得分:1)

从版本9开始,Sybase ASA确实支持CTE,但据我所知,我没有使用Sybase产品。对于您的查询,您不需要CTE,我会使用普通ranking functions

将以下内容添加到选择中:

, SUM(totals) OVER (PARTITION BY [year-month], [CompanyName]) AS companytotals