Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 29th, 2006, 11:45 PM
Registered User
 
Join Date: Mar 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Seemingly Complicated Query help needed

Hi, its my first post. Usually i wouldve worked out my problems on my own... but this time due to certain time constraints, i'm really on wits end....


I have three tables with their fields as follows(insignificant fields are left out here):

ZONE:
 - id

PROP:
 - id
 - type[string] < (either "typeA" or "typeB")

TAG:
 -id
 -propId
 -zoneId

The scenario is as follows: Each prop would be attached with a tag and can be found in any one zone at a time.

I need ONE SQL STATEMENT to generate a table as like the following sample:

ZoneID TypeACount TypeBCount TotalCount
------ ---------- ---------- ----------
101 9 5 14
102 3 7 10


It would have been simple if not for the required schema design.... help anyone?
 
Old March 30th, 2006, 04:42 AM
Authorized User
 
Join Date: Mar 2006
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try the following

Code:
Select
 Z.id ZoneID,
 count(case when p.type = 'Type A' then 1 else null end) TypeACount,
 count(case when p.type = 'Type B' then 1 else null end) TypeBCount,
 count(1) TotalCount
from
 Zone Z inner join Tag t on z.id = t.zoneid
 Inner join Prop p on z.propid = p.id
Group by
 z.id
 
Old March 30th, 2006, 05:57 AM
Registered User
 
Join Date: Mar 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

YOU ARE THE MAN!!
It worked good.
Thanks!



but here's another thing... if a zone has zer0 of typeA and typeB, the record for that zone wont be shown, is there something i can add for it to display '0' for typeACount and typeBCount instead of NOT showing up at all?

i'm working on it as well.

really.... thanks again

 
Old March 30th, 2006, 06:02 AM
Registered User
 
Join Date: Mar 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok, i changed the joins to left join instead of inner join. The zeroes would show up. But the TotalCount showed up a '1' instead of a 0

 
Old March 30th, 2006, 06:24 AM
Authorized User
 
Join Date: Mar 2006
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

you could change the total count logic to

Code:
count(case when p.type = 'Type A' or p.type = 'Type B' then 1 else null end) TotalCount
not really sure this will work.. if you could send me some sample data I could take a look at it for you

 
Old March 31st, 2006, 01:02 AM
Registered User
 
Join Date: Mar 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

once again thanks a million
you're great.
it worked totally now.

:') *sniff






Similar Threads
Thread Thread Starter Forum Replies Last Post
complicated if else statement help needed cmt9000 Classic ASP Basics 1 December 13th, 2006 06:34 PM
How do you query this complicated thing? sprion SQL Language 5 January 6th, 2005 02:06 AM
complicated query rajanikrishna Classic ASP Basics 4 June 17th, 2004 04:17 PM
Complicated Query roniestein Access 3 December 13th, 2003 10:34 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.