I've created this view that is painfully slow to query and creating performance issues. I think I need to utilize PIVOT but I can't really wrap my head around it...

Here's what I've got currently.

select
    p.instrumentsettingsid,
    sum(case when p1.id is null then 0 else 1 end) as 'Cash',
    sum(case when p2.id is null then 0 else 1 end) as HighInterest,
    sum(case when p3.id is null then 0 else 1 end) as 'ML Loan',
    sum(case when p4.id is null then 0 else 1 end) as 'Investment Note',
    sum(case when p5.id is null then 0 else 1 end) as 'Trading',
    sum(case when p6.id is null then 0 else 1 end) as 'DD Trading',
    sum(case when p7.id is null then 0 else 1 end) as 'ML Trading',
    sum(case when p8.id is null then 0 else 1 end) as 'NMLTrading',
    sum(case when p9.id is null then 0 else 1 end) as 'Individual Custody',
    sum(case when p10.id is null then 0 else 1 end) as 'ML Individual Custody',
    sum(case when p11.id is null then 0 else 1 end) as 'Custody',
    sum(case when p12.id is null then 0 else 1 end) as 'ML Custody Trading',
    sum(case when p13.id is null then 0 else 1 end) as 'Portfolio',
    sum(case when p14.id is null then 0 else 1 end) as 'ML Personal Portfolio',
    sum(case when p15.id is null then 0 else 1 end) as Other,
    i.status
from
    (select distinct instrumentsettingsid from res_db..instrumentproducttypepermissions) p
    inner join res_db..instrumentsettings i on p.instrumentsettingsid = i.id
    left join res_db..instrumentproducttypepermissions p1 on p.instrumentsettingsid = p1.instrumentsettingsid and p1.enabled = 1 and p1.producttypeid = 1
    left join res_db..instrumentproducttypepermissions p2 on p.instrumentsettingsid = p2.instrumentsettingsid and p2.enabled = 1 and p2.producttypeid = 2
    left join res_db..instrumentproducttypepermissions p3 on p.instrumentsettingsid = p3.instrumentsettingsid and p3.enabled = 1 and p3.producttypeid = 3
    left join res_db..instrumentproducttypepermissions p4 on p.instrumentsettingsid = p4.instrumentsettingsid and p4.enabled = 1 and p4.producttypeid = 4
    left join res_db..instrumentproducttypepermissions p5 on p.instrumentsettingsid = p5.instrumentsettingsid and p5.enabled = 1 and p5.producttypeid = 5
    left join res_db..instrumentproducttypepermissions p6 on p.instrumentsettingsid = p6.instrumentsettingsid and p6.enabled = 1 and p6.producttypeid = 6
    left join res_db..instrumentproducttypepermissions p7 on p.instrumentsettingsid = p7.instrumentsettingsid and p7.enabled = 1 and p7.producttypeid = 7
    left join res_db..instrumentproducttypepermissions p8 on p.instrumentsettingsid = p8.instrumentsettingsid and p8.enabled = 1 and p8.producttypeid = 8
    left join res_db..instrumentproducttypepermissions p9 on p.instrumentsettingsid = p9.instrumentsettingsid and p9.enabled = 1 and p9.producttypeid = 9
    left join res_db..instrumentproducttypepermissions p10 on p.instrumentsettingsid = p10.instrumentsettingsid and p10.enabled = 1 and p10.producttypeid = 10
    left join res_db..instrumentproducttypepermissions p11 on p.instrumentsettingsid = p11.instrumentsettingsid and p11.enabled = 1 and p11.producttypeid = 11
    left join res_db..instrumentproducttypepermissions p12 on p.instrumentsettingsid = p12.instrumentsettingsid and p12.enabled = 1 and p12.producttypeid = 12
    left join res_db..instrumentproducttypepermissions p13 on p.instrumentsettingsid = p13.instrumentsettingsid and p13.enabled = 1 and p13.producttypeid = 13
    left join res_db..instrumentproducttypepermissions p14 on p.instrumentsettingsid = p14.instrumentsettingsid and p14.enabled = 1 and p14.producttypeid = 14
    left join res_db..instrumentproducttypepermissions p15 on p.instrumentsettingsid = p15.instrumentsettingsid and p15.enabled = 1 and p15.producttypeid = 15
    group by p.instrumentsettingsid, i.status


Can someone who knows about SQL please tell me (show me?) how I can make this faster/less hideous please.

Also those 15 account names are hardcoded, in a perfect world it would get the product name from the table producttypes

Sorry to ask such an annoying question.

Thank you!


Question: When enumerating motivations, is it correct to write firstly, secondly, ..., or first, second, ...?
Answer: Both styles are used. In most genres, no-one will object to either. However, traditionally, first, secondly, thirdly etc. is used. Only pedants will insist on this usage, but it is something to be aware of, as there are many pedants. See Fowler's Modern English Usage (3rd edition).

The Oxford English Dictionary on firstly:


  Used only in enumerating heads, topics, etc. in discourse; and many writers prefer first, even though closely followed by secondly, thirdly, etc.


Burchfield in Fowler's Modern English Usage on first: