sql server group by,count,compute

时间:2011-07-06 15:41:31

标签: sql sql-server tsql aggregate-functions

查询:

 SELECT purchased_profileid, 
        COUNT(distinct(purchased_profileid)) as 'NoofPurchases', 
        final_paidprice  
   FROM MMBMembership mmb, 
        MMB_BusinessProfiles mmbProfiles, 
        basic_details bd, 
        orderdetails_purchasedProfiles odpp
   WHERE mmb.mmb_id = mmbProfiles.MMB_id
     AND mmb.Profile_id = LTRIM(bd.profile_id)
     AND odpp.purchased_profileid = bd.profile_id
     AND mmb.mmb_id = 1
     AND ispublished = 'true'
GROUP BY final_paidprice, purchased_profileid

结果:

purchased_profileid  NoofPurchases  final_paidprice
------------------------------------------------------
10                   1              314.828012285208
10                   1              788.635686407162
11                   1              926.007854252126
11                   1              1000
11                   1              1385.59720636606

我应该如何修改此查询以将输出作为

purchased_profileid  NoofPurchases  final_paidprice
----------------------------------------------------
10                   2              1103.462
11                   3              3311.597

1 个答案:

答案 0 :(得分:3)

select purchased_profileid, count(*) as 'NoofPurchases', sum(final_paidprice)  from MMBMembership mmb, MMB_BusinessProfiles mmbProfiles, basic_details bd, orderdetails_purchasedProfiles odpp
where mmb.mmb_id = mmbProfiles.MMB_id
and mmb.Profile_id = ltrim(bd.profile_id)
and odpp.purchased_profileid = bd.profile_id
and mmb.mmb_id = 1
and ispublished='true'
group by  purchased_profileid