473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

count distinct subquery

Hi All,

I'm trying to convert a T-SQl query to DB2.

I have two tables policy and policyHolder.
I would like a count of the amount of distinct poicyHolders per
policy, for a particular set of new policies.

select P.policyID, (select count(PH.PolicyHolderId) from PolicyHolder
PH where PH.policyID=P.policyID) CountPolicyHolders from Policy P
where P.policyID in (select policyID from NewPolicy)

OR

select P.policyID, (select count(PH.PolicyHolderId) from PolicyHolder
PH where PH.policyID=P.policyID) CountPolicyHolders from Policy P
inner join NewPolicy NP on (P.policyID=NP.policyID)
Any help would be appreciated.
Jan 15 '08 #1
4 4812
On Jan 15, 10:59 am, tom booster <ma.tech...@gmail.comwrote:
Hi All,

I'm trying to convert a T-SQl query to DB2.

I have two tables policy and policyHolder.
I would like a count of the amount of distinct poicyHolders per
policy, for a particular set of new policies.

select P.policyID, (select count(PH.PolicyHolderId) from PolicyHolder
PH where PH.policyID=P.policyID) CountPolicyHolders from Policy P
where P.policyID in (select policyID from NewPolicy)

OR

select P.policyID, (select count(PH.PolicyHolderId) from PolicyHolder
PH where PH.policyID=P.policyID) CountPolicyHolders from Policy P
inner join NewPolicy NP on (P.policyID=NP.policyID)

Any help would be appreciated.

I think this should do the trick:

SELECT
P.POLICYID,
COUNT(DISTINCT PH.POLICYHOLDERID)
FROM
POLICY P
JOIN
POLICYHOLDER PH
ON
PH.POLICYID = P.POLICYID
WHERE
P.POLICYID IN
(
SELECT
POLICYID
FROM
NEWPOLICY
)
GROUP BY
P.POLICYID

--Jeff
Jan 15 '08 #2
Hi Jeff,
My confusion is about the group by....why is it needed.
what would I do if I needed more string fields returned from both
tables?
Isn't DB2 like T-SQL where all the fields in the select clause not in
the group by have to use an aggregation function?

Essentially I want the distinct count to be based not on the returned
set but on an arbitrary table of my choosing

Thanks
On Jan 15, 2:35*pm, jefftyzzer <jefftyz...@sbcglobal.netwrote:
On Jan 15, 10:59 am, tom booster <ma.tech...@gmail.comwrote:


Hi All,
I'm trying to convert a T-SQl query to DB2.
I have two tables policy and policyHolder.
I would like a count of the amount of distinct poicyHolders per
policy, for a particular set of new policies.
select P.policyID, (select count(PH.PolicyHolderId) from PolicyHolder
PH where PH.policyID=P.policyID) CountPolicyHolders from Policy P
where P.policyID in (select policyID from NewPolicy)
OR
select P.policyID, (select count(PH.PolicyHolderId) from PolicyHolder
PH where PH.policyID=P.policyID) CountPolicyHolders from Policy P
inner join NewPolicy NP on (P.policyID=NP.policyID)
Any help would be appreciated.

I think this should do the trick:

SELECT
* * * * P.POLICYID,
* * * * COUNT(DISTINCT PH.POLICYHOLDERID)
FROM
* * * * POLICY P
JOIN
* * * * POLICYHOLDER PH
ON
* * * * PH.POLICYID = P.POLICYID
WHERE
* * * * P.POLICYID IN
* * * * * * * * (
* * * * * * * * SELECT
* * * * * * * * * * * * POLICYID
* * * * * * * * FROM
* * * * * * * * * * * * NEWPOLICY
* * * * * * * * )
GROUP BY
* * * * P.POLICYID

--Jeff- Hide quoted text -

- Show quoted text -
Jan 15 '08 #3
On Jan 15, 11:58 am, tom booster <ma.tech...@gmail.comwrote:
Hi Jeff,
My confusion is about the group by....why is it needed.
what would I do if I needed more string fields returned from both
tables?
Isn't DB2 like T-SQL where all the fields in the select clause not in
the group by have to use an aggregation function?

Essentially I want the distinct count to be based not on the returned
set but on an arbitrary table of my choosing

Thanks

On Jan 15, 2:35 pm, jefftyzzer <jefftyz...@sbcglobal.netwrote:
On Jan 15, 10:59 am, tom booster <ma.tech...@gmail.comwrote:
Hi All,
I'm trying to convert a T-SQl query to DB2.
I have two tables policy and policyHolder.
I would like a count of the amount of distinct poicyHolders per
policy, for a particular set of new policies.
select P.policyID, (select count(PH.PolicyHolderId) from PolicyHolder
PH where PH.policyID=P.policyID) CountPolicyHolders from Policy P
where P.policyID in (select policyID from NewPolicy)
OR
select P.policyID, (select count(PH.PolicyHolderId) from PolicyHolder
PH where PH.policyID=P.policyID) CountPolicyHolders from Policy P
inner join NewPolicy NP on (P.policyID=NP.policyID)
Any help would be appreciated.
I think this should do the trick:
SELECT
P.POLICYID,
COUNT(DISTINCT PH.POLICYHOLDERID)
FROM
POLICY P
JOIN
POLICYHOLDER PH
ON
PH.POLICYID = P.POLICYID
WHERE
P.POLICYID IN
(
SELECT
POLICYID
FROM
NEWPOLICY
)
GROUP BY
P.POLICYID
--Jeff- Hide quoted text -
- Show quoted text -

Perhaps a correlated scalar subselect will do the trick?

SELECT
P.POLICYID,
(SELECT COUNT( DISTINCT PH.POLICYHOLDERID) FROM POLICYHOLDER
PH WHERE PH.POLICYID = P.POLICYID)
FROM
POLICY P
WHERE
P.POLICYID IN
(
SELECT
POLICYID
FROM
NEWPOLICY
)

--Jeff
Jan 15 '08 #4
Thanks Jeff!

Jan 17 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: puskas | last post by:
I'm trying to make a report of users per hour (I have one that works for users per day) but when I execute the query I realize that I get more users than the ones I have here is my query ...
1
by: Steve Heath | last post by:
I have a query that provides detail for sales transactions meeting certain criteria (date, purchase type, etc.) I am creating a report based on that query, and I want to add a summary section. I...
1
by: Leny | last post by:
Hi, Since Access doesn't support COUTN DISTINCT, I'm facing a new problem I didn't take into account: how to get the count of PK's in a complex query. I create queries on the fly -this means I...
2
by: Michael Howes | last post by:
I have a single DataTable in a DataSet. It has 4 columns and i'd like to get a handful of counts of unique items in 3 of the 4 columns. Can a DataTables Select or Compute methods to COUNT DISTINCT?...
1
by: jerry.ranch | last post by:
I see that Access 2003 doesn't support count distinct?? I have a work around, but one statement would be preferably of course Jerry
1
by: aps786 | last post by:
Hi, There is a table where I store ipaddress and user who logged in from that IP. I have a query to findout all ipaddresses, from where diff users had made request. stat ------------ ip...
4
by: Mina Patel | last post by:
Hi trying to find the number of duplicate tuples/records in table based on multiple columns ie. select count(distinct list multiple column key ) from x where date = y HAVING ( COUNT(multiple...
0
by: tom booster | last post by:
Hi All, I'm trying to convert a T-SQl query to DB2. I have two tables policy and policyHolder. I would like a count of the amount of distinct poicyHolders per policy, for a particular set of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.