Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

Multiple Count functions, with different criteria from an alias table

$
0
0
I am trying to develop a query to run across multiple tables, then run multiple count functions with specific criteria for each. It seemed simplest to first pull all the data into an alias table, then use this table to pull all the date from (which may not be the simplest way, however I am limited in my knowledge).

For a simple example I have tables, user, shop, staff, shop_staff

user
id | name
=========
1 | John
2 | Kate
3 | Lisa

shop
id | store |assigned_user_id
=========================
S1 | Coffee Shop | 1
S2 | Pizza Shop | 2
S3 | Restaurant | 3
S4 | Take Away | 1

staff
id | trainer
=========
1 | Will
2 | Ben
3 | Jack

shop_staff
id | storeid | trainerid
====================
A | S1 | 1
A | S2 | 1
A | S2 | 2
A | S3 | 3
A | S4 | 3

I pull all the data required into an alias table, Alt, using this segment of in the final query to give the resulting table below.

Code:

SELECT    shop.id  ,
          shop.store,
          user.name ,
          staff.trainer
FROM      user
          INNER JOIN shop
          ON        user.id = shop.assigned_user_id
          INNER JOIN shop_trainer
          ON        shop.id = shop_staff.storeid
          INNER JOIN trainer
          ON        shop.staff_trainerid = trainer.id


id | store | name | trainer
================================
S1 | Coffee Shop | John | Will
S2 | Pizza Shop | Kate | Will
S2 | Pizza Shop | Kate | Ben
S3 | Restaurant | Lisa | Ben
S4 | Take Away | John | Jack

I then use the following code to display a list of the total number stores and trainers a user has linked to them. Stores must be distinct, and stores and trainers have some criteria to the search.

Code:

SELECT DISTINCT Alt.name          ,
                COUNT (Alt.trainer),
                COUNT(DISTINCT Alt.store)
FROM            (SELECT    shop.id  ,
                          shop.store,
                          user.name ,
                          staff.trainer
                FROM      user
                          INNER JOIN shop
                          ON        user.id = shop.assigned_user_id
                          INNER JOIN shop_trainer
                          ON        shop.id = shop_staff.storeid
                          INNER JOIN trainer
                          ON        shop.staff_trainerid = trainer.id
                )
                AS Alt
WHERE          Alt.store IN ('Coffee Shop',
                                'Pizza Shop' ,
                                'Take Away')
AND            Alt.trainer IN ('Will',
                                  'Ben')
GROUP BY        Alt.name

name | trainer | store
=============
John | 1 | 2
Kate | 2 | 1
Lisa | 1 | NULL

The result I want is above, however obviously this does not occur as the two criteria statements need to both be filled in the code I wrote.

What I want to do is have the first criteria only apply to the store count, and the second only apply to the trainer count.

Unsure how to do this. Hopefully that all makes sense. Thanks for any help!

Viewing all articles
Browse latest Browse all 13329

Trending Articles