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
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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.