Select distinct values from 1 column

后端 未结 9 2072
忘了有多久
忘了有多久 2020-12-10 00:47

I want to select distinct values from only one column (the BoekingPlaatsId column) with this query:

SELECT MAX(BoekingPlaatsId), BewonerId, Naam, VoorNaam
F         


        
9条回答
  •  有刺的猬
    2020-12-10 01:27

    This is how you can select from a table with only unique values for your column:

    CREATE VIEW [yourSchema].[v_ViewOfYourTable] AS
    WITH DistinctBoekingPlaats AS
    (
        SELECT [BewonerId], 
               [Naam],
               [VoorNaam],
               [BoekingPlaatsId],
               ROW_NUMBER() OVER(PARTITION BY [BoekingPlaatsId] ORDER BY DESC) AS 'RowNum'
        FROM [yourSchema].[v_ViewOfYourTable]
    )
    SELECT * 
    FROM DistinctProfileNames
    WHERE RowNum = 1
    --if you would like to apply group by you can do it in this bottom select clause but you don't need it to gather distinct values 
    

    You don't need group by to accomplish this.

提交回复
热议问题