473,586 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ 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 3375
Ray
"C L Humphreys" <cl*********@to ofgib.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, qry02GroupCodes Result = 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*********@to ofgib.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
14034
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 errors). The GROUP BY specifies a column that does exist in my db (and is also the only field that is select from the db). Where am I going wrong? ...
3
17359
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 I put it in there, I will get no duplicates (because it is the identity field). The whole point of this is to find dups. Thanks for any help.
4
28816
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 select statement). I'm mostly a SQL hobbiest, so it's possible that I am not doing this in the most efficient manner. Anyone care to comment on...
1
1387
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 1/1/2004 4000.00 40.00 4040.00 1/2/2004 1000.00 10.00 1010.00 1/2/2204 2000.00 20.00 2020.00 1/3/2004 1500.00 15.00 1515.00 I want my...
2
2351
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 00:00:00.000 Subject_1 877 1900-01-01 00:00:00.000 Subject_2 877 2004-12-20 00:00:00.000 Subject_3
2
5980
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 = '01/01/2005' SELECT @EndDate = '06/30/2005' SELECT @LO = 250
4
1802
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
1538
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 amount for each category, and also the 2 other supporting fields. My problems is getting the 3 supporting fields to show since GROUP BY only lets me...
5
6557
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 select="current-group()"/> </xsl:if> <xsl:if test="contains(name, 'teststring22')"> <xsl:apply-templates select="current-group()"/>
0
7912
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...
1
7959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8216
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5710
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...
0
5390
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...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
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
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1180
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...

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.