50,230 Members
3 added today
250,691 Resources
51 added today

All Devdex   All SQLdex   Current Category
SQLdex > Forums & Newsgroups > Newsgroups > microsoft.public.sqlserver.programming Add this category to My Favorites

View Message Thread  (7 replies)

Results 1 - 8 of 8

GROUP BY with join help please! Add this thread to My Favorites
From: Unknown User
Date Posted: 7/14/2010 8:07:00 AM

Hello everyone, I have a vexing problem that I am trying to solve
without resorting to something ugly like a cursor or a temp table....

(DDL and sample data below)

I have a table, Trees. Each tree has a TreeID (unique PK), a Species,
a Quality, and a related Sector. Each Sector in the Sectors table has
an Area in hectares.

I need a query to give me Species, Quality, Number of Trees, and
Number of Trees/Hectare for each (Species,Quality) combination.

Of course I can GROUP BY Species, Quality just to get the first three.
But I cannot get my head round how to join and get the area, and use
it to calculate the trees/unit area, without it being in the GROUP BY?
I am really trying to avoid having to resort to temp tables or worse
still a cursor. this mus be easy but I cannot see the wood for the
trees, ha ha! anyone care to point me up the right tree? (sorry, I'l
stop there)


create table Trees
(
TreeId int primary key
,SectorId int not null
,Species tinyint not null
,Quality char(1) not null
)

create table Sectors
(SectorId int primary key
,Area float not null
)

alter table Trees add constraint FK_1 foreign key (SectorId)
references Sector(SectorID)

create index idx_1 on Trees(SectorId)

--sample data:
insert into Sectors (SectorId, Area)
Values
,(1,5.9)
,(2,2.2)
,(3,3.2)
,(4,0.8)
,(5,3.5)

insert into Trees (TreeId, SectorId, Species, Quality)
values
(1,1,4,a)
,(2,2,3,b)
,(3,1,4,a)
,(4,1,4,b)
,(5,1,4,c)
,(6,1,5,a)
,(7,1,5,b)
,(8,1,5,a)
,(9,2,3,a)
,(10,2,3,b)
,(11,2,3,a)
,(12,2,3,b)
,(13,3,1,a)
,(14,3,1,a)
,(15,3,1,b)
,(16,3,1,c)
,(17,3,1,d)
,(18,3,1,a)
,(19,3,2,a)
,(20,3,2,b)
,(21,3,2,b)


--not right -- don't want to GROUP BY area, but how can I do this?!!
select t.Species, t.Quality, count(TreeId) as number, count(TreeId)/
s.Area as 'NumberPerHectare'
from Trees t inner join
Sectors s
on t.SectorId = s.SectorId
group by t.Species, t.Quality, s.Area

Re: GROUP BY with join help please!
From: Unknown User
Date Posted: 7/14/2010 8:15:00 AM

Sorry, corrected sample data for trees with quoted character data:-

insert into Trees (TreeId, SectorId, Species, Quality)
values
(1,1,4,'a')
,(2,2,3,'b')
,(3,1,4,'a')
,(4,1,4,'b')
,(5,1,4,'c')
,(6,1,5,'a')
,(7,1,5,'b')
,(8,1,5,'a')
,(9,2,3,'a')
,(10,2,3,'b')
,(11,2,3,'a')
,(12,2,3,'b')
,(13,3,1,'a')
,(14,3,1,'a')
,(15,3,1,'b')
,(16,3,1,'c')
,(17,3,1,'d')
,(18,3,1,'a')
,(19,3,2,'a')
,(20,3,2,'b')
,(21,3,2,'b')

Re: GROUP BY with join help please!
From: Tom Cooper
Date Posted: 7/14/2010 2:05:00 PM

If I understand what you want, just use the sum of the areas, that is

select t.Species, t.Quality, count(TreeId) as number,
  count(TreeId)/Sum(s.Area) as 'NumberPerHectare'
from Trees t inner join
Sectors s
on t.SectorId = s.SectorId
group by t.Species, t.Quality

Tom

<thomaspullen@hotmail.co.uk> wrote in message
news:56acb8ff-db36-4663-9523-3601143d70c3@j4g2000yqh.googlegroups.com...
> Sorry, corrected sample data for trees with quoted character data:-
>
> insert into Trees (TreeId, SectorId, Species, Quality)
> values
> (1,1,4,'a')
> ,(2,2,3,'b')
> ,(3,1,4,'a')
> ,(4,1,4,'b')
> ,(5,1,4,'c')
> ,(6,1,5,'a')
> ,(7,1,5,'b')
> ,(8,1,5,'a')
> ,(9,2,3,'a')
> ,(10,2,3,'b')
> ,(11,2,3,'a')
> ,(12,2,3,'b')
> ,(13,3,1,'a')
> ,(14,3,1,'a')
> ,(15,3,1,'b')
> ,(16,3,1,'c')
> ,(17,3,1,'d')
> ,(18,3,1,'a')
> ,(19,3,2,'a')
> ,(20,3,2,'b')
> ,(21,3,2,'b')

Re: GROUP BY with join help please!
From: thomarse
Date Posted: 7/14/2010 10:23:00 PM

Thanks Tom, but that's the problem. I don't want to sum the area.

I have had another think about this and it may be that the count is
only ever for 1 sector so I can possibly divide by a constant instead
of a column value.

Thanks for responding anyhow!

Re: GROUP BY with join help please!
From: Tom Cooper
Date Posted: 7/15/2010 2:17:00 AM

Sorry I wasn't more help. Could you show the result you want from the query
given the sample data you provided? That should help us find the query you
are looking for.

Tom

"thomarse" <tpullen@rm.com> wrote in message
news:007cb456-2a6e-445e-9809-70a364049b47@5g2000yqz.googlegroups.com...
> Thanks Tom, but that's the problem. I don't want to sum the area.
>
> I have had another think about this and it may be that the count is
> only ever for 1 sector so I can possibly divide by a constant instead
> of a column value.
>
> Thanks for responding anyhow!

Re: GROUP BY with join help please!
From: Unknown User
Date Posted: 7/15/2010 3:46:00 AM

Tom, yes I will do that shortly. I think I may need to use a CTE and
OVER etc to partition the data then count by sector so I can do the
maths. Essentially I need to sum all the trees for each sector to work
out their density (trees/hectare), then sum all the trees for each
(Species, Quality) combination and then work out the total trees/
hectare for each of these, probably by averaging the density for each
of the sectors in which they're been counted in. I will post expected
results when I've figured them out. Thank you for your time and help
so far.

Tom Pullen

Re: GROUP BY with join help please!
From: Iain Sharp
Date Posted: 7/15/2010 6:10:00 AM

On Wed, 14 Jul 2010 07:05:02 -0700 (PDT), thomaspullen@hotmail.co.uk
wrote:

>--not right -- don't want to GROUP BY area, but how can I do this?!!
>select t.Species, t.Quality, count(TreeId) as number, count(TreeId)/
>s.Area as 'NumberPerHectare'
>from Trees t inner join
>Sectors s
>on t.SectorId = s.SectorId
>group by t.Species, t.Quality, s.Area


Hmm try this, (entirely untested)

select t.Species, t.Quality, count(TreeId) as number, count(TreeId)/
y.Area as 'NumberPerHectare'
from Trees t
-- Subqueries to determine total area of sectors with these trees in.
inner join
-- Subquery x gets total area of all sectors containing trees/quals.
(select x.species, x.quality, sum(x.Area) as Area
from
--Subquery y gets areas with these trees/qualities in them
( select distinct t.species, t.quality, t.area
from trees t inner join
Sectors s
on t.SectorId = s.SectorId) as y
group by y.species, y.quality ) as x on x.species = t.species and
x.quality = t.quality
group by t.Species, t.Quality

Iain

Re: GROUP BY with join help please!
From: Thomas
Date Posted: 7/16/2010 9:31:00 AM

I couldn't get that to work, eventually I ended re-writing it as

select t.Species, t.Quality, count(TreeId) as number, count(TreeId)/
Area as 'NumberPerHectare'
from Trees t inner join
(
select y.Species, y.Quality, sum(y.Area) as Area
from
(
select distinct t.Species, t.Quality, S.Area
from Trees t inner join
Sectors s
on t.SectorId = s.SectorId
) as y
group by y.Species, y.Quality
) as x on x.Species = t.Species and
x.Quality = t.Quality
group by t.Species, t.Quality

which is back to the same old error which prompted the post in the
first place

Msg 8120, Level 16, State 1, Line 1
Column 'x.Area' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.

I am trying to get it to work using OVER .. PARTITION BY but it's
addling my poor little brain. Will post back if I crack it.

Results 1 - 8 of 8

 

Would you like to track this thread?

By adding this News Thread to your Favorites Area you can refer to it later with just a click of the mouse. Also you can optionally be notified instantly whenever there are any replies posted to this Thread. To add this Thread to your Favorites Area just click on the red arrow next to the subject of the thread above Add this thread to My Favorites.



ASP.NET Chart Control.
Charts from your Database.
Live Demo & FREE Trial!

ASP ArticlesThis category has been added to your weekly newsletter
ASP Web Sites
ADSI & WSH BooksThis category has been added to your weekly newsletter
FREE ComponentsThis category has been added to your weekly newsletter
ASP EventsThis category has been added to your weekly newsletter
ASP HeadlinesThis category has been added to your weekly newsletter

CSharp ArticlesThis category has been added to your weekly newsletter
C# Web SitesThis category has been added to your weekly newsletter

SQL ArticlesThis category has been added to your weekly newsletter
SQL Events
SQL HeadlinesThis category has been added to your weekly newsletter
SQL Jobs

Jobs in CaliforniaThis category has been added to your weekly newsletter

XML ArticlesThis category has been added to your weekly newsletter
XML BooksThis category has been added to your weekly newsletter
XML Web Sites
XML Tutorials

free asp host

"Alex Homer"This search has been added to your weekly newsletter

Edit My Favorites Edit Profile & Favorites

 

 




Developersdex Home | ASP | C# | SQL | VB | XML | Gurus
Add Your Link | Add Your Code | FAQ | Advertise | Link To Us | Contact Us |
Copyright © 2010 Developersdex™. All rights reserved.