如何通过联合集中的sql查询为以下查询进行排序

时间:2018-03-28 08:20:44

标签: sql-server-2008 sql-order-by

作为

开始

选择'项目购买',格式(总和(价格),' ##,## 0。#0')来自购买,其中Approved_Date喜欢'%&# 39; +' /' + @ +一年'%'和Approved_Month = @ month和类别不在('资产','民事义务','项目义务''报销')

联合

选择'资产',格式(总和(价格),' ##,## 0。#0')来自购买,其中Approved_Date喜欢'%&#39 ; +' /' + @ +一年'%'和Approved_Month = @ month和Category ='资产'

联合

选择'民事义务',格式(总和(价格),' ##,## 0。#0')来自购买,其中Approved_Date喜欢'%&# 39; +' /' + @ +一年'%'和Approved_Month = @ month和Category =' Civil Obligation'

联合

选择'项目义务',格式(总和(价格),' ##,## 0.#0')来自购买,其中Approved_Date喜欢'%&# 39; +' /' + @ +一年'%'和Approved_Month = @ month和Category =' Project Obligation'

联合

选择'报销',格式(总和(价格),' ##,## 0。#0')来自购买,其中Approved_Date喜欢'%&#39 ; +' /' + @ +一年'%'和Approved_Month = @ month和Category =' Reimbursement'

1 个答案:

答案 0 :(得分:1)

您可以选择整个联合结果,如下所示

select * from
(
select 'Project Purchase' as column1 ,Format(sum(Price),'##,##0.#0') as total from purchase where Approved_Date like '%'+'/'+@year+'%' and Approved_Month =@month and Category not in ('Assets','Civil Obligation','Project Obligation','Reimbursement')

union

select 'Assets' as column1,Format(sum(Price),'##,##0.#0') as total from purchase where Approved_Date like '%'+'/'+@year+'%' and Approved_Month =@month and Category='Assets'

union

select 'Civil Obligation' as column1,Format(sum(Price),'##,##0.#0') as total from purchase where Approved_Date like '%'+'/'+@year+'%' and Approved_Month =@month and Category='Civil Obligation'

union

select 'Project Obligation' as column1,Format(sum(Price),'##,##0.#0') as total from purchase where Approved_Date like '%'+'/'+@year+'%' and Approved_Month =@month and Category='Project Obligation'

union

select 'Reimbursement' as column1,Format(sum(Price),'##,##0.#0') as total from purchase where Approved_Date like '%'+'/'+@year+'%' and Approved_Month =@month and Category='Reimbursement'
) t order by t.total