Solved

Select Distinct from sub-query

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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

630 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