davethebear10
davethebear10

Reputation: 5

SQL SELECT DISTINCT columns then add another column in same table

I am using the following query to get DISTINCT records I'm interested in.

SELECT DISTINCT 
USERID, Policy_Name, Account_Type, WEEKNUM, EMPLOYEETYPE, MANAGERID
FROM table1

In table 1 there is also a column called Acc-Check.

I want to add Acc-Check to the results of the above query during or afterward without affecting the 'DISTINCT' number of results i.e. the output will then be:

USERID, Policy_Name, Account_Type, WEEKNUM, EMPLOYEETYPE, MANAGERID, Acc-Check

None of the columns are PK. Open to any suggestions and can edit the table as necessary.

USERID             nvarchar(MAX)
Policy_Name        nvarchar(MAX)
Account_Type       nvarchar(MAX)
WEEKNUM            nvarchar(MAX)
EMPLOYEETYPE       nvarchar(MAX)
MANAGERID          nvarchar(MAX)
Acc_Check          nvarchar(MAX)

Upvotes: 0

Views: 99

Answers (1)

jarlh
jarlh

Reputation: 44696

Do a GROUP BY to get same result as before, add max("Acc-Check") to get one acc-check value.

SELECT 
USERID, Policy_Name, Account_Type, WEEKNUM, EMPLOYEETYPE, MANAGERID, max("Acc-Check")
FROM table1
group by USERID, Policy_Name, Account_Type, WEEKNUM, EMPLOYEETYPE, MANAGERID

Upvotes: 1

Related Questions