Skip to content
Advertisement

Select Distinct and sequence at the same time

How can I able to use Distinct on select statement along with a sequence on SQL Server

I try this sub-query but no luck I get error of

NEXT VALUE FOR function is not allowed in check constraints, default objects, computed columns, views, user-defined functions, user-defined aggregates, user-defined table types, sub-queries, common table expressions, derived tables or return statements.

 Select Distinct (Select
                  Next Value for dbo.P_PM_TBlIF240_SEQ , 'PH70', LEFT(ToolID, LEN(ToolID) - 2) as TOOLID, UsedLife, GETDATE(), CURRENT_TIMESTAMP, 'PH70-TA' 
            From
                    Machine_ToolItem_Relationship ) from Machine_ToolItem_Relationship

Can anyone help me with this?

Advertisement

Answer

Solve thank for the help @Nick.McDermaid my final query looks like this.

Select
    Next Value for dbo.P_PM_TBlIF240_SEQ , 'PH70', LEFT(ToolID, LEN(a.ToolID) - 2) as TOOLID, a.UsedLife, GETDATE(), CURRENT_TIMESTAMP, 'PH70-TA' 
    from (Select
    Distinct  LEFT(ToolID, LEN(ToolID) - 2) as TOOLID, UsedLife 
    from Machine_ToolItem_Relationship) as a  
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement