Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / General DB Topics / General DB Topics / November 2004

Tip: Looking for answers? Try searching our database.

SQL HELP

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chuck100 - 18 Nov 2004 16:19 GMT
I have a table that looks like this (I've simplified it)

case_number payment_id
1000          500
1000          501
1000          502
1001          503
1002          504
1003          505
1003          506
1003          507
1004          508

I want to count the number of payment ids per case number so my output
looks like this :-

Number of payment ids     number of case numbers
     3                             2
     1                             3

In other words there were 2 case numbers that had 3 payment ids and
3 case numbers that had 1 payment id only.

Grateful for any ideas.
Jarl Hermansson - 18 Nov 2004 20:48 GMT
> I have a table that looks like this (I've simplified it)
>
[quoted text clipped - 20 lines]
>
> Grateful for any ideas.

The easiest way to understand this to do the operation in two steps.

First create a view which returns each distinct case_number together with
its number of payment_id's.

CREATE VIEW caseview (case_number, number_of_payment_ids) AS
 SELECT case_number, COUNT(*)
 FROM casetable
 GROUP BY case_number

This view will return the following:
SQL>select * from caseview;
CASE_NUMBER NUMBER_OF_PAYMENT_IDS
=========== =====================
      1000                     3
      1001                     1
      1002                     1
      1003                     3
      1004                     1

You can now use the view to get the wanted result:

SELECT number_of_payment_ids,
      COUNT(*) AS number_of_case_numbers
FROM caseview
GROUP BY number_of_payment_ids;

HTH,
Jarl
Tokunaga T. - 19 Nov 2004 02:06 GMT
How about this?
SELECT payment_cnt AS "Number of payment ids"
    , COUNT(*)    AS "Number of case numbers"
 FROM (SELECT COUNT(payment_id) AS payment_cnt
         FROM case_payment
        GROUP BY
              case_number
      ) AS Q
GROUP BY
      payment_cnt
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.