473,791 Members | 3,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2042
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
1746
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 user the ability to modify entries. By either clicking on a link or a button on each row, I want the user to be sent to another page to edit that entry. What is the best way to track which entry the user is wanting to edit? Thanks for...
14
4046
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") ' <= can I use query here? ' qryEmailS contains only one column - "strS" ' I want to loop through each item in the rst
5
1477
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 brilliant piece of design that I have used in 2 previous projects and they have worked beautifully. What it does is set up the tabs in a container with a different tab
2
1253
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, 2B, 3B, SS, LF, CF, RF, OF, DH Games = number of games played at each POS For example, using this data:
13
1654
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 create 40 queries (one for each defect) with just the two fields, count and defect, and sum the count field but that sounds like a lot of work. Is there a way I can do this much simpler and quicker? My database has two fields, one is called...
29
3582
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 one data field - but i'm not sure) :-) Background info:
3
1967
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 input from the 'shaker' form will populate another 20-25 fields but not the same fields (however there are about 10 common fields to both). I'd thought about using two tables (one for 'conveyor' and the other for 'shaker') but thought I'd try just...
4
2536
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
1638
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 values in 6 fields) are actually equal to the total they submitted on the form. The fields are set to Double with Auto for # decimal places. But I've also tried it with them set to 2-Decimal places. The SQL query looks like this: UPDATE ETL_TBL...
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9517
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5435
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.