Using the distinct function in SQL

前端 未结 18 947
無奈伤痛
無奈伤痛 2021-02-06 16:55

I have a SQL query I am running. What I was wanting to know is that is there a way of selecting the rows in a table where the value in on one of those columns is distinct? When

相关标签:
18条回答
  • 2021-02-06 17:46

    As soulmerge and Shiraz have mentioned you'll need to use a GROUP BY and subselect. This worked for me.

    DECLARE @table TABLE (
        [Teacher] [NVarchar](256) NOT NULL ,
        [Student] [NVarchar](256) NOT NULL 
    )
    
    INSERT INTO @table VALUES ('Teacher 1', 'Student 1')
    INSERT INTO @table VALUES ('Teacher 1', 'Student 2')
    INSERT INTO @table VALUES ('Teacher 2', 'Student 3')
    INSERT INTO @table VALUES ('Teacher 2', 'Student 4')
    
    SELECT 
        T.[Teacher],  
        (
            SELECT TOP 1 T2.[Student]
            FROM @table AS T2 
            WHERE T2.[Teacher] = T.[Teacher]
        ) AS [Student]
    FROM @table AS T
    GROUP BY T.[Teacher]
    

    Results

    Teacher 1, Student 1
    Teacher 2, Student 3
    
    0 讨论(0)
  • 2021-02-06 17:47

    Noone seems to understand what you want. I will take another guess.

    Select * from tbl
    Where ColA in (Select ColA from tbl Group by ColA Having Count(ColA) = 1)
    

    This will return all data from rows where ColA is unique -i.e. there isn't another row with the same ColA value. Of course, that means zero rows from the sample data you provided.

    0 讨论(0)
  • 2021-02-06 17:50

    Based on the limited details you provided in your question (you should explain how/why your data is in different tables, what DB server you are using, etc) you can approach this from 2 different directions.

    1. Reduce the number of columns in your query to only return the "teacher" and "email" columns but using the existing WHERE criteria. The problem you have with your current attempt is both DISTINCT and GROUP BY don't understand that you one want 1 row for each value of the column that you are trying to be distinct about. From what I understand, MySQL has support for what you are doing using GROUP BY but MSSQL does not support result columns not included in the GROUP BY statement. If you don't need the "student" columns, don't put them in your result set.

    2. Convert your existing query to use column based sub-queries so that you only return a single result for non-grouped data.

    Example:

    SELECT t1.a
            , (SELECT TOP 1 b FROM Table1 t2 WHERE t1.a = t2.a) AS b
            , (SELECT TOP 1 c FROM Table1 t2 WHERE t1.a = t2.a) AS c
            , (SELECT TOP 1 d FROM Table1 t2 WHERE t1.a = t2.a) AS d
        FROM dbo.Table1 t1
        WHERE (your criteria here)
        GROUP BY t1.a
    

    This query will not be fast if you have a lot of data, but it will return a single row per teacher with a somewhat random value for the remaining columns. You can also add an ORDER BY to each sub-query to further tweak the values returned for the additional columns.

    0 讨论(0)
  • 2021-02-06 17:50

    All you have to do is select just the columns you want the first one and do a select Distinct

    Select Distinct column1 -- where your criteria...
    
    0 讨论(0)
  • 2021-02-06 17:52

    The following might help you get to your solution. The other poster did point to this but his syntax for group by was incorrect.

    Get all teachers that teach any classes.

       Select teacher_id, count(*)
        from teacher_table inner join classes_table
        on teacher_table.teacher_id = classes_table.teacher_id
        group by teacher_id
    
    0 讨论(0)
  • 2021-02-06 17:52
    CREATE FUNCTION dbo.DistinctList
    (
    @List VARCHAR(MAX),
    @Delim CHAR
    )
    RETURNS
    VARCHAR(MAX)
    AS
    BEGIN
    DECLARE @ParsedList TABLE
    (
    Item VARCHAR(MAX)
    )
    DECLARE @list1 VARCHAR(MAX), @Pos INT, @rList VARCHAR(MAX)
    SET @list = LTRIM(RTRIM(@list)) + @Delim
    SET @pos = CHARINDEX(@delim, @list, 1)
    WHILE @pos > 0
    BEGIN
    SET @list1 = LTRIM(RTRIM(LEFT(@list, @pos - 1)))
    IF @list1 <> ''
    INSERT INTO @ParsedList VALUES (CAST(@list1 AS VARCHAR(MAX)))
    SET @list = SUBSTRING(@list, @pos+1, LEN(@list))
    SET @pos = CHARINDEX(@delim, @list, 1)
    END
    SELECT @rlist = COALESCE(@rlist+',','') + item
    FROM (SELECT DISTINCT Item FROM @ParsedList) t
    RETURN @rlist
    END
    GO
    
    0 讨论(0)
提交回复
热议问题