473,513 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Group By and Having are driving me crazy!

9 New Member
Hello,

I'm trying to get a Select query in a stored proc to give the correct results but all I've done so far is pull hair out. Maybe one of you gurus out there can help me!

I have a table with information like this (sorry, couldn't make this look right):

Field1....................... Field2
ABC.........................6
XYZ123.....................6
XYZ123.....................14
XYZ123.....................30
JKL456......................6
JKL456.....................14
JKL456.....................30
NOP987....................14
NOP987....................30
QRS001....................6
QRS001....................14
QRS001....................30

I want to get a distinct list of Field1 where Field2 = 6 but only if Field1 occurs more than once. The result I'm looking for would be:

XYZ123
JKL456
QRS001

This is making me crazy! Anybody got any ideas? Thanks in advance.
Jul 16 '08 #1
5 1313
ck9663
2,878 Recognized Expert Specialist
Try:

Expand|Select|Wrap|Line Numbers
  1. select field1 from yourtable where field2 = 6 group by field1 having count(*) > 1
  2.  

-- CK
Jul 16 '08 #2
xtech1005
9 New Member
I've tried that but it leaves out records. It only returns rows where Field1 AND Field2 occur more than once.

Here is the table with data.

ETF_AAA_Steve 14
ETF_AAA_Steve 30
ETF_AAA_Steve 6
ETF_Bethel_Fuel2 6
ETF_Citrus_Comp_Sales 6
ETF_Citrus_Comp_Sales 6
OPL_Stevie 14
OPL_Stevie 30
OPL_Stevie 6
PinonPltIn_Gen_Stat_DI1 30
PinonPltIn_Gen_Stat_DI1 6
Stevie_Battery_Voltage 14
Stevie_Battery_Voltage 30
Stevie_Battery_Voltage 6
Stevie_Board_Temp 14
Stevie_Board_Temp 30
Stevie_Board_Temp 6
Stevie_Charger_Voltage 14
Stevie_Charger_Voltage 30
Stevie_Charger_Voltage 6
Stevie_STN01 14
Stevie_STN01 30
Stevie_STN01 6

If I use this query -

SELECT field1 FROM #tb
WHERE field2 = 6
GROUP BY field1 HAVING COUNT(*) > 1

The only record I get is -

ETF_Citrus_Comp_Sales
Jul 16 '08 #3
Delerna
1,134 Recognized Expert Top Contributor
Haven't tested this but I think it will work.
You might need to adjust for syntax errors ???

Expand|Select|Wrap|Line Numbers
  1.  
  2. Select Distinct b.Field1
  3. FROM
  4. (select Field1 from TheTable having count(Field1)>1) a
  5. join TheTable b on a.Field1=b.Field1
  6. where b.Field2=6
  7.  
this bit
(select Field1 from TheTable having count(Field1)>1)
is a derived table that contains a list of Field1's that have more than 1 record

joining that to TheTable effectively filters out all records that don't have more than 1 record.
The where clause then filters out all records where Field2 is not equal to 6

Hope that helps!
Jul 17 '08 #4
Delerna
1,134 Recognized Expert Top Contributor
maybe a modification to CK's version would work also. Not sure without testing it?

Expand|Select|Wrap|Line Numbers
  1. select field1 from yourtable where field2 = 6 group by field1 having count(Field1) > 1
  2.  
Jul 17 '08 #5
xtech1005
9 New Member
Haven't tested this but I think it will work.
You might need to adjust for syntax errors ???

Expand|Select|Wrap|Line Numbers
  1.  
  2. Select Distinct b.Field1
  3. FROM
  4. (select Field1 from TheTable having count(Field1)>1) a
  5. join TheTable b on a.Field1=b.Field1
  6. where b.Field2=6
  7.  
this bit
(select Field1 from TheTable having count(Field1)>1)
is a derived table that contains a list of Field1's that have more than 1 record

joining that to TheTable effectively filters out all records that don't have more than 1 record.
The where clause then filters out all records where Field2 is not equal to 6

Hope that helps!
Delerna, that's pretty cool. I had to modify your example but it works great!

Expand|Select|Wrap|Line Numbers
  1.  
  2. Select Distinct b.Field1
  3. FROM
  4. (select Field1 from TheTable GROUP BY TheTable.Field1 having count(Field1)>1) a
  5. join TheTable b on a.Field1=b.Field1
  6. where b.Field2=6
  7.  
Many thanks to you and CK for your assistance! Thank you.
Jul 17 '08 #6

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

Similar topics

4
2518
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
9
2225
by: Melissa | last post by:
Hi, I'm new to the group and haven't had a chance to read the FAQ, but I have a question that's driving me crazy. If anyone here can help me with this or can point me to another site or...
0
1301
by: Shapper | last post by:
Hello, I have this code in Global.asax: Sub Session_Start(Sender As Object, E As EventArgs) Dim cookie As HttpCookie = Request.Cookies("MyCookie") If Not cookie Is Nothing Then...
1
1596
by: Miguel Dias Moura | last post by:
Hello, I have been trying, for days, to retrieve a control's ClientId in a javascript function. I am using a master page and this is why I need to retrieve the Control's ClientId. The control...
5
1699
by: Pupeno | last post by:
Hello, I am experiencing a weird behavior that is driving me crazy. I have module called Sensors containing, among other things: class Manager: def getStatus(self): print "getStatus(self=%s)"...
3
2268
by: rashpal.sidhu | last post by:
Please help, this problem is driving me crazy !! I am using metaphone to create phonetic keys. When i run the module stand-a-lone it works fine. I'm trying to create a runner for informix...
5
1749
by: mark4asp | last post by:
Every time the function below is called I get the alert. So I put a deliberate error in there and I check the value of (reportType=='MANDATE') in Firebug, which is found to be true. But still the...
6
15602
by: linzeyd | last post by:
I have a problem that's driving me crazy. i have a table that is a series of things like this (Unit type) (part description) (count) IWU High pressure flowmeter 3 IWU Transformer ...
2
3616
by: kheitmann | last post by:
OK, so I have a blog. I downloaded the "theme" from somewhere and have edited a few areas to suit my needs. There are different font themes within the page theme. Long story short, my "Text Posts"...
0
7161
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
7384
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
7539
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...
1
7101
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
5686
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
5089
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
3234
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
1596
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
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.