I have a code which calls a single page using SQL like this
select
<some fields>,
count(*) over.. as _$total_records
From
<some inline view>
ORDER BY ...
OFFSET ...
FETCH ...
This SQL might return duplicated ids
due to a join. And this was OK in the most of the cases. But a new requirement is to suppress duplicates. However, I must not suppress certain fields that are coming from the join which brings duplicates. In other words I need result set like this
From
id | name | reference |
---|---|---|
1 | A | 55 |
1 | A | 66 |
2 | B | 11 |
3 | C | 77 |
3 | C | 88 |
To
id | name | reference |
---|---|---|
1 | A | 55 |
2 | B | 11 |
3 | C | 77 |
I was thinking about this logic (pseudo code)
while (resultModelList.Count < pageSize && recordsReturned)
{
reader = CallDb(sql);
LoadPage(reader, resultModelList, out recordsReturned);
}
In this case ^^ LoadPage
will check if the id
is already added into the list and skip adding this record. And operation will repeat until page size is satisfied or no more records returned by the reader
.
But I see potential issues with skipped records. And also correct page number due to count() over
. Is there a good way to deal with it?
Update: this question is not about SQL solution but about paging solution. SQL here is built dynamically. It can have 1,2,3 joins based on configuration and criteria values. All these items are added dynamically. The SELECT
is built dynamically.