Solved

MS SQL - Remove Duplicate Values in a Column & Display All Columns (Not DISTINCT)

Posted on 2014-11-26
3
321 Views
Last Modified: 2014-11-27
Dear Experts,

In my query result I need to show results from a SQL alarms history table for 5 columns where Col1 = Date&Time, Col2 = Label, Col3 = Priority, Col4 = Status, Col5 = Cycle. I am using the following syntax which shows everything.

SELECT TOP 50 App_DateTime, Label, Priority, Enum_Status, Cycle FROM History

However because an alarm is recorded every time it cycles (ON/OFF) it is recorded as a new entry into the database with a unique Date&Time. This produces duplicate column values for Label, Priority and Status. I want to display only the maximum Cycle count for each alarm entry and not all the Cycle values below that for the same alarm. Therefore I want to eliminate duplicate entries in the Label column.

The problem I have in using 'GROUP BY Label' is that when I apply aggregates to all the other SELECT fields to allow the GROUP BY to function to work then I only get a single row result. I am expecting to see a list of different alarms together with their date, time, label, priority and status and maximum cycle value.

I am new to SQL so may well be approaching this the wrong way. I have searched around and tried using SELECT DISTINCT but that will eliminate a row only if all its fields are the same. Clearly this wont work for me since I am trying to eliminate duplicate values from a column and not duplicate rows.

Thanks, Gary
0
Comment
Question by:Elisys
  • 2
3 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 40468020
SELECT TOP (50) App_DateTime, Label, Priority, Enum_Status, Cycle
FROM (
    SELECT App_DateTime, Label, Priority, Enum_Status, Cycle,
        ROW_NUMBER() OVER(PARTITION BY Label ORDER BY Cycle DESC) AS row_num
    FROM History
) AS subquery1
WHERE
    row_num = 1
--ORDER BY <whatever>
0
 

Author Closing Comment

by:Elisys
ID: 40468772
Hi Scott, thanks for your answer. You wouldn't believe how long I have been trying to put this together. A brief explanation of how the query works would be very much appreciated for us SQL novices. G
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40469411
I actually do believe it :-).  It was a real pain to do certain things like this before ROW_NUMBER() came along.

Basically ROW_NUMBER() provides an inline ordering that resets to 1 on each PARTITION break.

For example, say you had a list of states and cities.  You could PARTITION BY state ORDER BY city, and you'd get a result like:
1 CA Sacramento
2 CA San Diego
3 CA San Francisco
1 TN Memphis --note that the state change causes the number to reset
2 TN Nashville --
1 WY ...


Now, let's go back to:
PARTITION BY Label ORDER BY Cycle DESC
We can see that ROW_NUMBER/row_num 1 will always be the highest cycle number for each label, which is what we want to list, so we restrict the output to:
row_num = 1

This is wonderfully useful/flexible.  For example, to get the top 3, you could use:
row_num BETWEEN 1 AND 3

How sweet is that!
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel conversion issue with Sql server 14 46
Optimizing a query 3 33
datetime in sql 6 24
ms sql last 8 weeks as columns 5 24
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now