I'm trying to add an ability to achieve the equivalent of a “SELECT TOP (X)” records in a Selection to restrict the number of records returned. I'm striking out.
I've come close by creating a Query View Extension using an un-ordered ROW_NUMBER. When run in SSMS it seems to do exactly what I'm looking for. However, when used in CRM, it returns the same counter values for each Constituent consistently - not an arbitrarily numbered list. It's unclear to me what the counter becomes tied to. It doesn't seem relate to the clustered index, sequenceid or anything else I've looked at.
From SSMS, I am able to get a similarly wrong result when I also add a pseudo RANDOM field and then sort by that - which, of course, is my ultimate goal. When I do that - the COUNTER field also becomes consistent. I'm guessing these things are related - I just don't understand how or what I might be able to do about it.
These give me what I'm looking for:
SELECT TOP 100 ID, LOOKUPID, COUNTER, RANDOM FROM V_QUERY_CONSTITUENT ORDER BY DATEADDED
SELECT TOP 100 ID, LOOKUPID, COUNTER, RANDOM FROM V_QUERY_CONSTITUENT ORDER BY KEYNAME, FIRSTNAME
When run within CRM, it does not. Also, the following does not:
SELECT TOP 100 ID, LOOKUPID, COUNTER, RANDOM FROM V_QUERY_CONSTITUENT ORDER BY RANDOM
<QueryViewSpecExtension
xmlns="bb_appfx_queryview"
xmlns:c="bb_appfx_commontypes"
ID="DBC72166-990E-4029-A13C-76588BF6B27A"
Name="Constituent Arbitrary Counter Query Extension (custom)"
Description="An extension to the Constituent query view to add an arbitrary counter"
Author="PHS - Mitch Gibbs"
ExtendsViewName="V_QUERY_CONSTITUENT"
>
<AddFields>
<AddField>
<SelectClause AliasAs="COUNTER">ROW_NUMBER() OVER(ORDER BY (SELECT NULL))</SelectClause>
<OutputField Name="COUNTER" Caption="Counter" DataType="Integer" />
</AddField>
<AddField>
<SelectClause AliasAs="RANDOM">CONVERT(INT, CRYPT_GEN_RANDOM(4))</SelectClause>
<OutputField Name="RANDOM" Caption="Random number" DataType="Integer" />
</AddField>
</AddFields>
</QueryViewSpecExtension>
Any ideas? Or, has anyone achieved anything similar? Thanks!