0

Hy,

I do have a query for a table which shows different entries for different usernames. But there are multiple entries for each username and I want to show only one entry for each username. My query looks like that:

SELECT Time AS 'Login', 
    User_Name  AS 'Username', 
    CASE 
    [User_Name] WHEN ('Demo') THEN 'Special' 
    ELSE 'Standard' END AS Usertype
    FROM table1

Is there a way to show only one entry for each username? I already tried out with "GROUP BY" but it didn´t work. Maybe someone could help me.

With this I would get the correct output, but is it anyhow possible to combine the two SELECTS?

WITH cte AS
    (
   SELECT *,
         ROW_NUMBER() OVER (PARTITION BY Username ORDER BY Username DESC) AS rn
   FROM table1
    )
    SELECT *
    FROM cte
    WHERE rn = 1
Clies
  • 23
  • 5
  • So, when there are multiple rows for a particular user, which row do you want? – Thom A Mar 22 '22 at 14:18
  • Side note, don't use single quotes (`'`) for aliases. Single quotes are for literal strings, not delimit identifying object names. They only work when you define them, no where else; `ORDER BY 'value'` would *not* order by your column aliased as `'value'`, it would order by the `varchar` literal `'value'` (so would effectively not order at all). Also some syntaxes with literal string aliases are deprecated. Stick to object and alias names that don't need delimit identifying, and if you *must* delimit identify them use the T-SQL identifier, brackets (`[]`), or ANSI-SQL's, double quotes (`"`). – Thom A Mar 22 '22 at 14:20
  • no matter which row I get. I just want only ONE of the entries for one user. Because I want to look if a user was online once a day. – Clies Mar 22 '22 at 14:20
  • So any old arbitrary row..? So the value of `time` doesn't matter? If so, why return it in the `SELECT`? – Thom A Mar 22 '22 at 14:27

0 Answers0