2013-12-16 76 views
0

我有这三个语句:如何将多个select语句组合到不同的列中?

select count (distinct vid) as SUM_OF_SEC from pdalis where pid in(500,504); 
select sum (amount*paidperitem) SUM_OF_A from pdalis where pid = 501 ; 
select sum(amount * paidperitem) SUM_OF_P from pdalis where pid IN (500,504); 

如何合并所有的人分成3并排列?

回答

1

子查询试试这个

select count (distinct vid) as SUM_OF_SEC ,(select sum (amount*paidperitem) from pdalis where pid = 501 ;) AS SUM_OF_A , (select sum(amount * paidperitem) from pdalis where pid IN (500,504);) as SUM_OF_P from pdalis where pid in(500,504); 
+0

谢谢,这个解决方案似乎是最简单的一个,但结果只显示一个标题SUM_OF_SEC第一列,其他列有“列??”在他们之上,即使我加了'as'。 – user3106585

0

请尝试这样。

SELECT TMP1.SUM_OF_SEC, TMP2.SUM_OF_A, TMP3.SUM_OF_P FROM 
(select count (distinct vid) as SUM_OF_SEC from pdalis where pid in(500,504)) AS TMP1, 
(select sum (amount*paidperitem) as SUM_OF_A from pdalis where pid = 501) AS TMP2, 
(select sum(amount * paidperitem) as SUM_OF_P from pdalis where pid IN (500,504)) AS TMP3 
0
select count (distinct (Case when pid!=501 then vid End)) as SUM_OF_SEC , 
sum (Case when pid=501 then (amount*paidperitem) Else 0 End) as SUM_OF_A , 
sum(Case when pid!=501 then (amount * paidperitem) Else 0 End)as SUM_OF_P from 
pdalis where pid IN (500,504,501); 
0
SELECT CellPhone.PhoneNumber, SUM(BillingHistory.AmountOwed) AS TotalOwed 
FROM Relationship 
INNER JOIN CellPhone ON CellPhone.PKCellPhone = Relationship.FKCellPhone 
INNER JOIN BillingHistory ON Relationship.PKRelationship = BillingHistory.FKRelationship 
GROUP BY Relationship.PKRelationship, CellPhone.PhoneNumber 

More Information You Can Go On 
http://social.msdn.microsoft.com/Forums/en-US/1b035516-440d-47d6-ae75-cdb8926f1138/combine-columns-from-two-select-statements?forum=transactsql