Tag: distinct

Select distinct column values and add the related of values of another column

I’m working on a time card system where a user can enter a time against a single unit or a ‘run’ of units. I have already created the time entry portion, where a user can enter time against a ‘run’ and it will create separate rows for each unit specified as being part of that […]

SQL – Query log from Users table

Based on the following example : (it is a “QueryLog” table, this table store interactions between a user and two different products N and R): Id Date UserID Product ————————————————– 0 2013-06-09 14:50:24.000 100 N 1 2013-06-09 15:27:23.000 100 N 2 2013-06-09 15:29:23.000 100 N 3 2013-06-17 15:31:23.000 100 N 4 2013-06-17 15:32:23.000 100 N […]

distinct value per column

I am looking at a report on policy exceptions based on various criteria such as Beacon Score, Debt to Income, and Loan to Value. This information is kept in multiple different tables, and right now the Loan to Value column is causing multiple entries in my report because a specific loan might have multiple pieces […]

Distinct code wont work SQL Server

SELECT DISTINCT U.Unit_ID, P.Plant_ID, P.Project_NR, U.Key_code_list, S.Status_type, Kc.Key_codes4 FROM Plant as P INNER JOIN Unit as U ON P.Plant_NR = U.Plant_NR INNER JOIN [dbo].[Key_code_list] as Kcl ON P.Project_NR = Kcl.Project_NR INNER JOIN Status_codes as S ON S.Status_nr = Kcl.Status INNER JOIN Key_codes as Kc ON Kc.Key_code_ID = Kcl.Key_code_list_ID I have this code, and it will […]

TSQL not returning DISTINCT Values

I am trying to get only unique primary key values using DISTINCT but cannot figure out why DISTINCT is not working. The TSQL below (Using Windows Azure SQL Server) is trying to identify duplicate addresses by looking at the first string of numeric values in the address field. It is returning matches properly but just […]

Counting the occurrences of a distinct values depending on its date

I am trying to find a SQL Server query that will find distinct values in a particular column, count the number of occurrences of that value depending on its date. Example data: in_date item_code ——— ——— 2015/11/25 item1 2015/11/25 item2 2015/12/10 item1 2015/12/10 item1 2015/12/10 item2 2016/01/01 item1 Expected output: Date Item1 Item2 ———- —– […]

Duplicate results – cannot condense

The results from this report list the same client 7-9 times. I’m not entirely certain why. Have tried GROUPING and DISTINCT. DISTINCT refines the results to only 3 duplicates per Client. Perhaps I am overlooking something obvious. In short, I am getting ClientID | First_Name | Last_Name 123……………..John……………………John 123……………..John……………………John 123……………..John……………………John 456……………..Bob……………………..Bob 456……………..Bob……………………..Bob 456……………..Bob……………………..Bob What I […]

Select top 2 distinct for each id and date

I have a table like this : Table1: [Id] [TDate] [Score] 1 1.1.00 50 1 1.1.00 60 2 1.1.01 50 2 1.1.01 70 2 1.3.01 40 3 1.1.00 80 3 1.1.00 30 3 1.2.00 40 My desired output should be like this: [ID] [TDate] [Score] 1 1.1.00 60 2 1.1.01 70 2 1.3.01 40 3 […]

How to get Distinct Email ID with muiltiple records where ID=@ID

I am getting the list of ID and On behalf of that ID I am getting the list of records I am attaching the records details in a image as you see in the image I am getting duplicate Email ID, so for making email id unique I used this query Here is my query […]

SQL Order By when DISTINCT?

I have query where I use distinct and order by. Two columns used in order by clause are used in Select statement but they are concatenated. Once I run the query error message appeared: Msg 145, Level 15, State 1, Line 1 ORDER BY items must appear in the select list if SELECT DISTINCT is […]

MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.