G
G
Georgy Baruchyan2019-01-13 01:49:40
1C-Bitrix
Georgy Baruchyan, 2019-01-13 01:49:40

How to add distinct to a query in Bitrix ORM?

Hello!
There is a table described in Entities\Marketing\PostingTmpTable
. It stores intermediate data on mailings from the site. The table has about 500,000 entries. Collected by an additional script.
The main field used is EMAIL. There is a connection with other tables through
it . \PostingTable has a MAILING_ID field - the id of the distribution group by which filtering is done. It turns out, something like this request:

$query  = (new \Bitrix\Main\Entity\Query(\Entities\Marketing\PostingTmpTable::getEntity()))

   ->registerRuntimeField('PROPERTY', [
        'data_type' => $hlBlockEntity,
        'reference' => [
            '=this.EMAIL' => 'ref.UF_EMAIL'
        ]
    ])
    ->registerRuntimeField('RECIPIENT', [
        'data_type' => '\Bitrix\Sender\PostingRecipientTable',
        'reference' => [
            '=this.EMAIL' => 'ref.EMAIL',
        ],
    ])
    ->registerRuntimeField('POSTING', [
        'data_type' => '\Bitrix\Sender\PostingTable',
        'reference' => [
            '=this.RECIPIENT.POSTING_ID' => 'ref.ID',
        ]
    ])
    ->where('POSTING.MAILING_ID', 6)
    ->setSelect(/* сюда ставлю собираемый массив селектов, к делу не относится*/)
;

The problem is that there are 7,000,000 records in the Bitrix\Sender\PostingRecipientTable table, and there can be many records for each email address
. Because of this, as a result, selecting by POSTING.MAILING_ID = 6, I get as a result the number of records more than in my table Entities\Marketing\PostingTmpTable
If you use the $query->getQuery() method and add distinct to the resulting sql query, the result becomes correct.
Hence the question, how to add distinct for the query object?? I did not find an answer in the Bitrix documentation, in the query class either.
There is \Bitrix\Main\Entity\Query::expr()->countDistinct("ID") - but this is only suitable for the total count of elements, and in the query itself I use limit to display a limited number of records, but I have it turns out a lot of duplicates, which should not be.
I hope I made the point clear!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
serginhold, 2019-01-13
@Snatch87

In general, the documentation has an expression field
https://dev.1c-bitrix.ru/learning/course/index.php...
new Entity\ExpressionField('DISTINCT_ID',
'DISTINCT %s', array('ID')
)
I think it's in select

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question