Solved

Select Distinct from sub-query

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
set default date format in mssql to mm/dd/yyyy 22 68
MSSQL: Replace text (typo) 7 33
How to use Full Text CONTAINS with Case in SQL 6 20
MS SQL Pivot table help 4 14
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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 date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

867 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

15 Experts available now in Live!

Get 1:1 Help Now