473,507 Members | 2,375 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 2368
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
2239
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...
3
7329
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,...
2
2342
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...
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...
4
8158
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
19861
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...
1
1785
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,...
1
1434
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...
11
1818
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 ...
4
2610
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...
0
7223
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
7111
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
7376
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
5623
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
5042
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
4702
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...
0
3191
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
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
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 ...

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.