如何在单独的列中组合多个select语句?

时间:2013-12-16 08:29:57

标签: sql join

我有这三个陈述:

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个并排列?

4 个答案:

答案 0 :(得分:1)

试试这样。

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

答案 1 :(得分: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);

答案 2 :(得分: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);

答案 3 :(得分: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