2015-10-16 91 views
0

我有一个SQL查询,给了我两列与一些项目的价格。在同一查询中是否可以合并这两列以获得总列数?有任何想法吗?如何将从查询中获得的两列合并到同一查询中的第三列中?

当前代码:

Select A.Company_Name, 

(Select COUNT(v_rpt_Configuration.Config_Name) 
From v_rpt_Configuration 
Where v_rpt_Configuration.Company_RecID=A.Company_RecID and v_rpt_Configuration.Config_Type = 'Managed Workstation' and v_rpt_Configuration.ConfigStatus = 'Active') AS Workstations, 

(Select COUNT(v_rpt_Configuration.Config_Name) 
From v_rpt_Configuration 
Where v_rpt_Configuration.Company_RecID=A.Company_RecID and v_rpt_Configuration.Config_Type LIKE '%Vendor%' and v_rpt_Configuration.ConfigStatus = 'Active') AS Vendors, 

(Select '$' + CONVERT(varchar(12),(IV_Item.List_Price * Count(v_rpt_Configuration.Config_Name)),1) 
From IV_Item, v_rpt_Configuration 
Where v_rpt_Configuration.Company_RecID=A.Company_RecID and v_rpt_Configuration.Config_Type = 'Managed Workstation' and v_rpt_Configuration.ConfigStatus = 'Active' and IV_Item.Description = 'RMM Managed Workstation' 
Group by IV_Item.List_Price) AS 'Workstation Costs', 

(Select '$' + CONVERT(varchar(12),(IV_Item.List_Price * Count(v_rpt_Configuration.Config_Name)),1) 
From IV_Item, v_rpt_Configuration 
Where v_rpt_Configuration.Company_RecID=A.Company_RecID and v_rpt_Configuration.Config_Type Like '%Vendor%' and v_rpt_Configuration.ConfigStatus = 'Active' and IV_Item.Description = 'Managed Vendor' 
Group by IV_Item.List_Price)AS 'Vendor Costs' 

From AGR_Header 
join Company as A on AGR_Header.Company_RecID=A.Company_RecID 
join Company_Type on A.Company_Type_RecID=Company_Type.Company_Type_RecID 
where Company_Type.Description LIKE '%Client%' and AGR_Date_Cancel is null and AGR_Type_RecID=30 
Order by A.Company_Name 

这里是什么,我试图做一个画面:

[http://imgur.com/A4aUljv][1]

谢谢!

+0

将它作为一个子查询包装,只是将这两个字段相加。如果您不知道子查询是什么,请在此处阅读:http://www.tutorialspoint.com/sql/sql-sub-queries.htm –

回答

0

正如豪尔赫说什么,

添加类似

SELECT src.*, ([WorkStation Costs] + [Vendor Costs]) AS 'Total Costs' 
FROM ( 
    your query here 
) AS src 
0

您可以使用公用表表达式(CTE的)作为替代子查询的性能更好,查询更清晰(容易阅读一个理解)并且具有更好的结构。

WITH <nameyourCTE> AS 
(
<inner query> 
) 
<outer query> 
相关问题