Solved

SQL 2008 Add numbers in a column

Posted on 2011-09-23
10
313 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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
 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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