MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 13th, 2012, 03:50 AM
mdemetri2 mdemetri2 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 13 mdemetri2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 57 m 12 sec
Reputation Power: 0
Making query slicker - help / advice?

Hi can anyone help in me making this a better query:

select distinct Region,
(SELECT count(*)
FROM critical_risks
WHERE propertyref in (select propertyref from locations where region = 'GB' and type != 'colo' or 'Data Centre') and classification in (2,3,4,5)) as IR,
(SELECT count(*)
FROM critical_risks
WHERE propertyref in (select propertyref from locations where region = 'GB'and type != 'colo' or 'Data Centre') and classification in (7,8,9,10)) as OP,
(SELECT count(*)
FROM critical_risks
WHERE propertyref in (select propertyref from locations where region = 'GB'and type != 'colo' or 'Data Centre') and classification in (12,13)) as MT,
(SELECT count(*)
FROM critical_risks
WHERE propertyref in (select propertyref from locations where region = 'GB'and type != 'colo' or 'Data Centre') and classification in (15,16,17)) as C,
(SELECT count(*)
FROM critical_risks
WHERE propertyref in (select propertyref from locations where region = 'GB'and type != 'colo' or 'Data Centre') and classification in (2,3,4,5,7,8,9,10,12,13,15,16,17)) as TotalRisks
from locations where region = 'GB';


I would actually like to not specifiy a region, but when I dont put it in although I get all the regions back the row counts are the same for each - bascially the totals. I know I just need to join and group some other way but not sure how or what!.

Thanks

Reply With Quote
  #2  
Old September 13th, 2012, 09:10 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,349 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 7 h 17 m 46 sec
Reputation Power: 390
Somewhat like

Code:
select locations.Region,
       sum(case when critical_risks.classification in (2,3,4,5) then 1 else 0 end) as IR,
       sum(case when critical_risks.classification in (7,8,9,10) then 1 else 0 end) as OP,
       sum(case when critical_risks.classification in (12,13) then 1 else 0 end) as MT,
       sum(case when critical_risks.classification in (15,16,17) then 1 else 0 end) as C,
       count(*)
  from locations
  join critical_risks
    on locations.propertyref = critical_risks. propertyref
   and critical_risks.classification in (2,3,4,5,7,8,9,10,12,13,15,16,17)
 where locations.type not in ('colo','Data Centre')
 group
    by locations.region


The expression

Code:
and type != 'colo' or 'Data Centre'


as stated, does not make any sense. I assume that you wish to disqualify those two types from the result.

Reply With Quote
  #3  
Old September 13th, 2012, 10:38 AM
mdemetri2 mdemetri2 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 13 mdemetri2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 57 m 12 sec
Reputation Power: 0
Hi swampBoogie, I will give that a go and see what happens, thank you.

In terms of the expression '!=' I wrote that to mean not equal to, your assumption is correct. In MySQL Query browser it works.....

Reply With Quote
  #4  
Old September 14th, 2012, 09:01 AM
sr sr is offline
Problem Solver
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Jan 2001
Location: Stockholm, Sweden
Posts: 4,428 sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 7 h 17 m 23 sec
Reputation Power: 532
Quote:
Originally Posted by mdemetri2
...
In MySQL Query browser it works.....

It doesn't work, MySQL just doesn't report an error.
And in this case the effect of this condition in the query is zilch.
The query will return all rows all the time.

Basically in MySQL a zero length string is interpreted as False and anything else is True.

So you have written:
Code:
return all rows where:
type not equals 'colo' (is either true or false)
OR 'someString' (is the same as 1=1, i.e is always true)

and since the second statement is always true all rows will always be returned from this condition regardless of if the first statement was true or false.
__________________
/Stefan

Reply With Quote
  #5  
Old September 20th, 2012, 07:10 AM
mdemetri2 mdemetri2 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 13 mdemetri2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 57 m 12 sec
Reputation Power: 0
Thanks swampBoogie, you are a star!

I have been trying to amend this somewhat, and although the IR, OR, MT and C split are ok, what if for each of those I wanted to do sum of a field estCost rather than count how many IR etc there are?

So I would for each category (IR, OR, MT and C) get the sum of field estCost....

I can crudley do it specifying only one classification:

select sum(estcost) from critical_risks where classification = 2 and propertyref in (select propertyref from locations where type not in ('colo','Data Centre'));

Any advice appreciated. I will continue to look at using the case when statement also......Cheers!

Reply With Quote
  #6  
Old September 20th, 2012, 07:28 AM
mdemetri2 mdemetri2 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 13 mdemetri2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 57 m 12 sec
Reputation Power: 0
How about this, is this right?

select locations.region,
sum(case when classification in (2,3,4,5) then estCost else 0 end) as IR_EstCost,
sum(case when classification in (7,8,9,10) then estCost else 0 end) as OP_EstCost,
sum(case when classification in (12,13) then estCost else 0 end) as MT_EstCost,
sum(case when classification in (15,16,17) then estCost else 0 end) as C_EstCost
from locations
join critical_risks on locations.propertyref = critical_risks.propertyref
where locations.type not in ('colo','Data Centre')
group by locations.region;

Reply With Quote
  #7  
Old October 2nd, 2012, 03:19 AM
mdemetri2 mdemetri2 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 13 mdemetri2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 57 m 12 sec
Reputation Power: 0
Hello, anyone?

I think I have kind of got the gistm but wanted some steer.

Reply With Quote
  #8  
Old October 2nd, 2012, 04:10 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
Looks pretty good to me

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Making query slicker - help / advice?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap