SELECT [UserID],[Name],[RegisterDate]
,((select sum([Score]) as sum1 from [UserInfo] where [UserID]=t.[UserID])
+(select sum([Price]*[Count])  as sum2 FROM [Property] where [UserID]=t.[UserID])
+(select sum(PPrice*PCount) as sum3 FROM [Present] where [UserID] =t.[UserID]))
 AS [3句的和]
FROM  [Acinfo] t (NOLOCK)
WHERE t.[UserID]

解决方案 »

  1.   


    SELECT [UserID],[Name],[RegisterDate]
    ,((select sum([Score]) as sum1 from [UserInfo] where [UserID]=t.[UserID])
    +(select sum([Price]*[Count])  as sum2 FROM [Property] where [UserID]=t.[UserID])
    +(select sum(PPrice*PCount) as sum3 FROM [Present] where [UserID] =t.[UserID]))
     AS [3句的和]
    FROM  [Acinfo] t (NOLOCK)
    WHERE t.[UserID]=2
      

  2.   


    SELECT [UserID],[Name],[RegisterDate],sum([Score]) as sum1
          , sum([Price]*[Count])  as sum2
          ,sum(PPrice*PCount) as sum3
    FROM  [Acinfo]  a
           JOIN [UserInfo] u ON a.UserID =  u.UserID
           JOIN Property p ON a.UserID =  p.UserID
           JOIN  [Present]  t ON a.UserID =  t.UserID
    GROUP BY [UserID],[Name],[RegisterDate]
      

  3.   

    因为3句sum 可能有null值或者正负值   把null值默认为 0 ,3句的绝对值相加该怎么做呢?
      

  4.   

    SELECT [UserID],[Name],[RegisterDate],(abs(select sum([Score]) as sum1 from [UserInfo] where [UserID]=t.[UserID])+abs(select sum([Price]*[Count])  as sum2 FROM [Property] where [UserID]=t.[UserID])+abs(select sum(PPrice*PCount) as sum3 FROM [Present] where [UserID] =t.[UserID])) AS [3句的和]FROM  [Acinfo] t (NOLOCK)WHERE t.[UserID]=2
      

  5.   


    --用isnull
    SELECT [UserID],[Name],[RegisterDate]
    ,((select isnull(sum([Score]),0) as sum1 from [UserInfo] where [UserID]=t.[UserID])
    +(select isnull(sum([Price]*[Count]),0)  as sum2 FROM [Property] where [UserID]=t.[UserID])
    +(select isnull(sum(PPrice*PCount),0) as sum3 FROM [Present] where [UserID] =t.[UserID]))
     AS [3句的和]
    FROM  [Acinfo] t (NOLOCK)
    WHERE t.[UserID]=2
      

  6.   

    嗯 这样是对了,第一个select sum  的  null情况默认为0了,那第一个select  sum的值是大于0,也默认为0 该怎么写呢