SQL查询:带有条件的三列上的SUM

时间:2010-09-14 16:44:52

标签: sql mysql sum

我有一个包含这些列的表格:

idx | amount | usercol1 | usercol2 | usercol3 | percentage1 | percentage2 | percentage3

数据通常是这样的:

0   | 1500   | 1        | null     | null     | 100         | null        | null
1   | 3000   | 2        | 3        | null     | 50          | 50          | null

我想为每个用户的金额制作一个SUM()。

示例:

  • user1 = 1500 * 100/100(金额* usercol1 / 100)
  • user2 = 3000 * 50/100(金额* usercol1 / 100)
  • user3 = 3000 * 50/100(金额* usercol2 / 100)

我试过UNION无济于事(没有总结SUM)。

有办法做到这一点吗?问题在于它应该是GROUP BY用户名(我使用LEFT OUTER JOIN用户名ON exampletable.usercol1 = usernames.idx)。

我知道这是非标准的,对于另一张桌子的关系会更好。但我不允许改变表结构。

非常感谢! :=)

因此,一个给出错误结果的例子(似乎只给出了中间查询的结果)

( 
SELECT SUM(projects.amount * (projects.percentage1/100)) as totalproj, 
entities.idx as idx, 
COUNT(projects.idx) as numproj, 
entities.name 
 FROM projects 
 INNER JOIN entities ON projects.usercol1=entities.idx 
 WHERE projects.usercol1=entities.idx 
GROUP BY name ORDER BY totalproj DESC 
) 
UNION ALL
( 
SELECT SUM(projects.amount * (projects.percentage2/100)) as totalproj, 
entities.idx as idx, 
COUNT(projects.idx) as numproj, 
entities.name 
 FROM projects 
 INNER JOIN entities ON projects.usercol2=entities.idx 
 WHERE projects.usercol2=entities.idx 
GROUP BY name ORDER BY totalproj DESC 
) 
UNION ALL
( 
SELECT SUM(projects.amount * (projects.percentage3/100)) as totalproj, 
entities.idx as idx, 
COUNT(projects.idx) as numproj, 
entities.name 
 FROM projects 
 INNER JOIN entities ON projects.usercol3=entities.idx 
 WHERE projects.usercol3=entities.idx 
GROUP BY name ORDER BY totalproj DESC 
)
ORDER BY totalproj DESC 
LIMIT 10

2 个答案:

答案 0 :(得分:3)

您可以使用派生表来模拟第一个普通表格表,然后加入到该表格中。

SELECT SUM(P.amount * (P.percentage/100)) as totalproj, 
       entities.idx as idx, 
       COUNT(P.idx) as numproj, 
       entities.name 
FROM 
(
SELECT idx, amount, usercol1 AS usercol, percentage1 AS percentage
FROM projects
UNION ALL
SELECT idx, amount, usercol2 AS usercol, percentage2 AS percentage
FROM projects
UNION ALL
SELECT idx, amount, usercol3 AS usercol, percentage3 AS percentage
FROM projects
) P
 INNER JOIN entities ON P.usercol=entities.idx 
 WHERE P.usercol=entities.idx 
 GROUP BY name 
 ORDER BY totalproj DESC 

答案 1 :(得分:1)

使用这些数据(我添加了一些陌生人数据以确保数学运算正常)

0   1500    1   NULL    NULL    100     NULL    NULL
1   3000    2   3       NULL    50      50      NULL
2   780     4   1       3       70      20      50
3   3800    2   4       1       30      20      10

我得到了这些结果

  user  commission
-------  -------------
    1     2036
    2     2640
    3     1890
    4     1306

这就是你要找的东西吗?以下是我的查询

SELECT  [user]
       ,SUM([commission]) AS commission
FROM    ( SELECT    [usercol1] AS [user]
                   ,( [amount] * [percentage1] ) / 100 AS commission
          FROM      [dbo].[projects]
          WHERE     [usercol1] IS NOT NULL
                    AND [percentage1] IS NOT NULL
          UNION ALL
          SELECT    [usercol2]
                   ,( [amount] * [percentage2] ) / 100
          FROM      [dbo].[projects]
          WHERE     [usercol2] IS NOT NULL
                    AND [percentage2] IS NOT NULL
          UNION ALL
          SELECT    [usercol3]
                   ,( [amount] * [percentage3] ) / 100
          FROM      [dbo].[projects]
          WHERE     [usercol3] IS NOT NULL
                    AND [percentage3] IS NOT NULL
        ) x
GROUP BY [user]