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 > Database Server Software > MySQL > Help with Group By Clause

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-25-10, 10:56
ozzii ozzii is offline
Registered User
 
Join Date: Mar 2007
Posts: 194
Help with Group By Clause

Hi,

am using the following SQL to see how many user I have from a particular location.

Code:
SELECT  l.location_id
	  , l.location_name
	  , l.location_parent_id
	  , l.location_root_id
	  , COUNT(u.user_id) AS num_users  
FROM tbl_locations as l
LEFT JOIN tbl_users AS u ON l.location_id = u.location_id 
GROUP BY l.location_id
ORDER BY l.location_id ASC;

For which i get the following results:

Code:
location_id  location_name  	location_parent_id  location_root_id  num_users
-------------------------------------------------------------------------------
1		UK			0		0		0
2 		East Anglia 		1 		1 		0
3 		Cambridgeshire 		2 		1 		0
4 		Norfolk 		2 		1 		0
5 		Suffolk 		2 		1 		0
6 		East Midlands 		1 		1 		0
7 		Derbyshire 		6 		1 		0
8 		Leicestershire 		6 		1 		0
9 		Lincolnshire 		6 		1 		0
10 		Northamptonshire 	6 		1 		0
11 		Nottinghamshire 	6 		1 		0
12 		Greater London 		1 		1 		0
13 		Central London 		12 		1 		50
14 		East London 		12 		1 		5
Since Central London and East London is part of Greater London how do I group the query so that It also shows 55 users for Greater London?
Reply With Quote
  #2 (permalink)  
Old 06-06-10, 07:51
ozzii ozzii is offline
Registered User
 
Join Date: Mar 2007
Posts: 194
Any one got any suggestion or even tips on this? Am still scratching my head to get it working!
Reply With Quote
  #3 (permalink)  
Old 06-07-10, 09:09
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
actually, this question was already asked and answered a little while back.
Dave
Reply With Quote
  #4 (permalink)  
Old 06-07-10, 13:14
ozzii ozzii is offline
Registered User
 
Join Date: Mar 2007
Posts: 194
Quote:
Originally Posted by dav1mo View Post
actually, this question was already asked and answered a little while back.
Dave
Was it? Dont suppose you have the link to it?
Reply With Quote
  #5 (permalink)  
Old 06-07-10, 14:16
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
nope, but start googling recursion or pivot for your answer.
Dave
Reply With Quote
  #6 (permalink)  
Old 06-20-10, 04:30
ozzii ozzii is offline
Registered User
 
Join Date: Mar 2007
Posts: 194
Quote:
Originally Posted by dav1mo View Post
nope, but start googling recursion or pivot for your answer.
Dave

I give up on this. I've spent hours on it - I dont think its possible
Reply With Quote
  #7 (permalink)  
Old 06-21-10, 03:47
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
It is definately possible. Are you saying that you want counts based on the 2nd level of the heirarchy?
__________________
George
Twitter | Blog
Reply With Quote
  #8 (permalink)  
Old 06-21-10, 12:41
ozzii ozzii is offline
Registered User
 
Join Date: Mar 2007
Posts: 194
Quote:
Originally Posted by gvee View Post
It is definately possible. Are you saying that you want counts based on the 2nd level of the heirarchy?
I need counts based on both the first and second level of the heirachy.

so if the user registered their location as Greater London instead of narowing it down to a specific area of London i.e. Central London.

the table would end up as follows:

Code:
location_id  location_name  	location_parent_id  location_root_id  num_users
-------------------------------------------------------------------------------
12 		Greater London 		1 		1 		1
13 		Central London 		12 		1 		50
14 		East London 		12 		1 		5
In the above instance I want the query to produce num_users for Greater London as 56.
Reply With Quote
  #9 (permalink)  
Old 06-22-10, 04:16
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Rats, just realised that this is MySQL, not SQL Server! I can do it in SQL Server but am not familiar with any recursion methods in MySQL, sorry
__________________
George
Twitter | Blog
Reply With Quote
  #10 (permalink)  
Old 06-22-10, 08:36
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
For just 2 levels and leaving out your master parent you could attempt something along the lines of:
Code:
select a.location_id,  a.location_name, a.location_parent_id
        ,a.location_root_id, value(sum(b.num_users),0) + a.num_users
    from my_table a
left outer join my_table b
   on a.location_id = b.location_parent_id
  and b.location_parent_id <> 1
I left off the main parent id of 1 as this query would not return the correct results for it as we are only getting the hierachy of 2 levels. I used a LEFT OUTER as a location id may not have a parent other than parent 1. This code is completely untested, but give it a go.
Dave
Reply With Quote
  #11 (permalink)  
Old 06-22-10, 08:38
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Also, I wanted to mention that you could try keeping this straight in the future by putting a trigger on the table to increment a counter on the parents' row. This way you could keep all of the parents in line with their children, to include you master parent id of 1(UK).
Dave
Reply With Quote
  #12 (permalink)  
Old 06-22-10, 08:47
sathkum2281 sathkum2281 is offline
Registered User
 
Join Date: Jun 2010
Posts: 4
You can't achieve it in single query, need to write either stored procedure in db side or iteration in programming language.

The query below will help to get total, one level up from last child,

"select l1.location_name,if(u1.num_users IS NULL,0,u1.num_users)+(select if(sum(u2.num_users) IS NULL,0,sum(u2.num_users)) from tbl_locations l2 left join tbl_users u2 on (l2.location_id = u2.location_id) where l2.location_parent_id = l1.location_id) as num_users from tbl_locations l1 left join tbl_users u1 on (l1.location_id = u1.location_id);"

...
Greater London 55
Central London 50
East London 5
...
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