473,804 Members | 3,809 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query using the Having clause

7 New Member
Hi guys,

I have some trouble with the following query.
Please look into this and lemme know the solution ASAP.
It certainly involves aggregations and the 'having clause'

BROKER(
ID integer primary key
, NAME string)

ACCOUNT(
ACCOUNT_NUM integer primay key,
BROKER_ID references BROKER.ID,
GAIN real)

The value of the attribute GAIN in the relation ACCOUNT indicates whether money has been lost or gained in that account. If value of GAIN is less than zero than the account has lost money. Otherwise the account is either even (if value of GAIN is zero or NULL) or has gained money (if value of GAIN is greater than zero).

Write a SQL statement to fire all brokers who lost money in at least 40% of their accounts. Assume that every broker has at least one account. Firing means that the rows corresponding to that broker must be removed from the Broker table and BrokerId in corresponding rows in Account table must be set to null.
Oct 26 '07 #1
8 2387
Rabbit
12,516 Recognized Expert Moderator MVP
There's no way you can do all that in one SQL statement. At best you're looking at two statements. Two update queries with subqueries.

What have you tried so far?
Oct 26 '07 #2
rbukkara
7 New Member
There's no way you can do all that in one SQL statement. At best you're looking at two statements. Two update queries with subqueries.

What have you tried so far?
i have first grouped by the broker id and in the having clause i want to count all the rows for a particular rating, which is easy and want to compare that with all the rows which have a negative gain -- which i am not able to do!
jus see this!
Expand|Select|Wrap|Line Numbers
  1. select broker_id from account group by broker_id having (0.4*count(ac.broker_id) in (select ac.broker_id, count(ac.broker_id) from account ac group by ac.broker_id)< count(ac1.broker_id) in (select ac1.broker_id, count(ac1.broker_id) from account ac1 where ac1.gain<0 group by ac1.broker_id));
  2.  
Oct 26 '07 #3
Rabbit
12,516 Recognized Expert Moderator MVP
i have first grouped by the broker id and in the having clause i want to count all the rows for a particular rating, which is easy and want to compare that with all the rows which have a negative gain -- which i am not able to do!
jus see this!
Expand|Select|Wrap|Line Numbers
  1. select broker_id from account group by broker_id having (0.4*count(ac.broker_id) in (select ac.broker_id, count(ac.broker_id) from account ac group by ac.broker_id)< count(ac1.broker_id) in (select ac1.broker_id, count(ac1.broker_id) from account ac1 where ac1.gain<0 group by ac1.broker_id));
  2.  
Please use code tags as per the forum guidelines.

As for your SQL. I can not make heads or tails of it. You have subquery in subquery using incorrect syntax. You've made it too complex. You only need one subquery. You just need to check that the count of negative accounts divided by a subquery returning a total count for that broker doesn't surpass your threshold. You'll also need to group by the Broker ID and only include fields where the gain is negative.
Oct 26 '07 #4
rbukkara
7 New Member
Please use code tags as per the forum guidelines.

As for your SQL. I can not make heads or tails of it. You have subquery in subquery using incorrect syntax. You've made it too complex. You only need one subquery. You just need to check that the count of negative accounts divided by a subquery returning a total count for that broker doesn't surpass your threshold. You'll also need to group by the Broker ID and only include fields where the gain is negative.

I was trying different ways of getting it done. I know it needs only one sub-query
this is the one I first framed and I guess it will make sense.

Expand|Select|Wrap|Line Numbers
  1. select broker_id from account group by broker_id having (0.4*count(*) < (select count(ac1.broker_id) from account ac1 where ac1.gain<0 ));
Instead of the division operator I have used it this way!
Oct 26 '07 #5
Rabbit
12,516 Recognized Expert Moderator MVP
I was trying different ways of getting it done. I know it needs only one sub-query
this is the one I first framed and I guess it will make sense.

Expand|Select|Wrap|Line Numbers
  1. select broker_id from account group by broker_id having (0.4*count(*) < (select count(ac1.broker_id) from account ac1 where ac1.gain<0 ));
Instead of the division operator I have used it this way!
You're close. I see where you're getting at but:

1) You set up your alias incorrectly and in the wrong place. An alias is set up with the AS keyword in between. So Account AS ac1. Also, it shouldn't be in the subquery but in the outer query.

2) Your subquery needs to account for the BrokerID of the outer query.
Oct 26 '07 #6
rbukkara
7 New Member
You're close. I see where you're getting at but:

1) You set up your alias incorrectly and in the wrong place. An alias is set up with the AS keyword in between. So Account AS ac1. Also, it shouldn't be in the subquery but in the outer query.

2) Your subquery needs to account for the BrokerID of the outer query.
It did not work when I used the 'AS' keyword. May be it is different in ORACLE.
and what do you mean by the 'needs to account for the BrokerID of the outer query'?
Oct 26 '07 #7
rbukkara
7 New Member
It did not work when I used the 'AS' keyword. May be it is different in ORACLE.
and what do you mean by the 'needs to account for the BrokerID of the outer query'?
Expand|Select|Wrap|Line Numbers
  1. select broker_id from account group by broker_id having 0.4*count(*) < (select count(ac1.broker_id) from account ac1 where ac1.broker_id=broker_id and ac1.gain<0);
even this is not working!
Oct 26 '07 #8
Rabbit
12,516 Recognized Expert Moderator MVP
Expand|Select|Wrap|Line Numbers
  1. select broker_id from account group by broker_id having 0.4*count(*) < (select count(ac1.broker_id) from account ac1 where ac1.broker_id=broker_id and ac1.gain<0);
even this is not working!
Please use Code tags as per the forum guidelines, second warning.

You're using Oracle? You do realize that this is the Access forum and that we have an Oracle forum right?

It's been a really long time since I've worked with Oracle. But, assuming you've set up the alias correctly, if you remember what I said in the previous post, it's still in the wrong place. The alias has to be for the outer query.
Oct 26 '07 #9

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

Similar topics

5
2256
by: Jerome | last post by:
Hi, I've got the following problem: I want my ASP page to display a certain number (based on a COUNT query), it works fine if the result is at least 1! If there are no records to be counted (= the query is empty), I get errors. What I've tried is this:
3
7342
by: David | last post by:
Consider this SQL Query: ----------------------------------------------------------------- SELECT c.CASE_NBR, DATEDIFF(d, c.CREATE_DT, GETDATE()) AS Age, c.AFFD_RCVD, c.PRV_CRD_ISS, x.RegE, x.Type, x.Fraud, c.CUST_FN + ' ' + c.CUST_LN AS CustFullName, c.ATM_CKCD_NBR, x.TotalLoss, x.Queue, x.Status, c.QUEUE AS Expr1, x.CHECK_ACT_NBR, c.CUST_LN, c.SSN, c.CREATE_DT FROM ( SELECT TOP 9999999 cl.CASE_NBR, cl.SSN, cl.CREATE_DT,
2
2363
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
14
611
by: signaturefactory | last post by:
I am trying the following query in and oleDbCommand: SELECT PartLocations.LocationName, Sum(PartsJournal.Quantity) AS SumOfQuantity, PartsJournal.PartsLotNumber FROM PartLocations INNER JOIN PartsJournal ON PartLocations.LocationID = PartsJournal.LocationID GROUP BY PartLocations.LocationName, PartsJournal.PartsLotNumber, PartsJournal.PartNumber, PartsJournal.LocationID HAVING (((Sum(PartsJournal.Quantity))>0) AND...
4
8180
by: uspensky | last post by:
I have a table (cars) with 3 fields: VIN, Class, sell_price 101, sports, 10000 102, sports, 11000 103, luxury, 9000 104, sports, 11000 105, sports, 11000 106, luxury, 5000 107, sports, 11000
24
19918
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every renewal in the history of the policyholder. The information is in 2 tables, policy and customer, which share the custid data. The polno changes with every renewal Renewals in 2004 would be D, 2005 S, and 2006 L. polexpdates for a given customer...
1
1801
by: mamapossible | last post by:
Hi, I've spent hours trying to optimize this simple query: SELECT count(sites_jobs.id) as jobCount, sites_jobs_categories.frn_site_id, sites_jobs_categories.id, sites_jobs_categories.name, FROM sites_jobs_categories LEFT JOIN sites_jobs ON sites_jobs.frn_jobs_categories_id = sites_jobs_categories.id GROUP BY sites_jobs_categories.id HAVING sites_jobs_categories.frn_site_id = #request.siteId#
1
1446
by: AJ | last post by:
Folllowing on from a previous post, i have created a stored query as follows. SELECT c.ID, c.Company_Name, p., 1 As QueryNbr FROM (Company AS c LEFT JOIN Sale AS s ON c.ID = s.Company_ID) LEFT JOIN Package AS p ON s.Package_ID = p.ID WHERE c.Category = 'EXH'
11
1847
by: Stan | last post by:
I am using MS Office Access 2003 (11.5614). I have been running the following query with good results. The query lets me enter the month period and returns the SUM of the age fields. SELECT AS , SUM() AS , SUM() AS , SUM() AS , SUM() AS FROM WHERE FORMAT(,"mm/yyyy")=; I now want to add an additional criteria of the city field. I added
4
2623
by: dsdevonsomer | last post by:
Hello, I have one simple query joining two tables with left outer join on 3 fields and using MIN on two fields. These two tables have lot of data about 3 mil in total. I am trying to migrate db from MS Access to SQL 2005. I get about 689000 rows in SQL Server, vs 863000 rows in MS Access. SELECT T1., T1., T2., MIN ( T1.), MIN(T1. ), T1.COUNT
0
9706
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
9577
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
10569
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...
1
10315
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
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
7615
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
6847
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
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3815
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.