简体   繁体   English

将另一列的值与另一列的值相加

[英]sum another column's values where the another column is distinct

Hi StackOverFlow members 您好StackOverFlow成员

reports = A table name. reports =表名。

Database 数据库

CREATE TABLE `reports` (
  `id` int(11) NOT NULL auto_increment,
  `report_day_name` varchar(20) NOT NULL,
  `report_day` varchar(20) NOT NULL,
  `report_month` varchar(20) NOT NULL,
  `report_year` varchar(20) NOT NULL,
  `report_result_number` varchar(20) NOT NULL,
  `report_result_text` varchar(20) NOT NULL,
  `report_since` varchar(20) NOT NULL,
  `report_date` varchar(20) NOT NULL,
  `catid` int(11) NOT NULL,
  `subjectid` int(11) NOT NULL,
  `userid` int(11) NOT NULL,
  `groupid` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=78 ;

INSERT INTO `reports` VALUES (73, 'day', '14', '1', '1434 h', '5', 'rate', '1234567890', '1434-1-14', 1, 132, 33, 35);
INSERT INTO `reports` VALUES (74, 'day', '12', '2', '1435 h', '4', 'rate', '1234567890', '1434-2-12', 2, 136, 36, 35);
INSERT INTO `reports` VALUES (75, 'day', '14', '1', '1434 h', '2', 'rate', '1354488730', '1434-1-14', 1, 132, 33, 35);
INSERT INTO `reports` VALUES (76, 'day', '12', '2', '1435 h', '4', 'rate', '1354488730', '1434-2-12', 2, 137, 36, 35);
INSERT INTO `reports` VALUES (77, 'day', '12', '2', '1435 h', '1', 'rate', '1354488730', '1434-2-12', 2, 134, 33, 35);

This is the database table: 这是数据库表:

id  report_result_number    subjectid   userid
73  5                       132         33
74  4                       136         36
75  2                       132         33
76  4                       137         36
77  1                       134         33

I want to SUM(reports.report_result_number) where (reports.subjectid) is DISTINCT 我想要SUM(reports.report_result_number) where (reports.subjectid) is DISTINCT

when i run this code.. 当我运行此代码..

SELECT
  users.user_id, users.user_name, users.user_country, SUM(reports.report_result_number) AS AllTotal, COUNT(DISTINCT reports.subjectid) AS TotalSubjects
FROM
  users
  INNER JOIN reports ON users.user_id = reports.userid
GROUP BY
  users.user_id
  ORDER BY
  AllTotal DESC LIMIT 4

it returns AllTotal 它返回AllTotal

user_id user_name   user_country    AllTotal    TotalSubjects
36       name         country        8 (correct)        2
33        name        country        8 (not correct)    2

The question is open to a couple of interpretations. 这个问题可以解释。

If what you want is the value of result_report_number to be included in the SUM aggregate ONLY if there is only ONE row for a given subjectid and userid, (if there is more than one row for the same subjectid, you want to EXCLUDE the report_result_number for all of those rows... 如果你想要的是 result_report_number的值仅包含在SUM聚合中,如果给定的subjectid和userid只有一行,(如果同一个subjectid有多行,你想要清除report_result_number为所有这些行......

Then something like this will work: 然后这样的事情会起作用:

 
 
 
  
  SELECT u.user_id , u.user_name , u.user_country , SUM(s.report_result_number) AS AllTotal , COUNT(DISTINCT r.subjectid) AS TotalSubjects FROM users u JOIN reports r ON r.userid = u.user_id JOIN ( SELECT d.userid , d.subjectid , d.report_result_number FROM reports d GROUP BY d.userid , d.subjectid HAVING COUNT(1) = 1 ) s ON s.userid = r.userid GROUP BY u.user_id ORDER BY AllTotal DESC LIMIT 4
 
  


That's only one (odd) interpretation of the requested result set. 这只是对请求结果集的一种(奇数)解释。 Sample data and an expected result set would go a long ways towards clarifying the specification. 样本数据和预期结果集将大大有助于澄清规范。


For the data you added to your question, this query should be returning, eg 对于您添加到问题中的数据,此查询应该返回,例如

 36 fee fi 8 2 33 foo bar 1 2 

There are two rows with a subjectid value of 132 for user 33, so the report_result_number for those rows is excluded from the SUM. 对于用户33,有两行具有132的子主题值,因此从SUM中排除这些行的report_result_number。 There are two distinct values for subjectid (132 and 134), so we're returning a :distinct: count of 2. subjectid(132和134)有两个不同的值,所以我们返回:distinct:count为2。


If you are asking for the SUM to return a value ONLY if there are no duplicate values for subjectid for a given user... 如果要求SUM仅在给定用户的subjectid没有重复值的情况下返回值...

 
 
 
  
  SELECT u.user_id , u.user_name , u.user_country , IF(COUNT(DISTINCT r.subjectid) = COUNT(r.subjectid) ,SUM(r.report_result_number) ,NULL ) AS AllTotal , COUNT(DISTINCT r.subjectid) AS TotalSubjects FROM users u JOIN reports r ON r.userid = u.user_id GROUP BY u.user_id ORDER BY AllTotal DESC LIMIT 4
 
  

Hasan said... "If there are duplicated values [of subjectid for a given userid], get one of them" Hasan说......“如果[给定用户标识的子主题]存在重复值,请获取其中一个”

Just remove the HAVING clause from the inline view aliased as s . 只需从作为s别名的内联视图中删除HAVING子句。 That will return the value of report_result_number for one row. 这将返回一行的report_result_number的值。 (It will be arbitrary as to which "matching" row the value will be returned from: (关于从哪个“匹配”行返回值将是任意的:

 SELECT u.user_id , u.user_name , u.user_country , SUM(r.report_result_number) AS AllTotal , COUNT(DISTINCT r.subjectid) AS TotalSubjects FROM users u JOIN ( SELECT d.userid , d.subjectid , d.report_result_number FROM reports d GROUP BY d.userid , d.subjectid ) r ON r.userid = u.user_id GROUP BY u.user_id ORDER BY AllTotal DESC LIMIT 4 

To make the resultset repeatable, to always get the lowest or highest value, you could add an aggregate function to specify which value to return. 要使结果集可重复,要始终获得最低或最高值,可以添加聚合函数以指定要返回的值。

replace... 更换...

  , d.report_result_number 

with... 与...

  , MAX(d.report_result_number) AS report_result_number 

With the MAX() aggregate, this will return: 使用MAX()聚合,这将返回:

 36 fee fi 8 2 33 foo bar 6 2 

(The query will get the value of '5' for subjectid=132 userid=33, and will omit the value of '2' for the same subjectid.) Absent the MAX aggregate, the query could validly (and arbitrarily) return a '3' in place of the '6'. (对于subjectid = 132 userid = 33,查询将得到'5'的值,并且对于相同的subjectid将省略'2'的值。)如果没有MAX聚合,查询可以有效地(并且任意地)返回'' 3'代替'6'。 (It can include either the '5' or the '2', and omit the other.) (它可以包括'5'或'2',并省略另一个。)

SQL Fiddle here SQL小提琴在这里

SQL Fiddle including the MAX aggregate here SQL Fiddle包括MAX聚合


Q: how can i use (where report_month = 'number') in your code? 问:我如何在代码中使用(where report_month ='number')?

A: add the WHERE clause in the inline view, after the FROM clause before the GROUP BY clause. 答:在GROUP BY子句之前的FROM子句之后的内联视图中添加WHERE子句。 Replace this: 替换这个:

  FROM reports d GROUP 

with eg 用例如

  FROM reports d WHERE d.report_month = 'number' GROUP 

Only rows that satisfy the specified predicate will be returned. 仅返回满足指定谓词的行。

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

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