473,473 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

group by clauses

Hi,

Anyone have a really good reference for learning group by/having clauses in
sql? I'm having real trouble getting my head around them.

I can run a query with say, 2 columns, grouping by one and using count() in
the other, but whenever I add another column it says it's not part of an
aggregate function. I don't think I want it to be but there must be another
way around it.

For example I can create this result.

Code Number
X 2
Y 2
Z 3

But now I want this result

ID Code Number
1 X 2
2 X 2
3 Y 2
4 Y 2
5 Z 3
6 Z 3
7 Z 3

Thanks in advance,
Chris
Nov 12 '05 #1
2 3366
Ray
"C L Humphreys" <cl*********@toofgib.moc> wrote in message news:<bm**********@ucsnew1.ncl.ac.uk>...
Hi,

Anyone have a really good reference for learning group by/having clauses in
sql? I'm having real trouble getting my head around them.

I can run a query with say, 2 columns, grouping by one and using count() in
the other, but whenever I add another column it says it's not part of an
aggregate function. I don't think I want it to be but there must be another
way around it.

For example I can create this result.

Code Number
X 2
Y 2
Z 3

But now I want this result

ID Code Number
1 X 2
2 X 2
3 Y 2
4 Y 2
5 Z 3
6 Z 3
7 Z 3

Thanks in advance,
Chris


Hello Chris,

Perhaps what you could do is use two queries to achive
your results.

1. qry01GroupCodes = Your souce query which is the same as
you produced in your sample result above.

Code Number
X 2
Y 2
Z 3

2. Next, qry02GroupCodesResult = Use the same source as you
did for "qry01..." and bring in "qry01.." as your second source.
You should have two objects to source from.

Link the code fields together and use the fields from your first
source,"ID", in your QBE Grid, with your "Code" field and "Number"
fields coming from your second souce. Then run the query.

You should get the following results:

ID Code Number
1 X 2
2 X 2
3 Y 2
4 Y 2
5 Z 3
6 Z 3
7 Z 3

Have fun experimenting.

Regards,

Ray
Nov 12 '05 #2
"C L Humphreys" <cl*********@toofgib.moc> wrote in message news:<bm**********@ucsnew1.ncl.ac.uk>...
Hi,

Anyone have a really good reference for learning group by/having clauses in
sql? I'm having real trouble getting my head around them.

I can run a query with say, 2 columns, grouping by one and using count() in
the other, but whenever I add another column it says it's not part of an
aggregate function. I don't think I want it to be but there must be another
way around it.

For example I can create this result.

Code Number
X 2
Y 2
Z 3

But now I want this result

ID Code Number
1 X 2
2 X 2
3 Y 2
4 Y 2
5 Z 3
6 Z 3
7 Z 3

Thanks in advance,
Chris


use 2 queries. One that does the top query that returns the count per
code. Then create another query where you join that result to the
original table that contains the ID... nothing doing.
Nov 12 '05 #3

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

Similar topics

10
by: Randell D. | last post by:
Folks, I have a SELECT that returns with multiple records - It works when I have a LIMIT clause but when I try to include a GROUP BY clause, the select returns nothing (ie no records, no...
3
by: Robby McGehee | last post by:
I need this to work: SELECT FROM WITH (NOLOCK) where ='a' GROUP BY , HAVING COUNT () > 1 The problem is that I get an error that needs to be in the GROUP BY clause or aggregate function. if...
4
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the...
1
by: Job Lot | last post by:
Is it possible to use Aggregate functions with GROUP BY Clauses on DataTable. I have a DataTable with following values: Date Amount Int Balance 1/1/2004 5000.00 50.00 5050.00...
2
by: aj70000 | last post by:
This is my query select ano,max(date),a_subject from MY_TAB where table_name='xyz' and ano=877 group by a_subject,ano order by a_subject ANO max(Date) A_Subject 877 2005-01-20...
2
by: JJA | last post by:
Please advise on how to get the GROUP BY coded in an acceptable way: DECLARE @LO INT DECLARE @HI INT DECLARE @StartDate varchar(10) DECLARE @EndDate varchar(10) SELECT @StartDate =...
4
by: Daniel R. Rossnagel | last post by:
I can obtain Where clauses, order by, group by, of a consultation SQL, by means of some class similar to codemodel
0
by: tabnaka | last post by:
Hi All, Basically how can I get around the GROUP BY rule of having column names in both SELECT and GROUP BY clauses? I have a query that needs to display two key fields, a field that shows a MAX...
5
by: bbembi_de | last post by:
Hello everyone, I have just a little xslt problem: <xsl:for-each-group select="item" group-by="name"> <xsl:if test="contains(name, 'teststring11')"> <xsl:apply-templates...
0
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,...
0
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...
0
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.