如何在SQL查询中使用别名获取列的总和 [英] How to get the sum of a column using alias in SQL query

查看:76
本文介绍了如何在SQL查询中使用别名获取列的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得别名sub_total的总和。



这是我的查询

i want to get the sum of the alias sub_total.

here's my query

SELECT SUM(Project_Total) AS Sub_Total, Project_Code, SectionCode
FROM Temp_Portal
WHERE (Project_Code = ?)
GROUP BY Project_Code, SectionCode
HAVING (Project_Code LIKE 'BA%') AND (SectionCode LIKE 'RN%')





这里是上面查询的结果





here is the result of the query above

Project_Code    SectionCode    Sub_Total
BA82014       |       RN1     |   86
BA82014       |       RN      |  18372







我需要的结果是这一个




The result i need is this one

<pre>
    Project_Code    SectionCode    Sub_Total
    BA82014       |       RN1     |   86
    BA82014       |       RN      |  18372   
                           Total  :  18458 </pre>





我需要得到rn和rn1的总和。

有人知道怎么做吗?





提前致谢!



I need to get the total sum of rn and rn1.
somebody know how to do this?


Thanks in advance!

推荐答案





查看此...





Hi,

Check this...


SELECT SUM(X.Sub_Total)
FROM
(
SELECT SUM(Project_Total) AS Sub_Total, Project_Code, SectionCode
FROM Temp_Portal
WHERE (Project_Code = '?' )
GROUP BY Project_Code, SectionCode
HAVING (Project_Code LIKE 'BA%') AND (SectionCode LIKE 'RN%')
) X







希望这会对你有所帮助。



干杯




Hope this will help you.

Cheers


在查询结尾处使用WITH CUBE子句将在结果的末尾为您提供一个额外的行,其中sub_total和所有其他列值的总和将为null。所以在一个查询中你可以获得一切





Using the WITH CUBE clause at the end of your query will give you an additional row at the end of your result with the sum of the sub_total and all other column values will be null. So in one query you can get everything


SELECT SUM(Project_Total) AS Sub_Total, Project_Code, SectionCode
FROM Temp_Portal
WHERE (Project_Code = ?)
GROUP BY Project_Code, SectionCode
HAVING (Project_Code LIKE 'BA%') AND (SectionCode LIKE 'RN%')
WITH CUBE


SELECT SUM(Project_Total) AS Sub_Total, Project_Code, SectionCode
FROM Temp_Portal
WHERE (Project_Code = ?)
GROUP BY Cube(Project_Code, SectionCode)
HAVING (Project_Code LIKE 'BA%') AND (SectionCode LIKE 'RN%')


这篇关于如何在SQL查询中使用别名获取列的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆