[英]Sum sql values based on distinct values from another column and put the result in array
[英]sum another column's values where the another column is distinct
您好StackOverFlow成员
reports =表名。
数据库
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);
这是数据库表:
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
我想要SUM(reports.report_result_number) where (reports.subjectid) is DISTINCT
当我运行此代码..
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
它返回AllTotal
user_id user_name user_country AllTotal TotalSubjects
36 name country 8 (correct) 2
33 name country 8 (not correct) 2
这个问题可以解释。
如果你想要的是
result_report_number
的值仅包含在SUM聚合中,如果给定的subjectid和userid只有一行,(如果同一个subjectid有多行,你想要清除report_result_number为所有这些行......
然后这样的事情会起作用:
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
这只是对请求结果集的一种(奇数)解释。 样本数据和预期结果集将大大有助于澄清规范。
对于您添加到问题中的数据,此查询应该返回,例如
36 fee fi 8 2 33 foo bar 1 2
对于用户33,有两行具有132的子主题值,因此从SUM中排除这些行的report_result_number。 subjectid(132和134)有两个不同的值,所以我们返回:distinct:count为2。
如果要求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说......“如果[给定用户标识的子主题]存在重复值,请获取其中一个”
只需从作为s
别名的内联视图中删除HAVING子句。 这将返回一行的report_result_number的值。 (关于从哪个“匹配”行返回值将是任意的:
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
要使结果集可重复,要始终获得最低或最高值,可以添加聚合函数以指定要返回的值。
更换...
, d.report_result_number
与...
, MAX(d.report_result_number) AS report_result_number
使用MAX()聚合,这将返回:
36 fee fi 8 2 33 foo bar 6 2
(对于subjectid = 132 userid = 33,查询将得到'5'的值,并且对于相同的subjectid将省略'2'的值。)如果没有MAX聚合,查询可以有效地(并且任意地)返回'' 3'代替'6'。 (它可以包括'5'或'2',并省略另一个。)
问:我如何在代码中使用(where report_month ='number')?
答:在GROUP BY子句之前的FROM子句之后的内联视图中添加WHERE子句。 替换这个:
FROM reports d GROUP
用例如
FROM reports d WHERE d.report_month = 'number' GROUP
仅返回满足指定谓词的行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.