Solved

SQL 2008 Add numbers in a column

Posted on 2011-09-23
10
306 Views
Last Modified: 2012-08-14
I have the following:

a database called coop_2011
a table called plantname_usa_inc.
a column called contract_information_total_weight
a column called contract_information_contract
a column called contract_information_weighted_Avg

a view called plant_2011
In this view I filter out several parts of the column called contract_information_contract to select only select contracts.

I now need to be able to total the weightes in the column contract_information_weight for the remaining contracts that have not been filtered out.

After that is completed I need to take the column contract_information_total_weight/the total recieved by adding all the weights still remaining and dump that data in the column contract_information_weighted_Avg

Any thougts??

Thanks!!
0
Comment
Question by:HDM
  • 4
  • 3
  • 3
10 Comments
 
LVL 6

Assisted Solution

by:MuffyBunny
MuffyBunny earned 100 total points
ID: 36587504
SELECT SUM(contract_information_total_weight), contract_information_contract, contract_information_weighted_Avg
FROM plantname_usa_inc
GROUP BY contract_information_contract, contract_information_weighted_Avg

Open in new window

0
 
LVL 42

Accepted Solution

by:
dqmq earned 400 total points
ID: 36587607
SELECT SUM(contract_information_total_weight), contract_information_contract, AVG(contract_information_total_weight)
FROM plantname_usa_inc
GROUP BY contract_information_contract


0
 

Author Comment

by:HDM
ID: 36588760
I know how I can use this to get the data I want, but how would I implement this into a view so it can run automatically??
0
 
LVL 6

Expert Comment

by:MuffyBunny
ID: 36589355
A view is essentially just a query. Write a query that returns what you want then create a view, copy & paste your query, save
0
 
LVL 42

Expert Comment

by:dqmq
ID: 36589451
create the view without filters:


create view plant_name_summary
as
SELECT SUM(contract_information_total_weight), contract_information_contract, AVG(contract_information_total_weight)
FROM plantname_usa_inc
GROUP BY contract_information_contract
go


Then use the view with filters:
Select * from plant_name_summary where contract_information_contract in (1234, 6789)

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 6

Expert Comment

by:MuffyBunny
ID: 36589643
Yeah, so what I said
0
 

Author Comment

by:HDM
ID: 36589807
I think we almost have this but not quite, or Im not doing things right.

What I need is a single number that is the sum of the column contract_information_total weight

Then I need each line item in the column contract_information_total_weight divided into the total and the output answer stored in the column contract_information_weighted_average

I have attached a spread sheet to illustrate.

In this query it wouuld also be useful to be able to add the summary based on certain contract numbers.  IE SUM if contract is 111 or 112 or 113

Thanks!!
experts.xlsx
0
 

Author Comment

by:HDM
ID: 36589903
After reading this several times I think you have told me what I want but I am just to dense to grasp it.

Can you just explain the break down of the queries so I can understand why it should work??

Thanks!!
0
 
LVL 42

Expert Comment

by:dqmq
ID: 36590156
SELECT SUM(contract_information_total_weight), contract_information_contract, AVG(contract_information_total_weight)
FROM plantname_usa_inc
GROUP BY contract_information_contract
is this what you mean?

;With CTE
as
(select * from plantname_usa_inc
  where 1=1
--put your contract filter here
--and contract_information_contract in (111 ,112 ,113)
)
Select Total_Weight
, contract_information_contract
, contract_information_total_weight/Total_Weight as contract_information_weighted_Avg
FROM CTE
,SELECT sum(contract_information_total_weight) as Total_Weight from CTE
Order by contract_information_contract

0
 

Author Closing Comment

by:HDM
ID: 36601878
Thanks All!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now