473,671 Members | 2,290 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 1320
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_Fuel 2 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_Te mp 14
Stevie_Board_Te mp 30
Stevie_Board_Te mp 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
2528
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() sorted_feature_vector.sort() #feature_vector.keys()=sorted_feature_vector
9
2233
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 something, I'd appreciate it. I'm trying to update my website, and I created about eight pages of html code on a Mac, using Simple Text. I viewed all pages locally on my Mac using Netscape, and they looked fine.
0
1304
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 Response.Write(" * Cookie Exists * ") If Not cookie.Values("culture") Is Nothing
1
1604
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 Id is "Panel1". I placed "<%=Panel1.ClientID %>" in my HTML code and I got "ctl00_pPanel1". In my Javascript function I am using:
5
1708
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)" % self return {"a": "b", "c": "d"} and then I have another module called SensorSingleton that emulates the
3
2276
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 which includes the function in order to allow me to call it from within a 4gl program. When i do this i get differences in the key that is produced????? This only happens when metahpone translates an x in a name. The stand
5
1763
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 alert comes up. Why? I checked the following watch expressions at the blah blah point. id = 5843 reportType = "MANDATE"
6
15644
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 IWU Fender 2 GEN Fender 2 GEN Antifreeze 2 GEN Connection 1 60A Relief valve 1
2
3628
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" are supposed to be in the font: Georgia, but they are showing up in "Times New Roman"...blah! I can't find anything wrong in the code, but who am I trying to fool? I know nothing about this stuff. The code is below. The parts that I *think*...
0
8473
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8390
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8911
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8667
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6222
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5692
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4222
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4402
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2808
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

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.