473,657 Members | 2,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Group column headings and sum their values

Hello,

can the following be done?

I have a one-rowed query, with the following column headings and
values:

M-1 M-5 M-7 B-C-1 B-D-2 B-D-4 E-5 R-4
--- --- --- ----- ----- ----- --- ---
4 5 1 2 1 3 7 3
I want similiar columns to be grouped and their respective values to be
summed.
I.e.

M-* B-* E-5 R-4
--- --- --- ---
10 6 7 3

Can someone plese tell me how this is done in SQL?

I could just add the columns manually through the Design-View, but this
would cause that new column headings that are added in the future to
not be reflected in the query.

Jean

Nov 13 '05 #1
4 2608
you can't do it any other way than manually. Why would you be adding
columns? This is a telltale sign of incorrect design. Your database
should add ROWS, not columns.

Can't be done in SQL...

Nov 13 '05 #2
Okay, since you can't do it in SQL, you *could* do it with code... but
it's going to be ugly. You have to have some consistent naming scheme
for your fields, and then you could loop through your fields, and
adding the ones with similar prefixes to a query. No matter how you
look at it, it's going to be VERY ugly.

Nov 13 '05 #3
If your table is structured something like this

ID VALUE
---- ---------
M-1 4
M-5 5
..
R-4 3

You can condition the data with 3 queries. that will allow addition of
columns by adding a row to the table. Open the 3rd query to see you ID
values in columns. There may be a way to do this with fewer query, Maybe
someone will post it.

The 1st query will sum grouped on the 1st 2 characters of "ID"

SELECT Left([ID],2) & "*" AS IDType, Sum(tblex.Value ) AS SumOfValue,
Count(Left([ID],2)) AS CountIDType, First(tblex.ID) AS FirstOfID
FROM tblex
GROUP BY Left([ID],2) & "*";

The 2nd query will output the Grouped ID or the original ID and its value,
RowV is added for the cross tab query to work

SELECT IIf([CountIDType]>1,[IDType],[FirstOfID]) AS ID, qryEx1.SumOfVal ue, 1
AS RowV
FROM qryEx1
ORDER BY IIf([CountIDType]>1,[IDType],[FirstOfID]);

The 3rd query is a crosstab to put your ID into columns

TRANSFORM Avg(qryEX2.SumO fValue) AS [The Value]
SELECT qryEX2.RowV
FROM qryEX2
GROUP BY qryEX2.RowV
PIVOT qryEX2.ID;

"Jean" <je**********@h otmail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Hello,

can the following be done?

I have a one-rowed query, with the following column headings and
values:

M-1 M-5 M-7 B-C-1 B-D-2 B-D-4 E-5 R-4
--- --- --- ----- ----- ----- --- ---
4 5 1 2 1 3 7 3
I want similiar columns to be grouped and their respective values to be
summed.
I.e.

M-* B-* E-5 R-4
--- --- --- ---
10 6 7 3

Can someone plese tell me how this is done in SQL?

I could just add the columns manually through the Design-View, but this
would cause that new column headings that are added in the future to
not be reflected in the query.

Jean

Nov 13 '05 #4
Hi guys,

well I am still stuck on this one, but I have taken a different
approach now. I decided to make use of ROWS rather, in order to not
complicate things.

I posted my current problem under this thread now:
http://groups-beta.google.com/group/...6fb733bc1a4e5b

Thanks for your advice.

Kind Regards,
Jean

Nov 13 '05 #5

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

Similar topics

4
21340
by: Thomas Jerkins | last post by:
When I write a create table SQL statement I want to add some information about the column heading of each column. How does the exact syntax for the create table look like (which includes this column data look)? How do I add later column headings ? Tom
8
7564
by: Donna Sabol | last post by:
First, I should start by saying I am creating a database to be used by some very impatient, non-computer literate people. It needs to be seameless in it's operation from their point of view. I want them to do as little as possible when they run their reports. I have a crosstab query that displays usage of items for each month. It looks pretty much like this: ITEM DESC UM 12/02 1/03 2/03 3/03 ...ETC. 1 Solution ...
1
2296
by: Steve | last post by:
I have looked through the newsgroup for an answer to this but haven't been able to find anything resembling my situation. What I want to do is relatively simple, I think. I have a crosstab query that is bound to a report I am creating. I would like the column headings to be the name of the correspoding field name. For example, I have field names ALS, BCD, HLP, and the detail section prints the values for these fields. I would like...
3
4161
by: Paula | last post by:
I need to do a summary using a crosstab query. The data has a Date field (Not named "Date"). I can do the Row Heading and Value but am having trouble with the Column Heading. The summary Columns needs to read like: Sept01 to Aug02 Sept02 to Aug03 Sept03 to Aug04 Can anyone give me a recommendation on how to get from the Date field to these Column Headings? I then would like to set criteria where I can select something that...
3
9235
by: Lee | last post by:
I have a crosstab query that lists the number of patients(DECnum)by County. County is my Row Heading. Currently, the Column heading is Age in months, which is set at one month intervals. I would like to reduce the number of columns by specifying age groupings. For example, Column 1 shoud cover ages 0 to 6 months, Column 2: 7 to 18 months, column 3: 19 -29 months and column 4: 30-36 months. I know I need to insert code for Pivot format,...
3
4859
by: PeterZ | last post by:
Hi, In a running C# app with a datagrid control I select all rows in the dataGrid using CTRL-A, I then paste into some other app like notepad or Word but the column headings get left off. Is there any way of including column headings when copying/pasting from a running datagrid to notepad? At this stage it looks like I have to write my own code but would rather not if someone knows how to do it.
3
1611
by: lorirobn | last post by:
Hi... I think this is a really simple question, but I can't find the answer. I have a subreport that has columns and column headings. The subreport can span over several pages. The column headings don't repeat on the subsequent pages (I have them set up as 'report headings' on the subreport). I read threads that say to instead make these 'group headings', and set repeat to Yes. I feel stupid asking this, but I can't figure out how to...
0
1621
by: damian | last post by:
I have a ListView and i am grouping my data using ListViewGroup. I have a problem in that if the group is empty, the group headings are not shown at all, so i cannot select any data at all for modificaiton in my list view. Is there a value I can toggle that forces the groups to remain shown even if they are empty. I have been working aroudn this by using treeviews instead but it is less than elegant in both the code i have to write and...
5
6414
by: stevepl7 | last post by:
I want to make the rows become column headings in an Access table or query. The data changes based on input to a form. It looks like this: A B N1 1 N2 2 N3 3 N4 4 Where A and B are the column headings.
0
8392
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
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7324
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
6163
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
5632
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
4151
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...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
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.