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. The two questions are:
I have answered question 1 three ways. 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
.
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()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.