Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Need Assistance

Author  Topic 

ARTYB
Starting Member

4 Posts

Posted - 2013-07-17 : 11:00:08
I have a table where i have a multitude of columns.
idnum,diagnosiscode,order_of_diagnosis,POA_Flag

000000000000001|234|01|Y
000000000000001|800|02|Y
000000000000001|E344|03|Y
000000000000001|667|04|Y
000000000000001|900|05|Y
000000000000001|234|06|Y
000000000000001|800|07|Y
000000000000001|E344|08|Y
000000000000001|667|09|Y
000000000000001|900|10|Y
000000000000001|288|11|Y
000000000000001|4800|12|Y
000000000000001|G887|13|Y
000000000000001|667|14|Y
000000000000001|900|15|Y

I am trying to write a select distinct query that will return each group with ONLY unique diagnosiscode, and renumbering as applicable so they stay in thier original order.

Thanks for any all assistance in advance.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-17 : 11:20:27
So what should be the output?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

ARTYB
Starting Member

4 Posts

Posted - 2013-07-17 : 11:36:32
Output should be the same format as input :D
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-17 : 11:58:52
Sorry i didnt get you. How do you want numbering to come?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

ARTYB
Starting Member

4 Posts

Posted - 2013-07-17 : 12:06:40
Here is the example content prior to removing the duplicate values

000000000000001|234|01|Y
000000000000001|800|02|Y
000000000000001|E344|03|Y
000000000000001|667|04|Y
000000000000001|900|05|Y
000000000000001|234|06|Y - to be removed as redundant
000000000000001|800|07|Y - to be removed as redundant
000000000000001|E344|08|Y - to be removed as redundant
000000000000001|667|09|Y - to be removed as redundant
000000000000001|900|10|Y - to be removed as redundant
000000000000001|288|11|Y
000000000000001|4800|12|Y
000000000000001|G887|13|Y
000000000000001|667|14|Y - to be removed as redundant
000000000000001|900|15|Y - to be removed as redundant


This would be the final output format:

000000000000001|234|01|Y
000000000000001|800|02|Y
000000000000001|E344|03|Y
000000000000001|667|04|Y
000000000000001|900|05|Y
000000000000001|288|06|Y (New Value and in ordinal order)
000000000000001|4800|07|Y
(New Value and in ordinal order)
Go to Top of Page

ARTYB
Starting Member

4 Posts

Posted - 2013-07-17 : 12:07:55
quote:
Originally posted by ARTYB

Here is the example content prior to removing the duplicate values

000000000000001|234|01|Y
000000000000001|800|02|Y
000000000000001|E344|03|Y
000000000000001|667|04|Y
000000000000001|900|05|Y
000000000000001|234|06|Y - to be removed as redundant
000000000000001|800|07|Y - to be removed as redundant
000000000000001|E344|08|Y - to be removed as redundant
000000000000001|667|09|Y - to be removed as redundant
000000000000001|900|10|Y - to be removed as redundant
000000000000001|288|11|Y
000000000000001|4800|12|Y
000000000000001|G887|13|Y
000000000000001|667|14|Y - to be removed as redundant
000000000000001|900|15|Y - to be removed as redundant


This would be the final output format:

000000000000001|234|01|Y
000000000000001|800|02|Y
000000000000001|E344|03|Y
000000000000001|667|04|Y
000000000000001|900|05|Y
000000000000001|288|06|Y (New Value and in ordinal order)
000000000000001|4800|07|Y (New Value and in ordinal order)
000000000000001|G887|08|Y (New Value and in ordinal order)


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-17 : 12:28:57
[code]
SELECT idnum,diagnosiscode,order_of_diagnosis,POA_Flag
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY idnum,diagnosiscode ORDER BY order_of_diagnosis ASC) AS RN,*
FROM Table
)t
WHERE RN=1
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -