简体   繁体   English

如何将一列的总数与SQL中的行条目相乘,即sum(column_name)*列名

[英]How to multiply total of one column with row entry in SQL ie sum(column_name) * column name

Unable to understnad how to use the formula of multiplying total of colum with entry of row. 无法理解如何使用列总数乘以行条目的公式。

Tried the code below 尝试了下面的代码

SELECT g.Item_No, (sum(g.units)* u.Unit_Percentage) as Units@2018Mix
FROM Global_GM_2019 g
FULL JOIN UnitMix_perc_2018 u 
ON u.item_no=g.item_no
GROUP BY g.item_no;

I get this eror which I know that should include all the columns in group by function. 我得到这个错误,我知道应该包括按功能分组的所有列。

Msg 8120, Level 16, State 1, Line 275
Column 'UnitMix_perc_2018.Unit_Percentage' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Expected 预期

Products    units of 2019   % of units of 2018  product mix
a              110                     34            20570
b              120                     23            13915
c              120                     12            7260
d              130                     12            7260
e              125                     23            13915

Expected result image added here 预期结果图片已添加到此处

Try computing a summary table and then joining (this will probably run fastest as the join will likely be smaller). 尝试计算汇总表,然后再进行合并(这可能会以最快的速度运行,因为合并可能会更小)。

Select item_No, unit_sum * Unit_Percentage as Units@2018Mix
From (
  SELECT item_no, sum(g.units) as unit_sum
  FROM Global_GM_2019
  Group by item_no
) as g
  FULL JOIN UnitMix_perc_2018 u 
  ON u.item_no=g.item_no

Or simply move the calculation into the sum 或者只是将计算移到总和中

SELECT g.Item_No, sum(g.units* u.Unit_Percentage) as Units@2018Mix
FROM Global_GM_2019 g
FULL JOIN UnitMix_perc_2018 u 
ON u.item_no=g.item_no
GROUP BY g.item_no;

Or add the unit percentage scalar to tour group by. 或将单位百分比标量添加到游览分组依据。

SELECT g.Item_No, (sum(g.units)* u.Unit_Percentage) as Units@2018Mix
FROM Global_GM_2019 g
FULL JOIN UnitMix_perc_2018 u 
ON u.item_no=g.item_no
GROUP BY g.item_no, u.unit_percentage:

In reply to the comment stream... @sag, you are sort of cheating - you are asking two questions in one. 在回复评论流... @sag时,您有点作弊-您要同时提出两个问题。 The two questions are: 这两个问题是:

  1. I have a syntax error in my posted query. 我发布的查询中存在语法错误。 (Saying " I know that should include all the columns in group by function" does not cancel this point out. If you know that, you should do that in your orignal post. Refer to create a Minimal, Complete, Verifiable example .) (说“我知道应该包括按功能分组的所有列”并不能消除这一点。如果您知道,则应该在原始文章中进行。请参考创建一个最小,完整,可验证的示例 。)
  2. I'm not going to tell you how it is stored nor how to calculate it, but I want to get some data from two years in my 4 columns of output. 我不会告诉您它的存储方式和计算方法,但是我想从两年的4列输出中获取一些数据。

I have answered question 1 three ways. 我已经通过三种方式回答了问题1。 I can only guess for question 2 (because you provide no details in your question on this subject). 我只能猜测第二个问题(因为您在这个问题上没有提供任何细节)。 Here is my guess. 这是我的猜测。

SELECT g.Item_No,
     sum(g19.units),
     (sum(g18.units)* u.Unit_Percentage) as Units@2018Mix,
     /* Maybe */ (sum(g19.units) (double) / sum(g18.units) * 100.0 as g19_pcnt_increase_over_g18  /*???*/
     /* I have no idea how you intend to calculate "product mix" because you provide zero information on this topic. */
FROM Global_GM_2019 g JOIN Global_gm_2018 as G18 on
       g.item_id = g18.item_id
FULL JOIN UnitMix_perc_2018 u 
ON u.item_no=g.item_no
GROUP BY g.item_no, u.unit_percentage:

We can only provide answers to the question that you have asked for which you provide some supporting information . 我们只能为您提出的问题提供答案,并提供一些支持信息 Again, please refer to how to create a Minimal, Complete, Verifiable example . 同样,请参考如何创建最小,完整,可验证的示例

I doubt that you really want a FULL JOIN , because you will get rows that are NULL . 我怀疑您是否真的想要FULL JOIN ,因为您将获得NULL行。

Based on your description, I think you want: 根据您的描述,我认为您想要:

SELECT g.Item_No, SUM(g.units * u.Unit_Percentage) as Units_2018Mix
FROM Global_GM_2019 g LEFT JOIN
     UnitMix_perc_2018 u 
     ON u.item_no = g.item_no
GROUP BY g.item_no;

In other words, do the multiplication before doing the SUM() . 换句话说, SUM() 之前先做乘法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM