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

时间:2012-12-12 21:46:45

标签: php mysql sum 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

1 个答案:

答案 0 :(得分: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的子主题值,因此这些行的report_result_number将从SUM中排除。 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

<击>

哈桑说......“如果[给定用户标识的子主题]有重复的值,那就得到其中一个”

从名为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',并省略另一个。)

SQL Fiddle here

SQL Fiddle including the MAX aggregate here


问:如何在代码中使用(where report_month ='number')?

A:在GROUP BY子句之前的FROM子句之后的内联视图中添加WHERE子句。替换这个:

       FROM reports d
      GROUP

与例如

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

只返回满足指定谓词的行。