473,396 Members | 1,929 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,396 software developers and data experts.

MS-SQL question about GROUP BY

Hi,

I have a table that has email, q1,q2 etc. I would like to select each email and the result should have each email listed once with the values of q1,q2,q3 & q4.

For example:
email-------------q1---q2
a@domain-----a
b@domain-----a
a@domain-------------b
b@domain-------------a

the result should be:
email------------q1---q2
a@domain-----a----b
b@domain-----a----a

with the query:
Expand|Select|Wrap|Line Numbers
  1. SELECT email, q1, q2, q3, q4 FROM my_table
  2. GROUP BY email
  3. ORDER BY email
I get the following error:
"Column 'mytable.q1' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause"

and this query gives every back without grouping:
Expand|Select|Wrap|Line Numbers
  1. SELECT email, q1, q2, q3, q4 FROM my_table
  2. GROUP BY email,q1,q2,q3,q4
  3. ORDER BY email
How can I resolve my problem?

Thanx in advance.

Denitram
Feb 14 '07 #1
2 3682
almaz
168 Expert 100+
Solution depends on what do you expect to get when there are several records for particular email with different values for q1, i.e.
Expand|Select|Wrap|Line Numbers
  1. email     q1  q2
  2. a@domain  a
  3. a@domain  b
Following solution gets a maximum from "a" and "b" for my example.
Expand|Select|Wrap|Line Numbers
  1. SELECT email, max(q1), max(q2), max(q3), max(q4)
  2. FROM my_table
  3. GROUP BY email
  4. ORDER BY email
Feb 15 '07 #2
Solution depends on what do you expect to get when there are several records for particular email with different values for q1, i.e.
Expand|Select|Wrap|Line Numbers
  1. email     q1  q2
  2. a@domain  a
  3. a@domain  b
Following solution gets a maximum from "a" and "b" for my example.
Expand|Select|Wrap|Line Numbers
  1. SELECT email, max(q1), max(q2), max(q3), max(q4)
  2. FROM my_table
  3. GROUP BY email
  4. ORDER BY email
Thank you a lot, it's working
Feb 15 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: CY FOK | last post by:
Hi I am planning to open a software company to develop client-server apps and web applications for my client. Now, i am in a difficult situation to determine what is the best platform i should use...
3
by: sridevi | last post by:
Hello How to export data from ms-access database to excel worksheet using ASP. mainly i need to export data to multiple worksheets. it is very urgent to us. i have a sample code which works...
7
by: Ronnie | last post by:
I'm curious if anyone has any insights into what Microsoft has in store for ACCESS in current or future releases? I'm currently working on Access 2000 and haven't seen the newer versions. I'm...
33
by: Uwe Range | last post by:
Hi to all! A customer of mine told me some days ago that her IT-people told her ACCESS would not be such a good idea for continuing with our project, because Access will not be continued in the...
0
by: com | last post by:
MS Access 2000 Password Recoverer 4.2 Screenshot - Soft30.com MS Access 2000 Password Recoverer will display the password to a MS Access database (*.mdb). This program works for MS Access files...
10
by: Wolfgang Kaml | last post by:
Hello All, I have been working on this for almost a week now and I haven't anything up my sleeves anymore that I could test in addition or change.... Since I am not sure, if this is a Windows...
2
by: Mikey | last post by:
Sample VB .NET source code to create mailing labels or customized letters using MS Word MailMerge This VB .NET source code will start MS Word and call methods and set properties in MS Word to...
4
by: PabsBath | last post by:
Hello, help please. I have been pulling my hair out for a few weeks now and been looking on the web for answers but not managed to find anything!! I'm currently operating a small (2mb - approx...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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
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...
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,...

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.