Мой запрос выполняется слишком долго, и это может быть причиной 'Удаленный сервер возвратил ошибку: 401 не авторизован' ошибка. Никто другой из моей команды не получит эту ошибку, кроме меня. Мой запрос ниже.
FROM IncidentReview IR
Left join( Select Category='ADL', DONRFunction='Eating', IR.ASSESSID, Comment, LOI
,UMN
-- ,IRECENTDR = Row_Number() OVER(PARTITION BY IR.INCIDENTID, scaleid ORDER BY REVIEWDATE DESC, IR.ASSESSID DESC)
FROM IncidentReview IR
Left Join (SELECT ASSESSID, SCALE,
Comment = CASE WHEN CONVERT(VARCHAR(255),ITEM) = '' THEN 'NULL' ELSE CONVERT(VARCHAR(255),ITEM) END
FROM IncidentDetReview IDR Where IDR.SCALEID=30085729
)Comment on IR.ASSESSID = Comment.ASSESSID
Left Join (SELECT ASSESSID, SCALE, LOI= CASE WHEN CONVERT(VARCHAR,ITEM) = '' THEN NULL ELSE CONVERT(INT,LEFT (CONVERT(VARCHAR,ITEM),1)) END
-- ,IRECENTDR = Row_Number() OVER(PARTITION BY IR.INCIDENTID, scaleid ORDER BY REVIEWDATE DESC, IR.ASSESSID DESC)
FROM IncidentDetReview IDR Where IDR.SCALEID=30085779
)LOI on IR.ASSESSID = LOI.ASSESSID
Left Join (SELECT ASSESSID, SCALE, UMN=CASE WHEN CONVERT(VARCHAR,ITEM) = '' THEN NULL ELSE CONVERT(INT,LEFT (CONVERT(VARCHAR,ITEM),1)) END FROM IncidentDetReview IDR Where IDR.SCALEID=30085780
)UMN on IR.ASSESSID = UMN.ASSESSID
UNION ALL
Select Category='ADL', DONRFunction='Bathing', IR.ASSESSID, Comment, LOI, UMN
FROM IncidentReview IR
Left Join (SELECT ASSESSID, SCALE, Comment = CASE WHEN CONVERT(VARCHAR(255),ITEM) = '' THEN 'NULL' ELSE CONVERT(VARCHAR(255),ITEM) END /*Comment=Convert(varchar(255), ITEM)*/ FROM IncidentDetReview IDR Where IDR.SCALEID=30085733)Comment on IR.ASSESSID = Comment.ASSESSID
Left Join (SELECT ASSESSID, SCALE, LOI=CASE WHEN CONVERT(VARCHAR,ITEM) = '' THEN NULL ELSE CONVERT(INT,LEFT (CONVERT(VARCHAR,ITEM),1)) END FROM IncidentDetReview IDR Where IDR.SCALEID=30085781)LOI on IR.ASSESSID = LOI.ASSESSID
Left Join (SELECT ASSESSID, SCALE, UMN=CASE WHEN CONVERT(VARCHAR,ITEM) = '' THEN NULL ELSE CONVERT(INT,LEFT (CONVERT(VARCHAR,ITEM),1)) END FROM IncidentDetReview IDR Where IDR.SCALEID=30085782)UMN on IR.ASSESSID = UMN.ASSESSID
UNION ALL
SELECT Category = 'ADL', DONRFunction = 'Gromming',IR.ASSESSID,Comment,LOI,UMN
FROM INCIDENTREVIEW IR
LEFT JOIN (SELECT ASSESSID,SCALE,Comment = CASE WHEN CONVERT(VARCHAR(255),ITEM) = '' THEN 'NULL' ELSE CONVERT(VARCHAR(255),ITEM) END /*COMMENT = CONVERT(VARCHAR(255),ITEM)*/FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085732)COMMENT ON IR.ASSESSID = COMMENT.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,LOI = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085783)LOI ON IR.ASSESSID = LOI.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,UMN = CASE WHEN CONVERT(VARCHAR,ITEM) = '' THEN NULL ELSE CONVERT (INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM IncidentDetReview IDR WHERE IDR.SCALEID = 30085784)UMN ON IR.ASSESSID = UMN.ASSESSID
UNION ALL
SELECT CATEGORY = 'ADL',DONRFunction = 'Dressing',IR.ASSESSID,COMMENT,LOI,UMN
FROM INCIDENTREVIEW IR
LEFT JOIN (SELECT ASSESSID,SCALE,Comment = CASE WHEN CONVERT(VARCHAR(255),ITEM) = '' THEN 'NULL' ELSE CONVERT(VARCHAR(255),ITEM) END/*COMMENT= CONVERT(VARCHAR(255),ITEM)*/FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085738)COMMENT ON IR.ASSESSID = COMMENT.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,LOI = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085785)LOI ON IR.ASSESSID = LOI.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,UMN = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM IncidentDetReview IDR WHERE IDR.SCALEID = 30085786)UMN ON IR.ASSESSID = UMN.ASSESSID
UNION ALL
SELECT CATEGORY = 'ADL',DONRFunction = 'Transferring',IR.ASSESSID,COMMENT,LOI,UMN
FROM INCIDENTREVIEW IR
LEFT JOIN (SELECT ASSESSID,SCALE,Comment = CASE WHEN CONVERT(VARCHAR(255),ITEM) = '' THEN 'NULL' ELSE CONVERT(VARCHAR(255),ITEM) END/*COMMENT= CONVERT(VARCHAR(255),ITEM)*/FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085734)COMMENT ON IR.ASSESSID = COMMENT.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,LOI = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085787)LOI ON IR.ASSESSID = LOI.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,UMN = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM IncidentDetReview IDR WHERE IDR.SCALEID = 30085788)UMN ON IR.ASSESSID = UMN.ASSESSID
UNION ALL
SELECT CATEGORY = 'ADL',DONRFunction = 'Contience',IR.ASSESSID,COMMENT,LOI,UMN
FROM INCIDENTREVIEW IR
LEFT JOIN (SELECT ASSESSID,SCALE,Comment = CASE WHEN CONVERT(VARCHAR(255),ITEM) = '' THEN 'NULL' ELSE CONVERT(VARCHAR(255),ITEM) END/*COMMENT= CONVERT(VARCHAR(255),ITEM)*/FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085728)COMMENT ON IR.ASSESSID = COMMENT.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,LOI = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085789)LOI ON IR.ASSESSID = LOI.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,UMN = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM IncidentDetReview IDR WHERE IDR.SCALEID = 30085790)UMN ON IR.ASSESSID = UMN.ASSESSID
UNION ALL
SELECT CATEGORY = 'IADL',DONRFunction = 'ManagingMoney',IR.ASSESSID,COMMENT,LOI,UMN
FROM INCIDENTREVIEW IR
LEFT JOIN (SELECT ASSESSID,SCALE,Comment = CASE WHEN CONVERT(VARCHAR(255),ITEM) = '' THEN 'NULL' ELSE CONVERT(VARCHAR(255),ITEM) END/*COMMENT= CONVERT(VARCHAR(255),ITEM)*/FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085730)COMMENT ON IR.ASSESSID = COMMENT.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,LOI = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085791)LOI ON IR.ASSESSID = LOI.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,UMN = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM IncidentDetReview IDR WHERE IDR.SCALEID = 30085792)UMN ON IR.ASSESSID = UMN.ASSESSID
UNION ALL
SELECT CATEGORY = 'IADL',DONRFunction = 'Telephone',IR.ASSESSID,COMMENT,LOI,UMN
FROM INCIDENTREVIEW IR
LEFT JOIN (SELECT ASSESSID,SCALE,Comment = CASE WHEN CONVERT(VARCHAR(255),ITEM) = '' THEN 'NULL' ELSE CONVERT(VARCHAR(255),ITEM) END /*COMMENT= CONVERT(VARCHAR(255),ITEM)*/FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085731)COMMENT ON IR.ASSESSID = COMMENT.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,LOI = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085793)LOI ON IR.ASSESSID = LOI.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,UMN = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM IncidentDetReview IDR WHERE IDR.SCALEID = 30085794)UMN ON IR.ASSESSID = UMN.ASSESSID
UNION ALL
SELECT CATEGORY = 'IADL',DONRFunction = 'PreparingMeal',IR.ASSESSID,COMMENT,LOI,UMN
FROM INCIDENTREVIEW IR
LEFT JOIN (SELECT ASSESSID,SCALE,Comment = CASE WHEN CONVERT(VARCHAR(255),ITEM) = '' THEN 'NULL' ELSE CONVERT(VARCHAR(255),ITEM) END/*COMMENT= CONVERT(VARCHAR(255),ITEM)*/FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085737)COMMENT ON IR.ASSESSID = COMMENT.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,LOI = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085795)LOI ON IR.ASSESSID = LOI.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,UMN = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM IncidentDetReview IDR WHERE IDR.SCALEID = 30085796)UMN ON IR.ASSESSID = UMN.ASSESSID
UNION ALL
SELECT CATEGORY = 'IADL',DONRFunction = 'Laundry',IR.ASSESSID,COMMENT,LOI,UMN
FROM INCIDENTREVIEW IR
LEFT JOIN (SELECT ASSESSID,SCALE,Comment = CASE WHEN CONVERT(VARCHAR(255),ITEM) = '' THEN 'NULL' ELSE CONVERT(VARCHAR(255),ITEM) END/*COMMENT= CONVERT(VARCHAR(255),ITEM)*/FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085796)COMMENT ON IR.ASSESSID = COMMENT.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,LOI = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085797)LOI ON IR.ASSESSID = LOI.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,UMN = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM IncidentDetReview IDR WHERE IDR.SCALEID = 30085798)UMN ON IR.ASSESSID = UMN.ASSESSID
UNION ALL
SELECT CATEGORY = 'IADL',DONRFunction = 'HouseWork',IR.ASSESSID,COMMENT,LOI,UMN
FROM INCIDENTREVIEW IR
LEFT JOIN (SELECT ASSESSID,SCALE,Comment = CASE WHEN CONVERT(VARCHAR(255),ITEM) = '' THEN 'NULL' ELSE CONVERT(VARCHAR(255),ITEM) END/*COMMENT= CONVERT(VARCHAR(255),ITEM)*/FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085735)COMMENT ON IR.ASSESSID = COMMENT.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,LOI = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085799)LOI ON IR.ASSESSID = LOI.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,UMN = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM IncidentDetReview IDR WHERE IDR.SCALEID = 30085800)UMN ON IR.ASSESSID = UMN.ASSESSID
UNION ALL
SELECT CATEGORY = 'IADL',DONRFunction = 'Outside Home',IR.ASSESSID,COMMENT,LOI,UMN
FROM INCIDENTREVIEW IR
LEFT JOIN (SELECT ASSESSID,SCALE,Comment = CASE WHEN CONVERT(VARCHAR(255),ITEM) = '' THEN 'NULL' ELSE CONVERT(VARCHAR(255),ITEM) END /*COMMENT= CONVERT(VARCHAR(255),ITEM)*/FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085740)COMMENT ON IR.ASSESSID = COMMENT.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,LOI = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085801)LOI ON IR.ASSESSID = LOI.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,UMN = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM IncidentDetReview IDR WHERE IDR.SCALEID = 30085803)UMN ON IR.ASSESSID = UMN.ASSESSID
UNION ALL
SELECT CATEGORY = 'IADL',DONRFunction = 'Routine Health',IR.ASSESSID,COMMENT,LOI,UMN
FROM INCIDENTREVIEW IR
LEFT JOIN (SELECT ASSESSID,SCALE,Comment = CASE WHEN CONVERT(VARCHAR(255),ITEM) = '' THEN 'NULL' ELSE CONVERT(VARCHAR(255),ITEM) END/*COMMENT= CONVERT(VARCHAR(255),ITEM)*/FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085742)COMMENT ON IR.ASSESSID = COMMENT.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,LOI = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085802)LOI ON IR.ASSESSID = LOI.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,UMN = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM IncidentDetReview IDR WHERE IDR.SCALEID = 30085804)UMN ON IR.ASSESSID = UMN.ASSESSID
UNION ALL
SELECT CATEGORY = 'IADL',DONRFunction = 'SpecialHealth',IR.ASSESSID,COMMENT,LOI,UMN
FROM INCIDENTREVIEW IR
LEFT JOIN (SELECT ASSESSID,SCALE,Comment = CASE WHEN CONVERT(VARCHAR(255),ITEM) = '' THEN 'NULL' ELSE CONVERT(VARCHAR(255),ITEM) END/*COMMENT= CONVERT(VARCHAR(255),ITEM)*/FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085741)COMMENT ON IR.ASSESSID = COMMENT.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,LOI = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085805)LOI ON IR.ASSESSID = LOI.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,UMN = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM IncidentDetReview IDR WHERE IDR.SCALEID = 30085806)UMN ON IR.ASSESSID = UMN.ASSESSID
UNION ALL
SELECT CATEGORY = 'IADL',DONRFunction = 'BeingAlone',IR.ASSESSID,COMMENT,LOI,UMN
FROM INCIDENTREVIEW IR
LEFT JOIN (SELECT ASSESSID,SCALE,Comment = CASE WHEN CONVERT(VARCHAR(255),ITEM) = '' THEN 'NULL' ELSE CONVERT(VARCHAR(255),ITEM) END/*COMMENT= CONVERT(VARCHAR(255),ITEM)*/FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085739)COMMENT ON IR.ASSESSID = COMMENT.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,LOI = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM INCIDENTDETREVIEW IDR WHERE IDR.SCALEID = 30085807)LOI ON IR.ASSESSID = LOI.ASSESSID
LEFT JOIN (SELECT ASSESSID,SCALE,UMN = CASE WHEN CONVERT(VARCHAR,ITEM)= '' THEN NULL ELSE CONVERT(INT,LEFT(CONVERT(VARCHAR,ITEM),1))END FROM IncidentDetReview IDR WHERE IDR.SCALEID = 30085808)UMN ON IR.ASSESSID = UMN.ASSESSID
)DONR on IR.ASSESSID = DONR.ASSESSID