Solved

SQL 2008 Add numbers in a column

Posted on 2011-09-23
10
307 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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