TSQL Как управлять поиском или обработкой дубликатов - PullRequest
0 голосов
/ 21 октября 2019

У меня есть запрос, который возвращает дубликаты - как я могу изменить запрос, чтобы предотвратить это или установить исправление, чтобы этого не произошло. Я пытался использовать различные, но безрезультатно.

Это запрос

select sigfind.*
from [RM-JOB] 
inner join  (
  select [JOB-NO], [LINK-JOB-NO] 
  from [RM-LINK-JOBS] 
  where [RM-LINK-JOBS].[reason-code] = 'FRA' 
) linkedjob on [RM-JOB].[JOB-NO] = linkedjob.[JOB-NO] 
inner join [RM-JOB] [RMLinkedJob] on linkedjob.[LINK-JOB-NO] = [RMLinkedJob].[JOB-NO] 
                                and [linkedjob].[JOB-NO] = [RM-JOB].[JOB-NO]
inner join (
  SELECT ar.JobNo, ar.[ActionRef], AR.ActionStatus, AR.ActionDueDate [LINKED VISIT TARGET DATE], ar.SignificantFindings [SignificantFindings], ar.SyncDate, ar.dateFirRiskIdentified [Date of FRA] , ard.caseref [Linked Case Number]
  FROM [CYHSQL01].[TM_FireRiskAssessment].[dbo].[ActionRequired] ar
  inner join [CYHSQL01].[TM_FireRiskAssessment].[dbo].[ActionRequireddetails] ard
        on AR.ResultID = ARD.ResultID and ard.actionref = ar.[ActionRef]
) sigfind on sigfind.JobNo = linkedjob.[JOB-NO]
where [RMLinkedJob].[JOB-TYPE] in ('FSRE') 
         and [RM-JOB].[JOB-STATUS] in ('06','90')

Это то, что вывод;

JobNo       ActionRef   ActionStatu LINKED VISIT                                                   TARGET DATE  SignificantFindings SyncDate    Date of FRA Linked Case Number
10265985    CP1 Closed  2019-01-18  Shoe cabinet is in the communal hallway door is a potential trip hazard when evacuating.    2019-10-18 11:11:41.457 2019-10-18  NULL
10265985    CP1 Closed  2019-01-18  Shoe cabinet is in the communal hallway door is a potential trip hazard when evacuating.    2019-10-18 11:11:41.457 2019-10-18  NULL
10265985    CP2 Closed  2019-10-25  Large screen tv has been fly tipped in the communal hallway.    2019-10-18 11:11:41.460 2019-10-18  NULL
10265985    CP2 Closed  2019-10-25  Large screen tv has been fly tipped in the communal hallway.    2019-10-18 11:11:41.460 2019-10-18  NULL
10265985    CP1 Closed  2019-01-18  Shoe cabinet is in the communal hallway door is a potential trip hazard when evacuating.    2019-10-18 11:11:41.457 2019-10-18  NULL
10265985    CP1 Closed  2019-01-18  Shoe cabinet is in the communal hallway door is a potential trip hazard when evacuating.    2019-10-18 11:11:41.457 2019-10-18  NULL
10265985    CP1 Closed  2019-01-18  Shoe cabinet is in the communal hallway door is a potential trip hazard when evacuating.    2019-10-18 11:11:41.457 2019-10-18  NULL
10265985    CP1 Closed  2019-01-18  Shoe cabinet is in the communal hallway door is a potential trip hazard when evacuating.    2019-10-18 11:11:41.457 2019-10-18  NULL
10265985    CP1 Closed  2019-01-18  Shoe cabinet is in the communal hallway door is a potential trip hazard when evacuating.    2019-10-18 11:11:41.457 2019-10-18  NULL
10265985    CP1 Closed  2019-01-18  Shoe cabinet is in the communal hallway door is a potential trip hazard when evacuating.    2019-10-18 11:11:41.457 2019-10-18  NULL
10265986    CP1 Closed  2019-10-28  There is a 5 litre petrol can stored under the stairs. This must be removed as a matter of urgency as it’s potentially highly flammable.    2019-10-21 13:05:56.533 2019-10-21  NULL
10265986    CP1 Closed  2019-10-28  There is a 5 litre petrol can stored under the stairs. This must be removed as a matter of urgency as it’s potentially highly flammable.    2019-10-21 13:05:56.533 2019-10-21  NULL
10265986    CP2 Open    2019-11-18  Shoe rack is being used in the communal area. This is a potential trip hazard in an evacuation situation.   2019-10-21 13:05:56.543 2019-10-21  NULL
10265986    CP2 Open    2019-11-18  Shoe rack is being used in the communal area. This is a potential trip hazard in an evacuation situation.   2019-10-21 13:05:56.543 2019-10-21  NULL
10265986    CP3 Open    2019-12-23  Christmas wreaths are a potential fire risk so must be removed. 2019-10-21 13:05:56.553 2019-10-21  Test Reference 12345
10265986    CP3 Open    2019-12-23  Christmas wreaths are a potential fire risk so must be removed. 2019-10-21 13:05:56.553 2019-10-21  Test Reference 12345
10265985    CP1 Closed  2019-01-18  Shoe cabinet is in the communal hallway door is a potential trip hazard when evacuating.    2019-10-18 11:11:41.457 2019-10-18  NULL
10265985    CP1 Closed  2019-01-18  Shoe cabinet is in the communal hallway door is a potential trip hazard when evacuating.    2019-10-18 11:11:41.457 2019-10-18  NULL

Могу ли я использовать окнафункция удаления дубликатов? Я бы использовал JobNo, ActionRef и Syncdate для получения числа 1 и фильтрации по нему?

Заранее благодарен за любую помощь.

1 Ответ

0 голосов
/ 21 октября 2019

Ваши дубликаты могут быть вызваны условиями соединения, хотя я должен признать, что у меня проблемы с пониманием вашего кода SQL. Если при добавлении отличных между выберите и sigfind. * не работает, попробуйте поместить весь код в предложение WITH, а затем выберите отличный.

with myResult as (
    Your Query here
)
select distinct column1, column2, ..., columnX from myResult

Может быть, лучше пройтись по логике вашего запроса, исправить ее, чтобы сделать ее более читабельной.

...