Avatar of Aleks
Aleks
Flag for United States of America asked on

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

Web Languages and StandardsMicrosoft SQL ServerWeb DevelopmentASPSQL

Avatar of undefined
Last Comment
Vitor Montalvão

8/22/2022 - Mon
Alexandre Simões

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
Aleks

ASKER
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.
Pawan Kumar

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Vitor Montalvão

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

Aleks

ASKER
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.
Scott Pletcher

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;
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Aleks

ASKER
Scott,

I still get duplicates.

'searchdate' and 'logid' should NOT be compared.
Vitor Montalvão

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.
Aleks

ASKER
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
Your help has saved me hundreds of hours of internet surfing.
fblack61
Pawan Kumar

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

Pawan Kumar

Please let me if it still gives you duplicate Logids..
Aleks

ASKER
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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Pawan Kumar

@Author - have u tried my code <<Pawan>> ?
Aleks

ASKER
I just did, above the results.
Vitor Montalvão

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
SOLUTION
Vitor Montalvão

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Pawan Kumar

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.
ASKER CERTIFIED SOLUTION
Pawan Kumar

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Alexandre Simões

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Aleks

ASKER
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.
Aleks

ASKER
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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Aleks

ASKER
Assigned some points to assited solution.
Scott Pletcher

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;
Aleks

ASKER
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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Vitor Montalvão

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