jjzjj

MySQL Statement (Group and SUM() => getting multiple "sum"result per line

coder 2023-10-23 原文

我有几个表:

table user:
id  firstname   lastname
1   John      Doe
2   Jane      Skith
3   will      Smith
...

table member:
member_id    member_user_id member_status member_activated_by
10         1              yes           partner 1
11         1              yes           partner 2
12         1              yes           partner 3
13         2              yes           partner 2
14         3              no            ----
...

table points:
points_id    points_user_id points_value points_date
10         1              10           2012-02-15 
11         2              15           2012-02-15 
12         2              20           2012-02-15 
13         1              5            2012-02-15 
14         3              30           2012-02-15 
15         1              12           2012-02-15 
...

So results SHOULD be:
 id1 - John Doe:   27 Points
 id2 - Jane Skith: 35 Points
 id3 - will Smith: nothing (not activated)

问题是,id1 带来了 81 点(因为他已经被激活了 3 次......)

这是我当前的 mysql 字符串(缩短...)

SELECT points_user_id, SUM( points_value ) AS points_total, id, firstname, lastname
FROM user
JOIN member ON member.member_user_id = user.id
JOIN points ON points.points_user_id = user.id
WHERE 1
AND member_status = 'yes'
GROUP BY points_user_id
ORDER BY points_total DESC 
LIMIT 0 , 100

.. 到目前为止,它“几乎”按预期工作。

但是:用户可能有几个合作伙伴将他激活“进入激活”表。

现在调用它时,我得到了 points_total 的重复(它将被计算多次,因为 uesr 已被合作伙伴激活......)这在排名列表中没有多大意义......

如何为每个 user_id 获得“一个”points_total

希望你明白,我想要达到的目标......非常感谢!

最佳答案

我认为这应该可行,而不是加入激活进行子选择,您将在其中每个 user_id 只获得一行然后加入这个子选择,我不确定这是否是 MySql 语法,但它应该向您展示方法。唯一要注意的是,您在内部查询中选择的字段对于相同的 user_id 可能不会更改

SELECT user_id, SUM( point_row ) AS points_total, user_data...,...
    FROM points
    JOIN user_data AS UD
    ON UD.user_id = points.user_id

JOIN ( SELECT field1,field2,field3, MAX(activation.user_id) as user_id 
  FROM activation WHERE activation_info = 'yes'
  GROUP BY field1,field2,field3 AS ACTIV )  AS ACTIV
  ON user_data.user_id = AS ACTIV.user_id

WHERE points.points_status = '1'
AND   ACTIV.activation_info = 'yes'
GROUP BY points.user_id
ORDER BY total DESC 
LIMIT 0 , 100

关于MySQL Statement (Group and SUM() => getting multiple "sum"result per line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11615595/

有关MySQL Statement (Group and SUM() => getting multiple "sum"result per line的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  4. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  5. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  6. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  7. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  8. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  9. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  10. ruby-on-rails - Nokogiri:使用 XPath 搜索 <div> - 2

    我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll

随机推荐