Solved

Select Distinct from sub-query

Posted on 2013-01-23
2
479 Views
Last Modified: 2013-01-24
Hi all

I have a table below

Given that I have ran the following query to truncate the values you are seeing..
I need to be able to isolate duplicate keys..  and return only the FactActID of one of the  uniqe FileName.
In this case of File name.. (...T762163
I need a query that will only return the first FactID  DBF9D....
And so on.. for the rest of the dupplicate records


 
select REPLACE( FileName, RIGHT(FileName, 4), '' )  as  FileName, FactACTID
 FROM Stat_Fact_ACT 
where FileName is not null
and CLTID = 100
order by FileName desc
      

Open in new window


Trans table
Thanks in Advance
0
Comment
Question by:ZURINET
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 38810669
SELECT
FileName
,FactACTID
FROM
(
SELECT
FileName
,FactACTID
,ROW_NUMBER() OVER(PARTITION BY FileName) AS Row
FROM
(
select REPLACE( FileName, RIGHT(FileName, 4), '' )  as  FileName
, FactACTID
 FROM Stat_Fact_ACT
where FileName is not null
and CLTID = 100
) Fil
)Fil2
WHERE Row = 1
order by FileName desc
0
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
ID: 38810826
If the order is not important, you can just do

select REPLACE( FileName, RIGHT(FileName, 4), '' )  as  FileName, max(FactACTID) FactACTID
 FROM Stat_Fact_ACT 
where FileName is not null
and CLTID = 100
group by REPLACE( FileName, RIGHT(FileName, 4), '' )
order by FileName desc

Open in new window



If not, I would just do one subquery

select * from (
select REPLACE( FileName, RIGHT(FileName, 4), '' )  as  FileName, FactACTID, row_number() over (partition by REPLACE( FileName, RIGHT(FileName, 4), '' ) order by REPLACE( FileName, RIGHT(FileName, 4), '' ) desc) rn
 FROM Stat_Fact_ACT 
where FileName is not null
and CLTID = 100
) a 
where rn = 1

Open in new window

0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

738 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