SQL Query for CONCAT and DISTINCT or GROUP BY

Jiri Zaloudek 21 Reputation points
2021-02-11T09:30:05.383+00:00

hello guys,

i have this query:

SELECT CONCAT(RTRIM([lastname]), ', ', LTRIM([firstname])) AS name, [oc] as user_id
  FROM [table]
  WHERE [priorita_max]='100'
  ORDER BY [lastname],[firstname] ASC
  • please note that ORDER BY is very important here

and I need results to be DISTINCT...
so one option is to use DISTINCT directly,
or GROUP BY [oc] / user_id

however for both cases it gives me errors...

Can you please share with what would be correct way to do that?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,788 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,637 questions
{count} votes

Accepted answer
  1. Wilko van de Velde 2,226 Reputation points
    2021-02-11T14:24:46.437+00:00

    I guess you can try:

     SELECT CONCAT(RTRIM([lastname]), ', ', LTRIM([firstname])) AS name, [oc] as user_id
       FROM [table]
       WHERE [priorita_max]='100'
       ORDER BY CONCAT(RTRIM([lastname]), ', ', LTRIM([firstname])) ASC
    

2 additional answers

Sort by: Most helpful
  1. Viorel 117.3K Reputation points
    2021-02-11T14:40:56.78+00:00

    Try something like this too:

    ;
    with E as
    (
        select distinct oc, firstname, lastname
        from [table]
        where [priorita_max] = '100'
    )
    select CONCAT(RTRIM([lastname]), ', ', LTRIM([firstname])) AS name, [oc] as user_id
    from E
    order by lastname, firstname
    
    1 person found this answer helpful.
    0 comments No comments

  2. Jiri Zaloudek 21 Reputation points
    2021-02-11T14:19:16.57+00:00

    Basicaly I have list of persons such as:

    OC      FNAME   LNAME   CARDnumber
    ---------------------------------------------------
    1111        fn1     ln1     016516515
    1111        fn1     ln1     546546541
    1111        fn1     ln1     232456345
    1112        fn2     ln2     524525436
    1112        fn2     ln2     546453245
    1112        fn2     ln2     545354224
    1113        fn3     ln3     567251233
    1113        fn3     ln3     152375727
    1113        fn3     ln3     897541567
    

    as you can see, it ID (OC) and name is always same, however one person can have any amount of CARDnumberts assigned to him...
    however what I need to do is DISTINCT OC and CONCAT(lname,", ",fname) and cardnumber doesnt bother me...

    so If i would just simply:

    SELECT DISTINCT [oc], CONCAT(lname, ", ", fname)
    ORDER BY lname ASC
    

    then i have each person there three times obviously.

    When I add:

    GROUP BY [oc]
    

    im having an error:
    Msg 8120, Level 16, State 1, Line 1
    Column 'lname' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
    Msg 8120, Level 16, State 1, Line 1
    Column 'fname' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.