updating transactions with preaggregated dataSolutions for INSERT OR UPDATE on SQL ServerSQL update from one Table to another based on a ID matchSQL update query using joinsHow can I do an UPDATE statement with JOIN in SQL?Update a table using JOIN in SQL Server?How do I UPDATE from a SELECT in SQL Server?What are the options for storing hierarchical data in a relational database?Insert into a MySQL table or update if existsSelecting rows based value in sql server 2008Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, or when the subquery is used as an expression

What is the source of the fear in the Hallow spell's extra Fear effect?

Mute single speaker?

Shoes for commuting

Looking for a big fantasy novel about scholarly monks that sort of worship math?

Was the lunar landing site always in the same plane as the CM's orbit?

Why does 8 bit truecolor use only 2 bits for blue?

Would you recommend a keyboard for beginners with or without lights in keys for learning?

In High Performance Liquid Chromatography, why are ratios of solvents used?

Are language and thought the same?

What's the point of a PROTOTYPE macro that merely expands to its arguments?

Why there is no wireless switch?

Ceiling fan electrical box missing female screw holes

Golfball Dimples on spaceships (and planes)?

How to create large inductors (1H) for audio use?

Do 643,000 Americans go bankrupt every year due to medical bills?

Does the Commodore CDTV-CR contain a 65C02 for some reason?

Matlab fmincon for a problem with many nonlinear constraints

What is the DC of the Tempest Cleric's Wrath of the Storm feature?

Remaining in the US beyond VWP admission period

How do German speakers decide what should be on the left side of the verb?

How does the UK House of Commons think they can prolong the deadline of Brexit?

Round away from zero

Infinitely many Primes

Is there any difference between these two sentences? (Adverbs)



updating transactions with preaggregated data


Solutions for INSERT OR UPDATE on SQL ServerSQL update from one Table to another based on a ID matchSQL update query using joinsHow can I do an UPDATE statement with JOIN in SQL?Update a table using JOIN in SQL Server?How do I UPDATE from a SELECT in SQL Server?What are the options for storing hierarchical data in a relational database?Insert into a MySQL table or update if existsSelecting rows based value in sql server 2008Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, or when the subquery is used as an expression






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have a transactions table. I need to update the table with a pre-aggregated value from another table and then roll these down to varying levels of granularity.



However, the final output is incorrect. Hoping someone here can help me figure out how to go about this.



Table A:

TransID BankName Location Region SaleType MonthlyPayment Weight

1 BOA Boston East F 3000 3
2 Mellon Pittsburgh East C 1000 3
3 BOA Boston East C 2000 2
4 BOA Boston East 1000 2


Table B

BanKname Location Region Sales
BOA Boston East 500
Mellon Pittsburgh East 1000


Desired Output structure

BankName Location Region SaleType AvgSales AvgMonthlyPayment


Issue is that when updating and doing the weighted average, each of the Boston transactions is getting the 500 sales. When added, total sales should be 1500 but is now 2500.



If I update Table A with Sales value from Table B, sales gets repeated for each saletype - so it throws off the final average sales.



 update is this: (Added new column sales in A)

update a
set a.sales = b.sales
from tableA a join tableB b on a.bankname=b.bankname and
a.location=b.location and a.region = b.region


weighted average from A is calculated like this:

select bankname,location,region,saletype,
sum(case when sales is not null then sales*weight else 0
end)/sum(weight) as avgsales
, sum(case when monthlypayment is not null then monthlypayment*weight
else 0 end)/sum(weight) as avgmonthlyp
from tableA
group by bankname,location,region,saletype


For each saletype, the sales is updated with value thus increasing the final value by that fold.



How can I update sales so that BOA only gets 500 and Mellon only gets 1000 and the total # sales is at 1500?










share|improve this question


























  • I honestly don't understand what is your problem.

    – Ajan Balakumaran
    Mar 28 at 4:55











  • Problem is: if I update table A with the sales from Table B, each row in Table A gets the sales due to different saletypes - thus throwing off the final sales value when its weighted average is calculated. So question is how do I update the txns table with this aggregated value without losing the integrity of the data

    – Lucky
    Mar 28 at 5:02











  • You need to get the sales for the different sales types and it seems like you can't move further without that since your weighted average requires to multiply sales by weight and weight differs with sales type.

    – Ajan Balakumaran
    Mar 28 at 5:20











  • And thats where the problem is, I dont have sales by different sale types - i only have aggregated sales. Hence the question here.

    – Lucky
    Mar 28 at 5:53






  • 1





    Your join is causing duplication so it is probably not a valid join

    – wavery
    Mar 28 at 6:14

















0















I have a transactions table. I need to update the table with a pre-aggregated value from another table and then roll these down to varying levels of granularity.



However, the final output is incorrect. Hoping someone here can help me figure out how to go about this.



Table A:

TransID BankName Location Region SaleType MonthlyPayment Weight

1 BOA Boston East F 3000 3
2 Mellon Pittsburgh East C 1000 3
3 BOA Boston East C 2000 2
4 BOA Boston East 1000 2


Table B

BanKname Location Region Sales
BOA Boston East 500
Mellon Pittsburgh East 1000


Desired Output structure

BankName Location Region SaleType AvgSales AvgMonthlyPayment


Issue is that when updating and doing the weighted average, each of the Boston transactions is getting the 500 sales. When added, total sales should be 1500 but is now 2500.



If I update Table A with Sales value from Table B, sales gets repeated for each saletype - so it throws off the final average sales.



 update is this: (Added new column sales in A)

update a
set a.sales = b.sales
from tableA a join tableB b on a.bankname=b.bankname and
a.location=b.location and a.region = b.region


weighted average from A is calculated like this:

select bankname,location,region,saletype,
sum(case when sales is not null then sales*weight else 0
end)/sum(weight) as avgsales
, sum(case when monthlypayment is not null then monthlypayment*weight
else 0 end)/sum(weight) as avgmonthlyp
from tableA
group by bankname,location,region,saletype


For each saletype, the sales is updated with value thus increasing the final value by that fold.



How can I update sales so that BOA only gets 500 and Mellon only gets 1000 and the total # sales is at 1500?










share|improve this question


























  • I honestly don't understand what is your problem.

    – Ajan Balakumaran
    Mar 28 at 4:55











  • Problem is: if I update table A with the sales from Table B, each row in Table A gets the sales due to different saletypes - thus throwing off the final sales value when its weighted average is calculated. So question is how do I update the txns table with this aggregated value without losing the integrity of the data

    – Lucky
    Mar 28 at 5:02











  • You need to get the sales for the different sales types and it seems like you can't move further without that since your weighted average requires to multiply sales by weight and weight differs with sales type.

    – Ajan Balakumaran
    Mar 28 at 5:20











  • And thats where the problem is, I dont have sales by different sale types - i only have aggregated sales. Hence the question here.

    – Lucky
    Mar 28 at 5:53






  • 1





    Your join is causing duplication so it is probably not a valid join

    – wavery
    Mar 28 at 6:14













0












0








0








I have a transactions table. I need to update the table with a pre-aggregated value from another table and then roll these down to varying levels of granularity.



However, the final output is incorrect. Hoping someone here can help me figure out how to go about this.



Table A:

TransID BankName Location Region SaleType MonthlyPayment Weight

1 BOA Boston East F 3000 3
2 Mellon Pittsburgh East C 1000 3
3 BOA Boston East C 2000 2
4 BOA Boston East 1000 2


Table B

BanKname Location Region Sales
BOA Boston East 500
Mellon Pittsburgh East 1000


Desired Output structure

BankName Location Region SaleType AvgSales AvgMonthlyPayment


Issue is that when updating and doing the weighted average, each of the Boston transactions is getting the 500 sales. When added, total sales should be 1500 but is now 2500.



If I update Table A with Sales value from Table B, sales gets repeated for each saletype - so it throws off the final average sales.



 update is this: (Added new column sales in A)

update a
set a.sales = b.sales
from tableA a join tableB b on a.bankname=b.bankname and
a.location=b.location and a.region = b.region


weighted average from A is calculated like this:

select bankname,location,region,saletype,
sum(case when sales is not null then sales*weight else 0
end)/sum(weight) as avgsales
, sum(case when monthlypayment is not null then monthlypayment*weight
else 0 end)/sum(weight) as avgmonthlyp
from tableA
group by bankname,location,region,saletype


For each saletype, the sales is updated with value thus increasing the final value by that fold.



How can I update sales so that BOA only gets 500 and Mellon only gets 1000 and the total # sales is at 1500?










share|improve this question
















I have a transactions table. I need to update the table with a pre-aggregated value from another table and then roll these down to varying levels of granularity.



However, the final output is incorrect. Hoping someone here can help me figure out how to go about this.



Table A:

TransID BankName Location Region SaleType MonthlyPayment Weight

1 BOA Boston East F 3000 3
2 Mellon Pittsburgh East C 1000 3
3 BOA Boston East C 2000 2
4 BOA Boston East 1000 2


Table B

BanKname Location Region Sales
BOA Boston East 500
Mellon Pittsburgh East 1000


Desired Output structure

BankName Location Region SaleType AvgSales AvgMonthlyPayment


Issue is that when updating and doing the weighted average, each of the Boston transactions is getting the 500 sales. When added, total sales should be 1500 but is now 2500.



If I update Table A with Sales value from Table B, sales gets repeated for each saletype - so it throws off the final average sales.



 update is this: (Added new column sales in A)

update a
set a.sales = b.sales
from tableA a join tableB b on a.bankname=b.bankname and
a.location=b.location and a.region = b.region


weighted average from A is calculated like this:

select bankname,location,region,saletype,
sum(case when sales is not null then sales*weight else 0
end)/sum(weight) as avgsales
, sum(case when monthlypayment is not null then monthlypayment*weight
else 0 end)/sum(weight) as avgmonthlyp
from tableA
group by bankname,location,region,saletype


For each saletype, the sales is updated with value thus increasing the final value by that fold.



How can I update sales so that BOA only gets 500 and Mellon only gets 1000 and the total # sales is at 1500?







sql sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 5:00







Lucky

















asked Mar 28 at 4:48









LuckyLucky

617 bronze badges




617 bronze badges















  • I honestly don't understand what is your problem.

    – Ajan Balakumaran
    Mar 28 at 4:55











  • Problem is: if I update table A with the sales from Table B, each row in Table A gets the sales due to different saletypes - thus throwing off the final sales value when its weighted average is calculated. So question is how do I update the txns table with this aggregated value without losing the integrity of the data

    – Lucky
    Mar 28 at 5:02











  • You need to get the sales for the different sales types and it seems like you can't move further without that since your weighted average requires to multiply sales by weight and weight differs with sales type.

    – Ajan Balakumaran
    Mar 28 at 5:20











  • And thats where the problem is, I dont have sales by different sale types - i only have aggregated sales. Hence the question here.

    – Lucky
    Mar 28 at 5:53






  • 1





    Your join is causing duplication so it is probably not a valid join

    – wavery
    Mar 28 at 6:14

















  • I honestly don't understand what is your problem.

    – Ajan Balakumaran
    Mar 28 at 4:55











  • Problem is: if I update table A with the sales from Table B, each row in Table A gets the sales due to different saletypes - thus throwing off the final sales value when its weighted average is calculated. So question is how do I update the txns table with this aggregated value without losing the integrity of the data

    – Lucky
    Mar 28 at 5:02











  • You need to get the sales for the different sales types and it seems like you can't move further without that since your weighted average requires to multiply sales by weight and weight differs with sales type.

    – Ajan Balakumaran
    Mar 28 at 5:20











  • And thats where the problem is, I dont have sales by different sale types - i only have aggregated sales. Hence the question here.

    – Lucky
    Mar 28 at 5:53






  • 1





    Your join is causing duplication so it is probably not a valid join

    – wavery
    Mar 28 at 6:14
















I honestly don't understand what is your problem.

– Ajan Balakumaran
Mar 28 at 4:55





I honestly don't understand what is your problem.

– Ajan Balakumaran
Mar 28 at 4:55













Problem is: if I update table A with the sales from Table B, each row in Table A gets the sales due to different saletypes - thus throwing off the final sales value when its weighted average is calculated. So question is how do I update the txns table with this aggregated value without losing the integrity of the data

– Lucky
Mar 28 at 5:02





Problem is: if I update table A with the sales from Table B, each row in Table A gets the sales due to different saletypes - thus throwing off the final sales value when its weighted average is calculated. So question is how do I update the txns table with this aggregated value without losing the integrity of the data

– Lucky
Mar 28 at 5:02













You need to get the sales for the different sales types and it seems like you can't move further without that since your weighted average requires to multiply sales by weight and weight differs with sales type.

– Ajan Balakumaran
Mar 28 at 5:20





You need to get the sales for the different sales types and it seems like you can't move further without that since your weighted average requires to multiply sales by weight and weight differs with sales type.

– Ajan Balakumaran
Mar 28 at 5:20













And thats where the problem is, I dont have sales by different sale types - i only have aggregated sales. Hence the question here.

– Lucky
Mar 28 at 5:53





And thats where the problem is, I dont have sales by different sale types - i only have aggregated sales. Hence the question here.

– Lucky
Mar 28 at 5:53




1




1





Your join is causing duplication so it is probably not a valid join

– wavery
Mar 28 at 6:14





Your join is causing duplication so it is probably not a valid join

– wavery
Mar 28 at 6:14












0






active

oldest

votes










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55390346%2fupdating-transactions-with-preaggregated-data%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes




Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55390346%2fupdating-transactions-with-preaggregated-data%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해