如何计算任意数量的 COUNTS 的总和

发布于 2024-12-25 08:26:16 字数 1083 浏览 0 评论 0原文

我想知道是否可以在一个 SQL 查询中将每张照片的分数乘以其分数类型系数的所有乘积求和?

示例:

t = total : my aim, the total score for each photo
c_foo = count score type foo = the amount of scores for each photo with the score type name = foo 
c_bar = count score type bar = the amount of scores for each photo with the score type name = bar
m_foo = foo factor
m_bar = bar factor

不同分数类型的数量是任意的。

t = c_foo * m_foo + c_bar * m_bar + … + c_last * m_last

我最初的想法是使用以下表结构:

所以到目前为止,我有以下查询:

SELECT p.id, st.name, st.factor, COUNT(*) AS count
FROM s2p_photo p
LEFT JOIN s2p_score s 
LEFT JOIN s2p_score_type st 
GROUP BY p.id, st.name
ORDER BY p.id ASC 

我收到照片的名称、系数和总和,但我无法进行数学计算。

我不知道是否UNION 这里是可行的。

我的表结构没问题吗?你们有人知道吗?

PS:抱歉,我无法发布图片;请在浏览器中手动打开它们:(

I'm wondering if it is possible to totalize all products of the amount of scores for each photo multiplied by their score type factor in one SQL query?

Example:

t = total : my aim, the total score for each photo
c_foo = count score type foo = the amount of scores for each photo with the score type name = foo 
c_bar = count score type bar = the amount of scores for each photo with the score type name = bar
m_foo = foo factor
m_bar = bar factor

The number of different score types is arbitrary.

t = c_foo * m_foo + c_bar * m_bar + … + c_last * m_last

My initial idea is to use the following table structure:

So far I have the following query:

SELECT p.id, st.name, st.factor, COUNT(*) AS count
FROM s2p_photo p
LEFT JOIN s2p_score s 
LEFT JOIN s2p_score_type st 
GROUP BY p.id, st.name
ORDER BY p.id ASC 

I receive the name, factor and sum of photos but I'm not able to do the math.

I have no idea if UNION is feasible here.

Is my table structure ok? Does anyone of you has a clue?

PS: Sorry I'm not able to post images; please open them manually in your browser :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不甘平庸 2025-01-01 08:26:16

不确定我是否明白你的意思,但这就是你要找的吗?

SELECT
  id,
  SUM(factor*count) AS totalscore
FROM (
    SELECT
      p.id AS id,
      st.factor AS factor,
      COUNT(*) AS count 
    FROM
      s2p_photo p 
      LEFT JOIN s2p_score s 
      LEFT JOIN s2p_score_type st 
    GROUP BY p.id, st.name
    ORDER BY p.id ASC
) AS baseview
GROUP BY id

Not sure whether I understand what you mean, but is this what you are looking for?

SELECT
  id,
  SUM(factor*count) AS totalscore
FROM (
    SELECT
      p.id AS id,
      st.factor AS factor,
      COUNT(*) AS count 
    FROM
      s2p_photo p 
      LEFT JOIN s2p_score s 
      LEFT JOIN s2p_score_type st 
    GROUP BY p.id, st.name
    ORDER BY p.id ASC
) AS baseview
GROUP BY id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文