Avatar of jasmeen kaur
jasmeen kaur
 asked on

Query Syntax

Hi All,

My table contains client number and related records. A client can have one or more than one account. I need records of the client which has only 1 account related to it. I am not able to build this query. Could you please help
Oracle DatabaseSQL

Avatar of undefined
Last Comment
Pawan Kumar

8/22/2022 - Mon
Ryan Chong

can you provide some sample data?

and what database are you using?
Pawan Kumar

Below query will give you proper output you need.  For this I am using count(*) Over (Partition by client order by account) so that we can count how many accounts each client has

SELECT * FROM 
(
SELECT * , COUNT(*) OVER (PARTITION BY client order by account) cnt
FROM yourtable 
)r 
WHERE cnt = 1

Open in new window

jasmeen kaur

ASKER
It is oracle Db. Below is the sample data. I need client which has only one account, here it is client 2 , so I need the 3 row of this table

Client Number     Account Number     Balance
C1                           A123                          100
C1                           A546                          50
C2                           A65456                      30
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Pawan Kumar

Please try this -

SELECT y.* FROM 
(
	SELECT [Client Number] FROM 
	(
		SELECT [Client Number] , COUNT(*) cnt
		FROM yourtable 
		GROUP BY [Client Number]
	)r 
	WHERE cnt = 1
)o INNER JOIN yourtable y ON O.[Client Number] = y.[Client Number]

Open in new window


Output

/*------------------------
OUTPUT
------------------------*/
Client Number Account Number Balance
------------- -------------- -----------
C2            A65456         30

(1 row(s) affected)

Open in new window

Pawan Kumar

For ORACLE  please use below

You can adjust the column names.

WITH CTE AS 
(
    SELECT 'C1' c ,'A123' a , 100 b
    FROM DUAL  UNION ALL
    SELECT 'C1','A546',50  FROM DUAL UNION ALL
    SELECT 'C2','A65456',30  FROM DUAL
)
,CTE1 AS
(
    SELECT c FROM 
    (SELECT c , COUNT(*) cnt
    FROM CTE 
    GROUP BY c) WHERE cnt = 1
)
SELECT k1.* FROM CTE1 k INNER JOIN CTE k1 ON k1.c = k.c

Open in new window


OUTPUT

 	C	A	B
1	C2	A65456	30

Open in new window

Ryan Chong

a similar solution using Group By with Having clause.

create table mytable (
    Client_Number varchar2(4000),
    Account_Number varchar2(4000),
    Balance number
);
/

insert into mytable (Client_Number, Account_Number, Balance)
select 'C1', 'A123', 100 from dual union all
select 'C1', 'A546', 50 from dual union all
select 'C2', 'A65456', 30 from dual;
/

select a.* from mytable a
inner join (
select Client_Number from mytable group by Client_Number having count(*) = 1
) b on a.Client_Number = b.Client_Number

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Pawan Kumar

another simple and tested method is given below -

Explanation - Just count how many accounts you have per client and take client who has only 1 account using -  COUNT(*) OVER(PARTITION BY c) as cnt

WITH CTE AS 
(
    SELECT 'C1' c ,'A123' a , 100 b
    FROM DUAL  UNION ALL
    SELECT 'C1','A546',50  FROM DUAL UNION ALL
    SELECT 'C2','A65456',30  FROM DUAL
)
SELECT c clientNumber ,a accountNumber , b Balance
FROM 
(
    SELECT c,b,a , COUNT(*) OVER(PARTITION BY c) as cnt FROM CTE

)WHERE cnt = 1 

Open in new window


Output

 	CLIENTNUMBER	ACCOUNTNUMBER	BALANCE
1	C2	A65456	30

Open in new window

jasmeen kaur

ASKER
@Pawan: Thanks for your help.
I have one issue: in the WITH CTE clause, how will I select the data? c1, A123 this is data...should I give column names here?
ASKER CERTIFIED SOLUTION
Pawan Kumar

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
awking00

select * from yourtable where clientnumber in
(select clientnumber from
 (select clientnumber, count(*) from yourtable group by clientnumber having count(*) = 1)
)
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Geert G

you don't need to supply the count(*) in the column names ...

select * from yourtable where clientnumber in
 (select clientnumber from yourtable group by clientnumber having count(*) = 1)

Open in new window

jasmeen kaur

ASKER
Thanks for your help guys. My problem solved
Pawan Kumar

welcome. glad to help as always.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.