Solved

Select Distinct from sub-query

Posted on 2013-01-23
2
475 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
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

808 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