473,498 Members | 359 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Syntax - group by and having count

Does anyone have any recommendations on how to solve the following?
I would like to have a query that selects ALL columns from a database,
however only records that have a count of a certain column which is
greater than 1 when the results are grouped by a few columns. I know
the following query doesnt work (because it contains items in the
SELECT that arent in the GROUP BY), but its the jist of what I need to
do.

select a,b,c,d,e,f,g,h
from table1
group by a,b,c,d
having count(e) > 1

Can anyone help me out with this?

Jul 23 '05 #1
3 16582
Below is just a guess because your spec isn't very precise. The
following article explains the best way to get help with this sort of
problem:
http://www.aspfaq.com/etiquett*e.asp?id=5006

Meanwhile, try this:

SELECT a,b,c,d,e,f,g,h
FROM Table1 AS T
WHERE EXISTS
(SELECT *
FROM Table1
WHERE a = T.a
AND b = T.b
AND c = T.c
AND d = T.d
AND e <> T.e)

--
David Portas
SQL Server MVP
--

Jul 23 '05 #2
Thanks for your speedy response. Sorry I was unclear in my question.
Hopefully the sample data will help with my explanation.

example data from table (the real table has 300K records)
a b c d e f g, h......
abcd 547 90 206 19126 02385
abce 547 90 207 19127 9872349
abce 547 90 207 79823 78923075
abce 547 90 207 79823 79872309
abce 547 90 207 79823 89723534
abce 547 90 208 79823 72983454
abce 547 90 208 77834 89052256
abcf 548 91 208 77834 89437545

desired results
abce 547 90 207 79823 78923075
abce 547 90 207 79823 79872309
abce 547 90 207 79823 89723534

Jul 23 '05 #3
In case anyone else is looking for a similar sulution, a user from
another forum posted this answer which helped me out greatly.

select a.* from <tablename> a join
(select col_a,col_b,col_c,col_d,col_e,count(*) dup_count from
<tablename>
group by col_a,col_b,col_c,col_d,col_e
having count(*)>1) b
on a.col_a=b.col_a and
a.col_b=b.col_b and
a.col_c=b.col_c and
a.col_d=b.col_d and
a.col_e=b.col_e

Jul 23 '05 #4

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

Similar topics

699
33308
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
22
3386
by: Tuang | last post by:
I'm checking out Python as a candidate for replacing Perl as my "Swiss Army knife" tool. The longer I can remember the syntax for performing a task, the more likely I am to use it on the spot if...
29
2477
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
3
17350
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...
2
3081
by: Paxton | last post by:
Hi, I'm trying to display the total page views per page within a given date range, but the correct SQL is seemingly beyond me. I get the correct result with a straightforward Group By and Count...
4
1817
by: Chris Kettenbach | last post by:
Hi Peter, I get error when processing the stylesheet. It errors here. <xsl:for-each select="registration)=1]"> specifically: Expression does not return a DOM node. registration)=1]<--
2
10520
by: Anne Heddal | last post by:
In the first line I'm trying to count the the unqiue values of the ID column, but I'm getting syntax error, any idea how to format the distinct count? SELECT Count(test.ID) AS IDCOUNT FROM...
1
11214
by: Sandesh | last post by:
Hello All, Me saying " has any body come across such error would be underestimating". Well I am getting a very peculiar and unique error "Line 1: Incorrect syntax near 'Actions'." ...
6
3583
by: DBDriver | last post by:
I have an Access 97 database on WinXP with a linked Excel table of about 100,000 rows. I'm trying to construct a query that will give me an editable datasheet showing records where data in a pair...
0
7124
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
6998
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
7163
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,...
0
7200
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
5460
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,...
1
4904
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...
0
3090
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...
0
1416
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 ...
0
287
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.