473,732 Members | 2,204 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Policy HolderId) from PolicyHolder
PH where PH.policyID=P.p olicyID) CountPolicyHold ers from Policy P
where P.policyID in (select policyID from NewPolicy)

OR

select P.policyID, (select count(PH.Policy HolderId) from PolicyHolder
PH where PH.policyID=P.p olicyID) CountPolicyHold ers from Policy P
inner join NewPolicy NP on (P.policyID=NP. policyID)
Any help would be appreciated.
Jan 15 '08 #1
4 4840
On Jan 15, 10:59 am, tom booster <ma.tech...@gma il.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.Policy HolderId) from PolicyHolder
PH where PH.policyID=P.p olicyID) CountPolicyHold ers from Policy P
where P.policyID in (select policyID from NewPolicy)

OR

select P.policyID, (select count(PH.Policy HolderId) from PolicyHolder
PH where PH.policyID=P.p olicyID) CountPolicyHold ers 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.POLICYHOLDER ID)
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...@sbc global.netwrote :
On Jan 15, 10:59 am, tom booster <ma.tech...@gma il.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.Policy HolderId) from PolicyHolder
PH where PH.policyID=P.p olicyID) CountPolicyHold ers from Policy P
where P.policyID in (select policyID from NewPolicy)
OR
select P.policyID, (select count(PH.Policy HolderId) from PolicyHolder
PH where PH.policyID=P.p olicyID) CountPolicyHold ers 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.POLICYHOLDER ID)
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...@gma il.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...@sbc global.netwrote :
On Jan 15, 10:59 am, tom booster <ma.tech...@gma il.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.Policy HolderId) from PolicyHolder
PH where PH.policyID=P.p olicyID) CountPolicyHold ers from Policy P
where P.policyID in (select policyID from NewPolicy)
OR
select P.policyID, (select count(PH.Policy HolderId) from PolicyHolder
PH where PH.policyID=P.p olicyID) CountPolicyHold ers 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.POLICYHOLDER ID)
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.POLICYHOLDER ID) 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
1710
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 select datepart(hour,date) as 'Hour', count (distinct user) 'User' from mensajeejb group by datepart(hour,date) order by datepart(hour,date)
1
3564
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 want to add some counts, such as the number of unique customers that purchased. I know can do this by running a new query to group by customerID, then doing another query against that to count the records, but there has to be a better way. Can I...
1
4174
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 can't use a stored macro or even a query view-. A simple example: Let's say you have a simple database with three tables: People, Laguages and a table with the relationship between them. How do you retrieve the count of persons who don't speak...
2
55948
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? These two attempts failed DataRow dr = ds.Tables.Select( "COUNT(DISTINCT(site_name))" ); object x = ds.Tables.Compute( "COUNT(DISTINCT(site_name))", "ProductionCount > 0" );
1
12570
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
5889
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 userid I wrote query like this
4
12320
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 column key) > 1 ) doesn't seem to work....any tips? thanks
0
567
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 new policies. select P.policyID, (select count(PH.PolicyHolderId) from PolicyHolder PH where PH.policyID=P.policyID) CountPolicyHolders from Policy P
0
8774
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
9307
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9235
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
9181
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...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
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
6031
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
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
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.