Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL 2008 Add numbers in a column

Posted on 2011-09-23
10
Medium Priority
?
324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 6

Assisted Solution

by:MuffyBunny
MuffyBunny earned 400 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 1600 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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

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.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard 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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

618 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