tobzzz
asked on
SQL COUNT statement using JOIN between TBLS
SQL 1:
Below is the example data in two tables, one called statistic and one called address. I need to write an SQL count statement that gives me the results below these two tables which is basically top visited cities in order of popularity.
table called 'statistic'
statisticid | addressid | listingid
-------------------------- ---------- --
1 | 1 | 9
-------------------------- ---------- --
2 | 3 | 2
-------------------------- ---------- --
3 | 1 | 10
-------------------------- ---------- --
4 | 3 | 4
-------------------------- ---------- --
5 | 2 | 6
-------------------------- ---------- --
6 | 1 | 1
-------------------------- ---------- --
7 | 1 | 7
-------------------------- ---------- --
table called 'address'
addressid | city |
-------------------------
1 | LONDON
-------------------------
2 | GLASGOW
-------------------------
3 | BELFAST
-------------------------
DESIRED RESULTS SQL.1
location | hits |
-------------------------
LONDON | 4
-------------------------
BELFAST | 2
-------------------------
GLASGOW | 1
-------------------------
SQL 2:
Then it gets a little trickier, using the statistic table above again and the table below called listing, I need to find the most visited category.
table called 'listing'
listingid | category
-------------------------
1 | beer
-------------------------
2 | beer
-------------------------
3 | cider
-------------------------
4 | spirits
-------------------------
5 | water
-------------------------
6 | juice
-------------------------
7 | wine
-------------------------
8 | coffee
-------------------------
9 | beer
-------------------------
10 | spirits
-------------------------
DESIRED RESULTS SQL.2
category | hits
-------------------------
beer | 3
-------------------------
spirits | 2
-------------------------
juice | 1
-------------------------
wine | 1
-------------------------
It's all probably very simple but I'm new to count statements in SQL. Please note that I'm running SQL Server 2005 so it must be SQL that is compatible with this.
Thanks!
Below is the example data in two tables, one called statistic and one called address. I need to write an SQL count statement that gives me the results below these two tables which is basically top visited cities in order of popularity.
table called 'statistic'
statisticid | addressid | listingid
--------------------------
1 | 1 | 9
--------------------------
2 | 3 | 2
--------------------------
3 | 1 | 10
--------------------------
4 | 3 | 4
--------------------------
5 | 2 | 6
--------------------------
6 | 1 | 1
--------------------------
7 | 1 | 7
--------------------------
table called 'address'
addressid | city |
-------------------------
1 | LONDON
-------------------------
2 | GLASGOW
-------------------------
3 | BELFAST
-------------------------
DESIRED RESULTS SQL.1
location | hits |
-------------------------
LONDON | 4
-------------------------
BELFAST | 2
-------------------------
GLASGOW | 1
-------------------------
SQL 2:
Then it gets a little trickier, using the statistic table above again and the table below called listing, I need to find the most visited category.
table called 'listing'
listingid | category
-------------------------
1 | beer
-------------------------
2 | beer
-------------------------
3 | cider
-------------------------
4 | spirits
-------------------------
5 | water
-------------------------
6 | juice
-------------------------
7 | wine
-------------------------
8 | coffee
-------------------------
9 | beer
-------------------------
10 | spirits
-------------------------
DESIRED RESULTS SQL.2
category | hits
-------------------------
beer | 3
-------------------------
spirits | 2
-------------------------
juice | 1
-------------------------
wine | 1
-------------------------
It's all probably very simple but I'm new to count statements in SQL. Please note that I'm running SQL Server 2005 so it must be SQL that is compatible with this.
Thanks!
To get them to sort most to least, add the following clause at the end of each statement:
ORDER BY COUNT(*) DESC
ORDER BY COUNT(*) DESC
1.
select city, count(*) as hits
from statistics s join address a on s.addressid = a.addressid
group by city
2.
select category, count(*) as hits
from listing
group by category
select city, count(*) as hits
from statistics s join address a on s.addressid = a.addressid
group by city
2.
select category, count(*) as hits
from listing
group by category
ASKER
the solution to SQL 1 is perfect but i messed up the SQL 2 question, sorry! I just need you to answer this and i'll send you the points! thanks guys...
SQL 2.
The statistic table has thousands of entries, every time a listing is viewed it is stored as a new row in the statistic table. So one listingid may occur on numerous rows, it may only have been viewed/stored one time, it's irrelevant. The point is, each listingid has a category assigned to it. I need to relate/join the statistic table to the listing table and I want to see which category has seen the most hits.
I hope that's clearer. Sorry again guys, you're answers were very fast and perfectly what i needed in SQL.1.
SQL 2.
The statistic table has thousands of entries, every time a listing is viewed it is stored as a new row in the statistic table. So one listingid may occur on numerous rows, it may only have been viewed/stored one time, it's irrelevant. The point is, each listingid has a category assigned to it. I need to relate/join the statistic table to the listing table and I want to see which category has seen the most hits.
I hope that's clearer. Sorry again guys, you're answers were very fast and perfectly what i needed in SQL.1.
tobzzz,
Sorry, still not clear on the second item. Can you work up an example, please? :)
Regards,
Patrick
Sorry, still not clear on the second item. Can you work up an example, please? :)
Regards,
Patrick
ASKER
OK, let me try again...
'statistic' table is populated by page views of an advert on a website. The table could look like this:
statisticid | advertid
----------------
1 | 9
----------------
2 | 2
----------------
3 | 10
----------------
4 | 4
----------------
5 | 6
----------------
6 | 1
----------------
7 | 7
----------------
'listing' table is the table of adverts, each row is unique with whatever the advertiser is selling but contains a column called 'category' which is one of five categories. So a the table could look like this:
advertid | category
-------------------------
1 | beer
-------------------------
2 | beer
-------------------------
3 | cider
-------------------------
4 | spirits
-------------------------
5 | water
-------------------------
6 | juice
-------------------------
7 | wine
-------------------------
8 | coffee
-------------------------
9 | beer
-------------------------
10 | spirits
-------------------------
We can see by eye from this table that 3 advertisers are selling beer. 2 are selling spirits. 1 is selling cider, 1 is selling water, 1 is selling juice and 1 is selling coffee but this is NOT what I am wanting to know.
What I want to know is, by relating the two tables together, what category has been VIEWED the most?
The desired result would be:
1st most viewed = beer ... 3 hits (in statistic number 1, 2 and 6)
2nd most viewed = spirits ... 2 hits (in statistic number 3 and 4)
joint 3rd most viewed = juice ... 1 hit (in statistic number 5)
joint 3rd most viewed = wine ... 1 hit (in statistic number 7)
cider, water and coffee saw NO views in the statistic table so they are not in the results.
I hope that makes sense! :@) Thanks Patrick
'statistic' table is populated by page views of an advert on a website. The table could look like this:
statisticid | advertid
----------------
1 | 9
----------------
2 | 2
----------------
3 | 10
----------------
4 | 4
----------------
5 | 6
----------------
6 | 1
----------------
7 | 7
----------------
'listing' table is the table of adverts, each row is unique with whatever the advertiser is selling but contains a column called 'category' which is one of five categories. So a the table could look like this:
advertid | category
-------------------------
1 | beer
-------------------------
2 | beer
-------------------------
3 | cider
-------------------------
4 | spirits
-------------------------
5 | water
-------------------------
6 | juice
-------------------------
7 | wine
-------------------------
8 | coffee
-------------------------
9 | beer
-------------------------
10 | spirits
-------------------------
We can see by eye from this table that 3 advertisers are selling beer. 2 are selling spirits. 1 is selling cider, 1 is selling water, 1 is selling juice and 1 is selling coffee but this is NOT what I am wanting to know.
What I want to know is, by relating the two tables together, what category has been VIEWED the most?
The desired result would be:
1st most viewed = beer ... 3 hits (in statistic number 1, 2 and 6)
2nd most viewed = spirits ... 2 hits (in statistic number 3 and 4)
joint 3rd most viewed = juice ... 1 hit (in statistic number 5)
joint 3rd most viewed = wine ... 1 hit (in statistic number 7)
cider, water and coffee saw NO views in the statistic table so they are not in the results.
I hope that makes sense! :@) Thanks Patrick
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect. I fully understand COUNT as I need it for my database. I added another 2 joins and a where statement using your principles and got exactly the result I needed. Thanks Patrick.
SELECT a.city, COUNT(*) AS Hits
FROM address a INNER JOIN
statistic s ON a.addressid = s.addressid
GROUP BY a.city
2)
SELECT l.category, COUNT(*) AS Hits
FROM listing l
GROUP BY l.category