473,385 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Question about summing up different fields in different records

Hi everyone, I have some trouble writing a SQL stored procedure that
can do the following:

We have data in one table in numeric form, but we want to sum the data
in this table based on the values of two different alpha fields. To
illustrate, let me write the following example:

Table with these records:
A B 1.1 2.2 Blah1 Blah1
A B 2.3 5.6 Blah2 Blah2
B C 7.8 9.1 Blah3 Blah3
B C 4.5 1.0 Blah4 Blah4
R F 1.1 4.3 Blah5 Blah5
B A 3.1 2.7 Blah6 Blah6

I need to write a query that will return the following result set from
the above table:
A B 3.4 7.8 Blah1 Blah1
B C 12.3 10.1 Blah3 Blah3
R F 1.1 4.3 Blah5 Blah5
B A 3.1 2.7 Blah6 Blah6

If the alphanumberic keys are the same, the sum the numeric columns up
whilst displaying one one of the records, the blah fields don't matter
if one only one is displayed. Can anyone recommend the best way to do
this? I'm running MS SQL 2005.

Thanks,
Herman

Jul 28 '06 #1
1 2029
herman404 wrote:
Hi everyone, I have some trouble writing a SQL stored procedure that
can do the following:

We have data in one table in numeric form, but we want to sum the data
in this table based on the values of two different alpha fields. To
illustrate, let me write the following example:

Table with these records:
A B 1.1 2.2 Blah1 Blah1
A B 2.3 5.6 Blah2 Blah2
B C 7.8 9.1 Blah3 Blah3
B C 4.5 1.0 Blah4 Blah4
R F 1.1 4.3 Blah5 Blah5
B A 3.1 2.7 Blah6 Blah6

I need to write a query that will return the following result set from
the above table:
A B 3.4 7.8 Blah1 Blah1
B C 12.3 10.1 Blah3 Blah3
R F 1.1 4.3 Blah5 Blah5
B A 3.1 2.7 Blah6 Blah6

If the alphanumberic keys are the same, the sum the numeric columns up
whilst displaying one one of the records, the blah fields don't matter
if one only one is displayed. Can anyone recommend the best way to do
this? I'm running MS SQL 2005.

Thanks,
Herman

SELECT col1, col2,
SUM(col3) AS col3, SUM(col4) AS col4,
MIN(col5) AS col5, MIN(col6) AS col6
FROM your_table
GROUP BY col1, col2 ;

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Jul 28 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mark Feller | last post by:
I want to provide users a page where they can browse entries in a database 10 at a time, for example. I am doing this as a table, where each row is a database entry. I want to be able to give the...
14
by: deko | last post by:
For some reason this does not seem to be working... Am I missing something basic? Dim rst As DAO.Recordset Dim db As DAO.Database Set db = CurrentDb Set rst = db.OpenRecordset("qryEmailS") '...
5
by: Bob | last post by:
Hi Everybody Difficult question Has anyone else used the "Using the Tab control as a container" database example that comes with the above book chapter 7 on the accompanying disc. It is a...
2
by: Cliff | last post by:
I have a database with a table named Fielding that contains (among others) the following records and fields: playerID = unique with records for each change in POS, year, team POS = P, C, 1B,...
13
by: Don Sealer | last post by:
I have about 40 different defects I'm tracking. I'd like to have all of these defects totaled individually, both by month and by year. I'd like to show these results in a report. I know I could...
29
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
3
by: cover | last post by:
I have a table with 50 fields that receive input depending on whether that input came in from a 'shaker' form or a 'conveyor' form. Input from the 'conveyor' form might populate 25 fields while...
4
by: dancole42 | last post by:
So I have an invoicing database based on two main forms: Orders and OrderLines. Orders has fields like: OrderID BillingMethod OrderDate CreditCard CCExp OrdSubTotal ShippingCharge
2
by: JHNielson | last post by:
I Know I've posted an Urgent message before. But I'm in the middle of system testing, and these little stupid bugs are killing me...... I have a query that checks that a set of values (the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.