Sap ase 包含包含GROUPBY子句的求和行

Sap ase 包含包含GROUPBY子句的求和行,sap-ase,Sap Ase,查询: SELECT aType, SUM(Earnings - Expenses) "Rev" FROM aTable GROUP BY aType ORDER BY aType ASC | aType | Rev | | ----- | ----- | | A | 20 | | B | 150 | | C | 250 | 结果: SELECT aType, SUM(Earnings - Expenses) "Rev" FROM aTable GR

查询:

SELECT aType, SUM(Earnings - Expenses) "Rev"
FROM aTable
GROUP BY aType
ORDER BY aType ASC
| aType | Rev   |
| ----- | ----- |
| A     | 20    |
| B     | 150   |
| C     | 250   |
结果:

SELECT aType, SUM(Earnings - Expenses) "Rev"
FROM aTable
GROUP BY aType
ORDER BY aType ASC
| aType | Rev   |
| ----- | ----- |
| A     | 20    |
| B     | 150   |
| C     | 250   |
问题: 在我的初始查询中使用Sybase语法,是否可以在下面这样的底部显示摘要行,或者它必须是一个单独的查询

| aType | Rev   |
| ----- | ----- |
| A     | 20    |
| B     | 150   |
| C     | 250   |
=================
| All   | 320   |
我无法从SQL获取汇总函数以成功地转换到Sybase,但我不确定是否有其他方法可以做到这一点(如果有的话)


谢谢

并非所有Sybase版本都支持汇总。你可以用老式的方式来做:

with t as 
    (SELECT aType, SUM(Earnings - Expenses) "Rev"
     FROM aTable
     GROUP BY aType
    )
select t.*
from ((select aType, rev from t) union all
      (select NULL, sum(rev))
     ) t
ORDER BY (case when atype is NULL then 1 else 0 end), aType ASC
这是一种令人恶心的暴力手段。如果此版本的Sybase不支持带有的
,则可以执行以下操作:

select t.aType, t.Rev
from ((SELECT aType, SUM(Earnings - Expenses) "Rev"
       FROM aTable
       GROUP BY aType
      ) union all
      (select NULL, sum(rev))
     ) t
ORDER BY (case when atype is NULL then 1 else 0 end), aType ASC

这是非常基本的标准SQL。

并非所有Sybase版本都支持汇总。你可以用老式的方式来做:

with t as 
    (SELECT aType, SUM(Earnings - Expenses) "Rev"
     FROM aTable
     GROUP BY aType
    )
select t.*
from ((select aType, rev from t) union all
      (select NULL, sum(rev))
     ) t
ORDER BY (case when atype is NULL then 1 else 0 end), aType ASC
这是一种令人恶心的暴力手段。如果此版本的Sybase不支持带有的
,则可以执行以下操作:

select t.aType, t.Rev
from ((SELECT aType, SUM(Earnings - Expenses) "Rev"
       FROM aTable
       GROUP BY aType
      ) union all
      (select NULL, sum(rev))
     ) t
ORDER BY (case when atype is NULL then 1 else 0 end), aType ASC

这是非常基本的标准SQL。

您可以在sybase中使用compute by子句进行计算,如:

create table #tmp1( name char(9), earning int , expense int) 
insert into #tmp1 values("A",30,20)
insert into #tmp1 values("B",50,30)
insert into #tmp1 values("C",60,30)

select name, (earning-expense) resv from #tmp1
group by name
order by name,resv
compute sum(earning-expense)

谢谢,
Gopal

可能是您可以在sybase中使用compute by子句进行计算,如:

create table #tmp1( name char(9), earning int , expense int) 
insert into #tmp1 values("A",30,20)
insert into #tmp1 values("B",50,30)
insert into #tmp1 values("C",60,30)

select name, (earning-expense) resv from #tmp1
group by name
order by name,resv
compute sum(earning-expense)

谢谢,
Gopal

您是否尝试过使用类似于以下内容的
联合ALL

select aType, Rev
from
(
  SELECT aType, SUM(Earnings - Expenses) "Rev", 0 SortOrder
  FROM aTable
  GROUP BY aType
  UNION ALL
  SELECT 'All', SUM(Earnings - Expenses) "Rev", 1 SortOrder
  FROM aTable
) src
ORDER BY SortOrder, aType
看。结果如下:

| ATYPE | REV |
---------------
|     A |  10 |
|     B | 150 |
|     C | 250 |
|   All | 410 |

您是否尝试过使用与此类似的
联合所有

select aType, Rev
from
(
  SELECT aType, SUM(Earnings - Expenses) "Rev", 0 SortOrder
  FROM aTable
  GROUP BY aType
  UNION ALL
  SELECT 'All', SUM(Earnings - Expenses) "Rev", 1 SortOrder
  FROM aTable
) src
ORDER BY SortOrder, aType
看。结果如下:

| ATYPE | REV |
---------------
|     A |  10 |
|     B | 150 |
|     C | 250 |
|   All | 410 |

按汇总分组(aType)
应在Sybaser中工作。我收到错误:“未找到函数‘汇总’。如果这是SQLJ函数或SQL函数,请使用sp_help…”@Matt。Sybase的哪个版本?
GROUP BY ROLLUP(aType)
应该在Sybase rs中工作。我收到错误:“未找到函数‘ROLLUP’。如果这是SQLJ函数或SQL函数,请使用sp_help…”@Matt。什么版本的Sybase?即使附加了;在前面,就像SQL()中的情况一样。此外,我尝试使用一个临时表来使用它,但仍然不好:/@Matt。真令人沮丧。我猜您的Sybase版本不支持
with
语句。看起来是这样的!管理员控制也不在我的掌握之中,谢谢你的帮助,尽管伙计,谢谢你。这仍然不起作用,即使有附件;在前面,就像SQL()中的情况一样。此外,我尝试使用一个临时表来使用它,但仍然不好:/@Matt。真令人沮丧。我猜您的Sybase版本不支持
with
语句。看起来是这样的!管理员控制也不在我的掌握之中,谢谢你的帮助,伙计,谢谢。