Я создал это представление, которое мучительно медленно запрашивает и создает проблемы с производительностью.Я думаю, что мне нужно использовать PIVOT, но я не могу по-настоящему обдумать это ...
Вот что у меня сейчас есть.
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
Может кто-то, кто знает о SQLпожалуйста, скажите мне (покажите мне?), как я могу сделать это быстрее / менее отвратительно, пожалуйста.
Кроме того, эти 15 имен учетных записей жестко закодированы, в идеальном мире было бы получить название продукта из таблицы producttypes
Извините, что задаю такой раздражающий вопрос.
Спасибо!