简体   繁体   English

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

[英]SQL Query: SUM on three columns with criteria

I have a table with columns like these : 我有一张桌子,上面有这些列:

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

Data is typically like this : 数据通常是这样的:

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

I would like to make a SUM() of every user's amount. 我想对每个用户的数量做一个SUM()。

Example : 范例:

  • user1= 1500*100/100 (amount*usercol1/100) user1 = 1500 * 100/100(数量* usercol1 / 100)
  • user2= 3000*50/100 (amount*usercol1/100) user2 = 3000 * 50/100(数量* usercol1 / 100)
  • user3= 3000*50/100 (amount*usercol2/100) user3 = 3000 * 50/100(数量* usercol2 / 100)

I tried UNION to no avail (did not sum the SUMs). 我尝试使用UNION无济于事(未对总和求和)。

Is there a way to do this ? 有没有办法做到这一点 ? The problem being that it should GROUP BY the username (which I get with a LEFT OUTER JOIN usernames ON exampletable.usercol1=usernames.idx). 问题在于它应该按用户名分组(我在exampletable.usercol1 = usernames.idx上通过左外连接用户名获得)。

I know this is non standard and would be better with relations from another table. 我知道这是非标准的,与其他表格的关系会更好。 But I am not allowed to change the table structure. 但是我不允许更改表结构。

Many many many thanks ! 非常感谢! :=) :=)

Hereby, an example that gives a wrong result (seems to give only results from the query in the middle) 在此,给出一个错误结果的示例(似乎仅在中间给出查询的结果)

( 
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

You could use a derived table to simulate a first normal form table then join onto that. 您可以使用派生表来模拟第一个范式表,然后将其联接到该表上。

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 

using this data (i added some stranger data to make sure the math was working properly) 使用此数据(我添加了一些陌生数据以确保数学正常运行)

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

i got these results 我得到了这些结果

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

is this what you were looking for? 这是您要找的东西吗? below is my query 以下是我的查询

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM