X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id BE58E5391F for ; Tue, 19 Apr 2005 04:13:05 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 75698-03 for ; Tue, 19 Apr 2005 07:12:56 +0000 (GMT) Received: from ausimss.pervasive.com (ausimss.pervasive.com [66.45.103.246]) by svr1.postgresql.org (Postfix) with ESMTP id 48F905393A for ; Tue, 19 Apr 2005 04:12:55 -0300 (ADT) Received: from ausbayes1.aus.pervasive.com ([172.16.8.6]) by ausimss.pervasive.com with InterScan Messaging Security Suite; Tue, 19 Apr 2005 02:12:56 -0500 Received: from ausmail2k3.aus.pervasive.com ([172.16.4.8]) by ausbayes1.aus.pervasive.com with Microsoft SMTPSVC(5.0.2195.6713); Tue, 19 Apr 2005 02:12:55 -0500 Received: from BLRMAIL1.aus.pervasive.com ([10.3.16.60]) by ausmail2k3.aus.pervasive.com with Microsoft SMTPSVC(5.0.2195.6713); Tue, 19 Apr 2005 02:12:55 -0500 content-class: urn:content-classes:message Subject: Re: SQL subquery (count distinct) - Any Ideas? MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Date: Tue, 19 Apr 2005 12:42:45 +0530 X-MimeOLE: Produced By Microsoft Exchange V6.0.6249.0 Message-ID: <02767D4600E59A4487233B23AEF5C59922C290@blrmail1.aus.pervasive.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [SQL] SQL subquery (count distinct) - Any Ideas? Thread-Index: AcVEg3DAF9vDc2BIRem+JSIC3IFD6gALCFPg From: "Ramakrishnan Muralidharan" To: "Matt Fulford" , X-OriginalArrivalTime: 19 Apr 2005 07:12:55.0062 (UTC) FILETIME=[35176B60:01C544AF] X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.464 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200504/349 X-Sequence-Number: 21242 Hi I have modified the SQL query given by you and I hope the query given = below will give you the correct count. =20 SELECT U.USER_ID , U.NAME , COUNT(*) FROM USER U , CALL C , = CALLER_SESSION CS=20 WHERE C.CALLER_SESSION_ID =3D CS.CALLER_SESSION_ID AND U.USER_ID =3D = CS.USER_ID AND CAST( CS.SESSION_DATE AS DATE ) =3D CAST( '2005-04-13' AS = DATE ) GROUP BY U.USER_ID Regards, R.Muralidharan -----Original Message----- From: Matt Fulford [mailto:mtfulford@yahoo.co.uk] Sent: Wednesday, April 13, 2005 10:59 PM To: pgsql-sql@postgresql.org Subject: [SQL] SQL subquery (count distinct) - Any Ideas? I'm trying to write a query to return the number of different customers=20 called on a single day. The database has a table called 'user', a table = called 'caller_session' which references a 'user' record, and a table = called=20 'call' which references a 'caller_session'. This is my current attempt: select user.name, sum((select count(distinct call.customer_id) from call where=20 call.caller_session_id=3Dcs.caller_session_id)) as contacted from user, caller_session cs where cs.user_id=3Duser.user_id and date(cs.session_date) =3D date('2005-04-13') group by user.name; I get back a list of names and a call count (as desired), but the count = is=20 not correct! The subqery is counting the number of different customers=20 called for each caller_session, and summing them based on the user.name=20 field. If the same customer is called in 2 different sessions, this is=20 being counted as 2 customers, but should be 1! The 'contacted' count calculation has to be in a subquery as above, = because=20 there are other subqueries after this one to calculate other data based = on=20 the outer query (the outer query has to be as it is here). Not quite = sure=20 where the sum, count and distinct should really go to get the required=20 results! Any help greatly appreciated! ---------------------------(end of broadcast)--------------------------- TIP 8: explain analyze is your friend