If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > SQL average query

View Poll Results: SQL Average query
You may post attachments 0 0%
You may post replies 0 0%
You may edit your posts 0 0%
You may edit your posts 0 0%
Multiple Choice Poll. Voters: 0. You may not vote on this poll

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-21-04, 19:04
Rajen Rajen is offline
Registered User
 
Join Date: Apr 2004
Location: Los Angeles, CA
Posts: 4
SQL average query

I need your help to write a one SQL query. Following are the table name and column name.

Table Column Name
RA_CUSTOMER_TRX_ALL bill_to_customer_id
cust_trx_type_id

Now, my task is to get average number of transaction types per customer, minimum number of transaction type per customer, maximum number of transaction type.
For example, In following table How many TRX_type used per customer?

bill_to_customer_id cust_trx_type_id
1041 1020
1041 1021
1043 1020
1043 1021
1043 1023
1045 1023
1045 1027
1047 1020
1050 1027
1050 1021
---- ----
---- ----
---- ----
For above example, customer 1041 has 2 transaction type used, 1043 has 3 used, I am looking for all customer average how many transaction type used.
Average 1.25 transaction type used per customer
Minimum 1.0 transaction type used for customer
Maximum 4.0 transaction type used for customer
Reply With Quote
  #2 (permalink)  
Old 04-21-04, 22:52
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
PHP Code:
select min(cust_trx_count) as min_cust_trx_count
     
max(cust_trx_count) as max_cust_trx_count
     
sum(cust_trx_count)
        / 
count(*)         as avg_cust_trx_count
  from 
(        
       
select bill_to_customer_id 
             
,count(*)  as  cust_trx_count
         from RA_CUSTOMER_TRX_ALL 
       group
           by bill_to_customer_id   
       
) as derivedtable 
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 04-22-04, 12:08
Rajen Rajen is offline
Registered User
 
Join Date: Apr 2004
Location: Los Angeles, CA
Posts: 4
SQL average query

Hello,

Thanks for quick reply. This query calculates only transaction type's average but not by customer. What I am looking from this query is how many transaction type used per customer. Is there any other way?

Rajen
Reply With Quote
  #4 (permalink)  
Old 04-22-04, 12:33
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
sorry

add bill_to_customer_id to the outer SELECT

then add GROUP BY bill_to_customer_id at the end
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 04-22-04, 15:55
Rajen Rajen is offline
Registered User
 
Join Date: Apr 2004
Location: Los Angeles, CA
Posts: 4
SQL average query

thanks again but result is not what I am looking for. Here is the result from query. Just sample,

Bill_To_custoner_id AVG_CUST_COUNT
1041 4
1043 2
1045 1
1047 2
--- ------

However, I am looking for average transaction type used per customer. On above example, total 4 customer and total transaction type 9 so 9 divide by 4 customer which will be average. 2.25. Then minimu will be 1 and maximum will be 4.
Reply With Quote
  #6 (permalink)  
Old 04-22-04, 19:10
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
select bill_to_customer_id 
     , min(cust_trx_count) as min_cust_trx_count
     , max(cust_trx_count) as max_cust_trx_count
     , sum(cust_trx_count)
        / count(*)         as avg_cust_trx_count
  from (        
       select bill_to_customer_id 
             ,count(*)  as  cust_trx_count
         from RA_CUSTOMER_TRX_ALL 
       group
           by bill_to_customer_id   
       ) as derivedtable
group
    by bill_to_customer_id
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On