деталь Платежные реквизиты

Есть потребность выводить в реестр Контрагентов юридическое наименование и ИНН. Я в запросе для контрагента делаю следующее:
SELECT
[tbl_Account].[ID] AS [ID],
[tbl_Account].[Name] AS [Name],
[tbl_Account].[OfficialAccountName] AS [OfficialAccountName],
[tbl_Account].[AnnualRevenue] AS [AnnualRevenue],
[tbl_Account].[EmployeesNumber] AS [EmployeesNumber],
[tbl_Account].[Address] AS [Address],
[tbl_Account].[AddressTypeID] AS [AddressTypeID],
[tbl_Account].[Communication1] AS [Communication1],
[tbl_Account].[Communication1TypeID] AS [Communication1TypeID],
[tbl_Account].[Communication2] AS [Communication2],
[tbl_Account].[Communication2TypeID] AS [Communication2TypeID],
[tbl_Account].[Communication3] AS [Communication3],
[tbl_Account].[Communication3TypeID] AS [Communication3TypeID],
[tbl_Account].[Communication4] AS [Communication4],
[tbl_Account].[Communication4TypeID] AS [Communication4TypeID],
[tbl_Account].[Communication5] AS [Communication5],
[tbl_Account].[Communication5TypeID] AS [Communication5TypeID],
[tbl_City].[Name] AS [CityName],
[tbl_Account].[CityID] AS [CityID],
[tbl_Account].[ZIP] AS [ZIP],
[tbl_Campaign].[Name] AS [CampaignName],
[tbl_Account].[CampaignID] AS [CampaignID],
[tbl_Contact].[Name] AS [PrimaryContactName],
[tbl_Account].[PrimaryContactID] AS [PrimaryContactID],
[tbl_Country].[Name] AS [CountryName],
[tbl_Account].[CountryID] AS [CountryID],
[tbl_State].[Name] AS [StateName],
[tbl_Account].[StateID] AS [StateID],
[tbl_Territory].[Name] AS [TerritoryName],
[tbl_Account].[TerritoryID] AS [TerritoryID],
[Owner].[Name] AS [OwnerName],
[tbl_Account].[OwnerID] AS [OwnerID],
[tbl_Account].[ActivityID] AS [ActivityID],
[tbl_Activity].[Name] AS [ActivityName],
[tbl_Account].[FieldID] AS [FieldID],
[tbl_Field].[Name] AS [FieldName],
[tbl_Account].[AccountTypeID] AS [AccountTypeID],
[tbl_AccountType].[Name] AS [AccountTypeName],
[tbl_AddressType].[Name] AS [AddressTypeName],
[CommunicationType1].[Name] AS [Communication1TypeName],
[CommunicationType2].[Name] AS [Communication2TypeName],
[CommunicationType3].[Name] AS [Communication3TypeName],
[CommunicationType4].[Name] AS [Communication4TypeName],
[CommunicationType5].[Name] AS [Communication5TypeName],
[tbl_Account].[Code] AS [Code],
[tbl_Account].[TaxRegistrationCode] AS [TaxRegistrationCode],
[tbl_Account].[CreatedOn] AS [CreatedOn],
[tbl_Account].[CreatedByID] AS [CreatedByID],
[CreatedBy].[Name] AS [CreatedByName],
[tbl_Account].[ModifiedOn] AS [ModifiedOn],
[tbl_Account].[ModifiedByID] AS [ModifiedByID],
[ModifiedBy].[Name] AS [ModifiedByName],
[tbl_Job].[NameOf] AS [JobNameOf],
[tbl_Account].[SettledCredit] AS [SettledCredit],
[tbl_Account].[PostponementPayment] AS [PostponementPayment],
NULL AS [UID1C],
NULL AS [Object1C],
[tbl_Account].[SiteID] AS [SiteID],
[tbl_Account].[IsActive] AS [IsActive],
[tbl_Account].[DoNotCall] AS [DoNotCall],
[vw_AppealInfo].[AppealDate] AS [AppealDate],
[vw_AppealInfo].[AppealStatusID] AS [AppealStatusID],
[AITaskStatus].[Status] AS [AppealStatus],
[vw_AppealInfo].[AppealTypeID] AS [AppealTypeID],
[AITaskType].[Name] AS [AppealTypeName],
[vw_LoyaltyInfo].[LoyContDate] AS [LoyContDate],
[vw_LoyaltyInfo].[LoyContTypeID] AS [LoyContTypeID],
[LITaskType].[Name] AS [LoyContTypeName],
[vw_LoyaltyInfo].[LoyOrderNum] AS [LoyOrderNum],
[vw_LoyaltyInfo].[LoyResultID] AS [LoyResultID],
[LITaskResult].[Result] AS [LoyResultName],
[vw_LoyaltyInfo].[LoyComment] AS [LoyComment],
[vw_LoyaltyRepInfo].[LoyRepCallDate] AS [LoyRepCallDate],
[vw_ReminderInfo].[RemContDate] AS [RemContDate],
[vw_ReminderInfo].[RemContTypeID] AS [RemContTypeID],
[RITaskType].[Name] AS [RemContTypeName],
[vw_ReminderInfo].[RemOrderNum] AS [RemOrderNum],
[vw_ReminderInfo].[RemResultID] AS [RemResultID],
[RITaskResult].[Result] AS [RemResultName],
[vw_ReminderInfo].[RemComment] AS [RemComment],
[vw_ReminderRepInfo].[RemRepCallDate] AS [RemRepCallDate],
[tbl_Account].[SitePaymentRecID] AS [SitePaymentRecID],
[tbl_AccountBillingInfo].[INN] AS [INN],
[tbl_AccountBillingInfo].[Name] AS [NameUr]

FROM
[dbo].[tbl_Account] AS [tbl_Account]
LEFT OUTER JOIN
[dbo].[tbl_Contact] AS [tbl_Contact] ON [tbl_Contact].[ID] = [tbl_Account].[PrimaryContactID]
LEFT OUTER JOIN
[dbo].[tbl_Territory] AS [tbl_Territory] ON [tbl_Territory].[ID] = [tbl_Account].[TerritoryID]
LEFT OUTER JOIN
[dbo].[tbl_Contact] AS [Owner] ON [Owner].[ID] = [tbl_Account].[OwnerID]
LEFT OUTER JOIN
[dbo].[tbl_Campaign] AS [tbl_Campaign] ON [tbl_Campaign].[ID] = [tbl_Account].[CampaignID]
LEFT OUTER JOIN
[dbo].[tbl_City] AS [tbl_City] ON [tbl_City].[ID] = [tbl_Account].[CityID]
LEFT OUTER JOIN
[dbo].[tbl_State] AS [tbl_State] ON [tbl_State].[ID] = [tbl_Account].[StateID]
LEFT OUTER JOIN
[dbo].[tbl_Country] AS [tbl_Country] ON [tbl_Country].[ID] = [tbl_Account].[CountryID]
LEFT OUTER JOIN
[dbo].[tbl_Activity] AS [tbl_Activity] ON [tbl_Activity].[ID] = [tbl_Account].[ActivityID]
LEFT OUTER JOIN
[dbo].[tbl_Field] AS [tbl_Field] ON [tbl_Field].[ID] = [tbl_Account].[FieldID]
LEFT OUTER JOIN
[dbo].[tbl_AccountType] AS [tbl_AccountType] ON [tbl_AccountType].[ID] = [tbl_Account].[AccountTypeID]
LEFT OUTER JOIN
[dbo].[tbl_Contact] AS [CreatedBy] ON [CreatedBy].[ID] = [tbl_Account].[CreatedByID]
LEFT OUTER JOIN
[dbo].[tbl_Contact] AS [ModifiedBy] ON [ModifiedBy].[ID] = [tbl_Account].[ModifiedByID]
LEFT OUTER JOIN
[dbo].[tbl_AddressType] AS [tbl_AddressType] ON [tbl_AddressType].[ID] = [tbl_Account].[AddressTypeID]
LEFT OUTER JOIN
[dbo].[tbl_CommunicationType] AS [CommunicationType1] ON [CommunicationType1].[ID] = [tbl_Account].[Communication1TypeID]
LEFT OUTER JOIN
[dbo].[tbl_CommunicationType] AS [CommunicationType2] ON [CommunicationType2].[ID] = [tbl_Account].[Communication2TypeID]
LEFT OUTER JOIN
[dbo].[tbl_CommunicationType] AS [CommunicationType3] ON [CommunicationType3].[ID] = [tbl_Account].[Communication3TypeID]
LEFT OUTER JOIN
[dbo].[tbl_CommunicationType] AS [CommunicationType4] ON [CommunicationType4].[ID] = [tbl_Account].[Communication4TypeID]
LEFT OUTER JOIN
[dbo].[tbl_CommunicationType] AS [CommunicationType5] ON [CommunicationType5].[ID] = [tbl_Account].[Communication5TypeID]
LEFT OUTER JOIN
[dbo].[tbl_Job] AS [tbl_Job] ON [tbl_Job].[ID] = [tbl_Contact].[JobID]
LEFT OUTER JOIN
[dbo].[vw_AppealInfo] AS [vw_AppealInfo] ON [vw_AppealInfo].[AccountID] = [tbl_Account].[ID]
LEFT OUTER JOIN
[dbo].[vw_LoyaltyInfo] AS [vw_LoyaltyInfo] ON [vw_LoyaltyInfo].[AccountID] = [tbl_Account].[ID]
LEFT OUTER JOIN
[dbo].[vw_LoyaltyRepInfo] AS [vw_LoyaltyRepInfo] ON [vw_LoyaltyRepInfo].[AccountID] = [tbl_Account].[ID]
LEFT OUTER JOIN
[dbo].[vw_ReminderInfo] AS [vw_ReminderInfo] ON [vw_ReminderInfo].[AccountID] = [tbl_Account].[ID]
LEFT OUTER JOIN
[dbo].[vw_ReminderRepInfo] AS [vw_ReminderRepInfo] ON [vw_ReminderRepInfo].[AccountID] = [tbl_Account].[ID]
LEFT OUTER JOIN
[dbo].[tbl_TaskStatus] AS [AITaskStatus] ON [AITaskStatus].[ID] = [vw_AppealInfo].[AppealStatusID]
LEFT OUTER JOIN
[dbo].[tbl_TaskType] AS [AITaskType] ON [AITaskType].[ID] = [vw_AppealInfo].[AppealTypeID]
LEFT OUTER JOIN
[dbo].[tbl_TaskType] AS [LITaskType] ON [LITaskType].[ID] = [vw_LoyaltyInfo].[LoyContTypeID]
LEFT OUTER JOIN
[dbo].[tbl_TaskResult] AS [LITaskResult] ON [LITaskResult].[ID] = [vw_LoyaltyInfo].[LoyResultID]
LEFT OUTER JOIN
[dbo].[tbl_TaskType] AS [RITaskType] ON [RITaskType].[ID] = [vw_ReminderInfo].[RemContTypeID]
LEFT OUTER JOIN
[dbo].[tbl_TaskResult] AS [RITaskResult] ON [RITaskResult].[ID] = [vw_ReminderInfo].[RemResultID]
LEFT OUTER JOIN
[dbo].[tbl_AccountBillingInfo] AS [tbl_AccountBillingInfo] ON [tbl_AccountBillingInfo].[AccountID] = [tbl_Account].[ID]

Использую левое соедиение с таблицей tbl_AccountBillingInfo.
Когда я вывожу колонки NameUr,INN в реестр контрагентов строка контрагентов начинает повторяться столько раз сколько у контрагента в детали строк, это логично, но мне нужно что бы в реестре строка контрагента выводилась только один раз без дублирований, помогите пожалуйста как это сделать.

Нравится

5 комментариев

вывод строк реестра контаргентов

Поскольку связь между контрагентом и платежными реквизитами, из которых Вы выбираете ИНН, представляет собой 1:M, выводить данные таким образом некорректно.
Реализуйте задачу аналогично отраслям контрагента:
- в таблицу контрагента добавлено поле отрасль.
- для каждой отрасли на детали можно указать признак «Основная».
- при добавлении основной отрасли на детали, обновляется значение в таблице контрагента.

Наталия, дело в том, что деталь "Платежные реквизиты" блокируется по действию: синхронизировать с ИС. не имеет смысла делать признак "Основной" Можно ли вместо признака "Основная" в запросе использовать значение ID импортируемой записи?

Тогда создайте в запросе sq_Account колонку подзапроса (SubSelectColumn), в ней определите нужные условия.
Например:
/system/files/subselectcolumn.png

(SELECT TOP 1
		[BillingInfo].[INN] AS [INN]
	FROM
		[dbo].[tbl_AccountBillingInfo] AS [BillingInfo]
	WHERE([BillingInfo].[AccountID] = [tbl_Account].[ID] AND
		[BillingInfo].[RSN] = :RSN)) AS [INN]

Спасибо, все получилось

Показать все комментарии