Select distinct

I have the query below, which may return identical records other than the 'logid' being different.

I want to display only those records which are different, in other words, records that are identical OTHER than the 'logid' which will be different since its an incremental ID.  
The problem is if I remove the logid then I get the records I need, but then I can't order by logid  :$  which I need to have the most recent record at the top.
Is there a way around this ?

SELECT DISTINCT TOP 6
        a.logid ,
        a.firmid ,
        a.userid ,
        a.fileid ,
        a.filetype ,
        a.searchdate ,
        b.FirstNm ,
        b.LastNm ,
        b.Dob ,
        b.DobD
FROM    dbo.LogSearches a
        INNER JOIN Users AS b ON b.UserId = a.fileid
WHERE   a.firmid = 2
        AND a.userid = 1713
        AND a.filetype = 'contact'
ORDER BY a.logid DESC;

Open in new window

LVL 1
AleksAsked:
Who is Participating?
 
Pawan KumarConnect With a Mentor Database ExpertCommented:
May be this

SELECT TOP 6 z.logid , r.* FROM 
(
	SELECT DISTINCT 
			a.firmid ,
			a.userid ,
			a.fileid ,
			a.filetype ,
			MAX(a.searchdate) OVER (PARTITION BY a.firmid ,a.userid, a.fileid , a.filetype , b.FirstNm  , b.LastNm ,b.Dob, b.DobD) searchdate,
			b.FirstNm ,
			b.LastNm ,
			b.Dob ,
			b.DobD
	FROM    dbo.LogSearches a
			INNER JOIN Users AS b ON b.UserId = a.fileid
	WHERE   a.firmid = 2
			AND a.userid = 1713
			AND a.filetype = 'contact'
)r
CROSS APPLY
(	
	SELECT MAX(k.logid) logid
	FROM dbo.LogSearches k
	WHERE 
			k.firmid = r.firmid AND 
			k.userid = r.userid AND 
			k.fileid = r.fileid AND 
			k.filetype = r.filetype AND 
			k.searchdate = r.searchdate
)z
ORDER BY z.logid DESC;

Open in new window

0
 
Alexandre SimõesManager / Technology SpecialistCommented:
What you have to think about is what's the criteria to choose between the identical records?

As far as I understood, you want the latest right?
So, in your table, which is the column that stores the record date?

If you give me this information I can draft you a query.

Cheers,
Alex
0
 
AleksAuthor Commented:
I want the most recent 6 records.
the date is recorded on column:  a.searchdate

Which you can use to order it or the logind  ordered DESC would give you the same result.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Pawan KumarDatabase ExpertCommented:
Try this..

SELECT TOP 6 z.logid , r.* FROM 
(
	SELECT DISTINCT 
			a.firmid ,
			a.userid ,
			a.fileid ,
			a.filetype ,
			a.searchdate ,
			b.FirstNm ,
			b.LastNm ,
			b.Dob ,
			b.DobD
	FROM    dbo.LogSearches a
			INNER JOIN Users AS b ON b.UserId = a.fileid
	WHERE   a.firmid = 2
			AND a.userid = 1713
			AND a.filetype = 'contact'
)r
CROSS APPLY
(	
	SELECT MAX(k.logid) logid
	FROM dbo.LogSearches k
	WHERE 
			k.firmid = r.firmid
			k.userid = r.userid
			k.fileid = r.fileid
			k.filetype = r.filetype
			k.searchdate = r.searchdate
)z
ORDER BY z.logid DESC;

Open in new window



2. 2nd one

SELECT TOP 6 z.logid , r.* FROM 
(
	SELECT DISTINCT 
			a.firmid ,
			a.userid ,
			a.fileid ,
			a.filetype ,
			a.searchdate ,
			b.FirstNm ,
			b.LastNm ,
			b.Dob ,
			b.DobD
	FROM    dbo.LogSearches a
			INNER JOIN Users AS b ON b.UserId = a.fileid
	WHERE   a.firmid = 2
			AND a.userid = 1713
			AND a.filetype = 'contact'
)r
CROSS APPLY
(	
	SELECT MAX(k.logid) logid
	FROM dbo.LogSearches k
	WHERE 
			k.firmid = r.firmid
			k.userid = r.userid
			k.fileid = r.fileid
			k.filetype = r.filetype
			k.searchdate = r.searchdate
)z
ORDER BY r.searchdate DESC;

Open in new window

0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
It will help if you can post some data sample. Like you explained isn't really clear to me so I'm asking if removing the DISTINCT keyword it makes any difference for you:
SELECT TOP 6
        a.logid ,
        a.firmid ,
        a.userid ,
        a.fileid ,
        a.filetype ,
        a.searchdate ,
        b.FirstNm ,
        b.LastNm ,
        b.Dob ,
        b.DobD
FROM    dbo.LogSearches a
        INNER JOIN Users AS b ON b.UserId = a.fileid
WHERE   a.firmid = 2
        AND a.userid = 1713
        AND a.filetype = 'contact'
ORDER BY a.logid DESC

Open in new window

0
 
AleksAuthor Commented:
Pawan, got this error:

Msg 102, Level 15, State 1, Line 25
Incorrect syntax near 'k'.

Vitor, this are the results (Data)

890	2	1713	30638	contact	2016-09-29 13:12:00	Joe	Doe	07/18/2016	2016-07-18 00:00:00.000
887	2	1713	30638	contact	2016-09-28 16:25:00	Joe	Doe	07/18/2016	2016-07-18 00:00:00.000
881	2	1713	30638	contact	2016-09-21 12:12:00	Joe	Doe	07/18/2016	2016-07-18 00:00:00.000
812	2	1713	30746	contact	2016-08-04 09:32:00	Gina	CARDENAS	01/01/2000	2000-01-01 00:00:00.000
755	2	1713	31066	contact	2016-07-13 09:20:00	Maria	DOE	01/02/2000	2000-01-02 00:00:00.000
752	2	1713	31735	contact	2016-07-13 09:13:00	Patti	Doe	06/22/1977	1977-06-22 00:00:00.000

Open in new window


As you can see the logid is different but should NOT be compared in the distinct clause, neither should the 'searchdate' field. the rest should be compared, but not those two because they will be different and used to order BY.
0
 
Scott PletcherSenior DBACommented:
SELECT  /*logid ,*/ /*optional*/
        firmid ,
        userid ,
        fileid ,
        filetype ,
        searchdate ,
        FirstNm ,
        LastNm ,
        Dob ,
        DobD
FROM (
    SELECT  a.logid ,
            a.firmid ,
            a.userid ,
            a.fileid ,
            a.filetype ,
            a.searchdate ,
            b.FirstNm ,
            b.LastNm ,
            b.Dob ,
            b.DobD ,
            ROW_NUMBER() OVER(PARTITION BY
            a.logid ,
            a.firmid ,
            a.userid ,
            a.fileid ,
            a.filetype ,
            a.searchdate ,
            b.FirstNm ,
            b.LastNm ,
            b.Dob ,
            b.DobD ORDER BY a.logid DESC) AS row_num
    FROM    dbo.LogSearches a
            INNER JOIN Users AS b ON b.UserId = a.fileid
    WHERE   a.firmid = 2
            AND a.userid = 1713
            AND a.filetype = 'contact'
) AS derived
WHERE row_num <= 6
ORDER BY logid DESC;
0
 
AleksAuthor Commented:
Scott,

I still get duplicates.

'searchdate' and 'logid' should NOT be compared.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Vitor, this are the results (Data)
Is what you're expecting to be returned? If so, then please add some raw data as well so we can have an idea which 6 records we should return from the universe of your data.
0
 
AleksAuthor Commented:
Please see above. Those 6 show 3 duplicate records. Same name and same DOB.
In fact the USERID is the one that should be unique. The rest of the columns don't matter
0
 
Pawan KumarDatabase ExpertCommented:
Sorry missed AND

Updated

SELECT TOP 6 z.logid , r.* FROM 
(
	SELECT DISTINCT 
			a.firmid ,
			a.userid ,
			a.fileid ,
			a.filetype ,
			a.searchdate ,
			b.FirstNm ,
			b.LastNm ,
			b.Dob ,
			b.DobD
	FROM    dbo.LogSearches a
			INNER JOIN Users AS b ON b.UserId = a.fileid
	WHERE   a.firmid = 2
			AND a.userid = 1713
			AND a.filetype = 'contact'
)r
CROSS APPLY
(	
	SELECT MAX(k.logid) logid
	FROM dbo.LogSearches k
	WHERE 
			k.firmid = r.firmid AND 
			k.userid = r.userid AND 
			k.fileid = r.fileid AND 
			k.filetype = r.filetype AND 
			k.searchdate = r.searchdate
)z
ORDER BY z.logid DESC;

Open in new window

0
 
Pawan KumarDatabase ExpertCommented:
Please let me if it still gives you duplicate Logids..
0
 
AleksAuthor Commented:
I still got duplicates. the 'a.userid' should be unique.

890	2	1713	30638	contact	2016-09-29 13:12:00	Joe	Doe	07/18/2016	2016-07-18 00:00:00.000
887	2	1713	30638	contact	2016-09-28 16:25:00	Joe	Doe	07/18/2016	2016-07-18 00:00:00.000
881	2	1713	30638	contact	2016-09-21 12:12:00	Joe	Doe	07/18/2016	2016-07-18 00:00:00.000
812	2	1713	30746	contact	2016-08-04 09:32:00	Gina	CARDENAS	01/01/2000	2000-01-01 00:00:00.000
755	2	1713	31066	contact	2016-07-13 09:20:00	Maria	DOE	01/02/2000	2000-01-02 00:00:00.000
752	2	1713	31735	contact	2016-07-13 09:13:00	Patti	Doe	06/22/1977	1977-06-22 00:00:00.000

Open in new window


As you can see Joe Doe is listed 3 times, the fileid is the same.

I apologize is not 'userid' that is unique, its the 'fileid'

I made the query simpler based on yours but I still get Joe Doe listed 3 times.

SELECT TOP 6 z.logid , r.* FROM 
(
	SELECT DISTINCT 
			a.fileid ,
			a.searchdate ,
			b.FirstNm ,
			b.LastNm ,
			b.DobD
	FROM    dbo.LogSearches a
			INNER JOIN Users AS b ON b.UserId = a.fileid
	WHERE   a.firmid = 2
			AND a.userid = 1713
			AND a.filetype = 'contact'
)r
CROSS APPLY
(	
	SELECT MAX(k.logid) logid
	FROM dbo.LogSearches k
	WHERE 
			k.fileid = r.fileid AND 
			k.searchdate = r.searchdate
)z
ORDER BY z.logid DESC;

Open in new window

0
 
Pawan KumarDatabase ExpertCommented:
@Author - have u tried my code <<Pawan>> ?
0
 
AleksAuthor Commented:
I just did, above the results.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Those 6 show 3 duplicate records. Same name and same DOB.
Your definition of duplicate is not the same for a database and I think that's why a lot of confusion is still here. Also, that's why posting sample data of the source and the expected results helps a lot who is trying to give you a solution.
0
 
Vitor MontalvãoConnect With a Mentor MSSQL Senior EngineerCommented:
Try this:
SELECT TOP 6
        MAX(a.logid) LogID,
        a.firmid,
        a.userid,
        a.fileid,
        a.filetype,
        MAX(a.searchdate),
        b.FirstNm,
        b.LastNm,
        b.Dob,
        b.DobD
FROM    dbo.LogSearches a
        INNER JOIN Users AS b ON b.UserId = a.fileid
WHERE   a.firmid = 2
        AND a.userid = 1713
        AND a.filetype = 'contact'
GROUP BY a.userid,
        a.fileid,
        a.filetype,
        b.FirstNm,
        b.LastNm,
        b.Dob,
        b.DobD
ORDER BY 1 DESC

Open in new window

0
 
Pawan KumarDatabase ExpertCommented:
Vitor is correct. You are running a different query. My query should return 7 columns only, Your output is  giving us more column than that.

Please provide us more info - full requirement , expected results etc.

Are you taking more columns in the select.
0
 
Alexandre SimõesConnect With a Mentor Manager / Technology SpecialistCommented:
Hi mate,
I think what you need is something more like the bellow.

A warning tho, I couldn't test this so it's possible that I have some typos.
The idea is that we do a groupby with a max date and without the id.
Then we re-join with the same table, including the max date field.

This strategy won't work if you have two logs exactly with the same date...
SELECT TOP(6) 
        t.logid, 
        t.firmid ,
        t.userid ,
        t.fileid ,
        t.filetype ,
        t.SearchDate,
        t.FirstNm ,
        t.LastNm ,
        t.Dob ,
        t.DobD
FROM (
      SELECT 
        a.firmid ,
        a.userid ,
        a.fileid ,
        a.filetype ,
        MAX(a.searchdate) MaxSearchDate,
        b.FirstNm ,
        b.LastNm ,
        b.Dob ,
        b.DobD
      FROM    dbo.LogSearches a
        INNER JOIN Users AS b ON b.UserId = a.fileid
      WHERE   a.firmid = 2
        AND a.userid = 1713
        AND a.filetype = 'contact'
      GROUP BY 
        a.firmid ,
        a.userid ,
        a.fileid ,
        a.filetype ,
        b.FirstNm ,
        b.LastNm ,
        b.Dob ,
        b.DobD
) r
INNER JOIN dbo.LogSearches t
ON 
        r.firmid = t.firmid AND
        r.userid = t.userid AND
        r.fileid = t.fileid AND
        r.filetype = t.filetype AND
        r.FirstNm = t.FirstNm AND
        r.LastNm = t.LastNm AND
        r.Dob = t.Dob AND
        r.DobD = t.DobD AND
        r.MaxSearchDate = t.SearchDate

Open in new window

0
 
AleksAuthor Commented:
Victor:

Msg 8120, Level 16, State 1, Line 24
Column 'dbo.LogSearches.firmid' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Alexandre:

Msg 207, Level 16, State 1, Line 44
Invalid column name 'FirstNm'.
Msg 207, Level 16, State 1, Line 45
Invalid column name 'LastNm'.
Msg 207, Level 16, State 1, Line 46
Invalid column name 'Dob'.
Msg 207, Level 16, State 1, Line 47
Invalid column name 'DobD'.
Msg 207, Level 16, State 1, Line 8
Invalid column name 'FirstNm'.
Msg 207, Level 16, State 1, Line 9
Invalid column name 'LastNm'.
Msg 207, Level 16, State 1, Line 10
Invalid column name 'Dob'.
Msg 207, Level 16, State 1, Line 11
Invalid column name 'DobD'.


Pawan: Seems to have worked. I will test and confirm.
0
 
AleksAuthor Commented:
Pawan's code worked. the rest gave errors. I will assign points to Pawan since its the only answer that worked with no errors. I will post a new sql that is very similar to this query in one minute.
0
 
AleksAuthor Commented:
Assigned some points to assited solution.
0
 
Scott PletcherSenior DBACommented:
Sorry, I meant to remove logid from the PARTITION BY; and I will remove searchdate also.

SELECT  /*logid ,*/ /*optional*/
        firmid ,
        userid ,
        fileid ,
        filetype ,
        searchdate ,
        FirstNm ,
        LastNm ,
        Dob ,
        DobD
FROM (
    SELECT  *,
            ROW_NUMBER() OVER(PARTITION BY
            a.firmid ,
            a.userid ,
            a.fileid ,
            a.filetype ,
            b.FirstNm ,
            b.LastNm ,
            b.Dob ,
            b.DobD ORDER BY a.logid DESC) AS row_num
    FROM    dbo.LogSearches a
            INNER JOIN Users AS b ON b.UserId = a.fileid
    WHERE   a.firmid = 2
            AND a.userid = 1713
            AND a.filetype = 'contact'
) AS derived
WHERE row_num <= 6
ORDER BY logid DESC;
0
 
AleksAuthor Commented:
Scott:  I tried your code above and got this error:

Msg 8156, Level 16, State 1, Line 52
The column 'UserId' was specified multiple times for 'derived'.

I used Pawan's code and it worked fine.

Just posted a similar query.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Sorry, just could see the comments now. I've forgot to add one column to the GROUP BY clause:
SELECT TOP 6
        MAX(a.logid) LogID,
        a.firmid,
        a.userid,
        a.fileid,
        a.filetype,
        MAX(a.searchdate),
        b.FirstNm,
        b.LastNm,
        b.Dob,
        b.DobD
FROM    dbo.LogSearches a
        INNER JOIN Users AS b ON b.UserId = a.fileid
WHERE   a.firmid = 2
        AND a.userid = 1713
        AND a.filetype = 'contact'
GROUP BY a.firmid,
        a.userid,
        a.fileid,
        a.filetype,
        b.FirstNm,
        b.LastNm,
        b.Dob,
        b.DobD
ORDER BY 1 DESC

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.