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 > sum of counts of same data existing in 2 different tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-24-09, 22:45
joe robles joe robles is offline
Registered User
 
Join Date: Jun 2009
Posts: 10
sum of counts of same data existing in 2 different tables

Hi all,

There are two similar tables in 2 different schemas/databses that contain employee information.

I have a requirement to get the list of the emp ids from those two tables whose sum of the count in the above tables is less than or equal to a user defined value.

This is a wierd requirement but we need this for the data setup for one of our important performance tests.


Example:

Emp_ids 1, 2 and 3 exist both in table A and table B.

For emp_id 1, table A has 20 records for that emp_id and table B has 20 records => sum of counts = 40

For emp_id 2, table A has 40 records for that emp_id and table B has 40 records => sum of counts = 80

For emp_id 3, table A has 100 records for that emp_id and table B has 100 records => sum of counts = 100

User wants only those emp_ids whose sum of count from both tables is less than or equal to 80.

Thus the result of the sql must be emp_id 1 and 2 only.

How do I do that?

I tried few things but in vain.

Any help is appreciated.

Thanks in advance for your time and interest.
Reply With Quote
  #2 (permalink)  
Old 07-25-09, 06:39
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,511
Code:
SELECT emp
     , SUM(subtotal) AS total
  FROM ( SELECT emp
              , COUNT(*) AS subtotal
           FROM A
         GROUP
             BY emp
         UNION ALL
         SELECT emp
              , COUNT(*) 
           FROM B
         GROUP
             BY emp ) AS d
GROUP
    BY emp
HAVING total <= 80
__________________
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