Jump to content

Help to produce a slicker query......


mdemetri2

Recommended Posts

Hi, quite a novice at queries at the moment, am wanting to create summaries primarily based on counts (and in the future sums). The query below is a start at this:

 

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'm sure this can be made slicker (somehow) any advice? Also, at the moment I specifcy a region, but would like not to and have it provide the counts for each, but if I dont have the region it does come back with each region but the row counts are the same for each - just the count of all.

Link to comment
https://forums.phpfreaks.com/topic/268286-help-to-produce-a-slicker-query/
Share on other sites

Done using this in the end:

 

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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.