;WITH CTE
AS (SELECT OrganizationID,
CustomerID,
OrderID,
COUNT(*) OVER (PARTITION BY OrganizationID) AS C,
ROW_NUMBER() OVER (PARTITION BY OrganizationID
ORDER BY (SELECT 0)) AS RN
FROM YourTable)
SELECT OrganizationID,
CustomerID,
OrderID
FROM CTE
WHERE RN <= CEILING(0.5 * C)
SQL Fiddle
I skipped the code example, and jumped to what seems to be the real question here
On top of that, a manual inspection of the index in question shows no text, ntext, image, xml or varchar(MAX), nvarchar(MAX) or varbinary(MAX). Could there be something I'm missing here?
For the record, this is a clustered index.
You certainly are missing something, and the answer is in the body of the error message:
For a clustered index, the column could be any column of the table
For Online Index Operations
there is a difference between a Clustered
and a Non-Clustered
index.
both may not contain a BLOB column in order to rebuild online,
but while a Non-Clustered Index "contains" the columns in it's definition (and included columns),
a Clustered Index "contains" the entire table with all of the columns.
in your case there is a column named MEMO
which is of data type text, ntext, image, varchar(max), nvarchar(max), varbinary(max), xml, or large CLR type.
this column is part of the table, and thus part of the Clustered index.
Your options are:
- Perform the
REBUILD
operation offline
- Upgrade to SQL Server 2012
- Change the column's data type
Guidelines for Performing Online Index Operations
Best Answer
If your version of SQL Server is recent enough (2012+), the problem can be solved with the window/analytic function
LAST_VALUE()
:or
FIRST_VAUE()
with inverse order: