[英]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.