mdemetri2 Posted September 12, 2012 Share Posted September 12, 2012 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 12, 2012 Share Posted September 12, 2012 Joins and Group By Quote Link to comment Share on other sites More sharing options...
Barand Posted September 12, 2012 Share Posted September 12, 2012 and type != 'colo' or 'Data Centre' Wrong syntax. AND type NOT IN ('colo', 'Data Centre') Quote Link to comment Share on other sites More sharing options...
mdemetri2 Posted September 13, 2012 Author Share Posted September 13, 2012 Jesirose - ok, but in any particular way, so instead of using 'where' do a join..... Barand - thanks, it worked even with !=, but will use NOT IN.... Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 13, 2012 Share Posted September 13, 2012 The or 'DataCenter' line does nothing. it doesn't "work" it just didn't throw up an error. Quote Link to comment Share on other sites More sharing options...
mdemetri2 Posted September 18, 2012 Author Share Posted September 18, 2012 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; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.