• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 325
  • Last Modified:

SQL 2008 Add numbers in a column

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
Curtis Long
Asked:
Curtis Long
  • 4
  • 3
  • 3
2 Solutions
 
MuffyBunnyCommented:
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
 
dqmqCommented:
SELECT SUM(contract_information_total_weight), contract_information_contract, AVG(contract_information_total_weight)
FROM plantname_usa_inc
GROUP BY contract_information_contract


0
 
Curtis LongAuthor Commented:
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
MuffyBunnyCommented:
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
 
dqmqCommented:
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
 
MuffyBunnyCommented:
Yeah, so what I said
0
 
Curtis LongAuthor Commented:
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
 
Curtis LongAuthor Commented:
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
 
dqmqCommented:
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
 
Curtis LongAuthor Commented:
Thanks All!!
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now