Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6053693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:04:10+00:00 2026-05-23T08:04:10+00:00

declare @userid int set @userid=9846 SELECT TOP 10 tagid, [Description] FROM tagsuggestion WHERE tagid

  • 0
declare  @userid int
set @userid=9846
SELECT TOP 10 
          tagid, 
          [Description]
FROM   tagsuggestion 
WHERE  tagid IN (SELECT **DISTINCT** TOP 10  tagid
             FROM  (SELECT ulpt.tagid, 
                           createddate 
                    FROM   userlocationposttag ulpt 
                            WHERE  ulpt.UserID=@userid                                

                    UNION ALL 
                    SELECT ult.tagid, 
                           createddate 
                    FROM   userlocationtag ult 
                             WHERE  ult.UserID=@userid
                    UNION ALL 
                    SELECT upt.tagid, 
                           createddate 
                    FROM   userprofiletag upt 
                             WHERE  upt.UserID=@userid
) T

             ORDER  BY createddate DESC)  

The above query is error ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

.but if i remove DISTINCT this query runs

I want to ensure the distinctness and i also want to keep the sort order by createddate DESC

one of my associated post is

Last created 10 records from all of the 3 tables

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:04:11+00:00Added an answer on May 23, 2026 at 8:04 am

    Okay, so we only care about each tag based on it’s most recent created date, so we can structure the inner query as:

    SELECT tagid,MAX(createddate) as createddate FROM (
    SELECT ulpt.tagid, 
           createddate 
    FROM   userlocationposttag ulpt 
    WHERE  ulpt.UserID=@userid                                
    UNION ALL 
    SELECT ult.tagid, 
           createddate 
    FROM   userlocationtag ult 
    WHERE  ult.UserID=@userid
    UNION ALL 
    SELECT upt.tagid, 
           createddate 
    FROM   userprofiletag upt 
    WHERE  upt.UserID=@userid
    ) t
    GROUP BY tagid
    

    And then we can wrap that in another subselect, and apply the top ten:

    SELECT TOP 10 tagid FROM (
    SELECT tagid,MAX(createddate) as createddate FROM (
    SELECT ulpt.tagid, 
           createddate 
    FROM   userlocationposttag ulpt 
    WHERE  ulpt.UserID=@userid                                
    UNION ALL 
    SELECT ult.tagid, 
           createddate 
    FROM   userlocationtag ult 
    WHERE  ult.UserID=@userid
    UNION ALL 
    SELECT upt.tagid, 
           createddate 
    FROM   userprofiletag upt 
    WHERE  upt.UserID=@userid
    ) t
    GROUP BY tagid
    ) t ORDER BY createddate desc
    

    And we no longer need distinct (because GROUP BY and MAX already ensured that each tag only appears once)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

DECLARE @Symb varchar SET @Symb = 'Lara' SELECT Names FROM Cricket where Names like
I've the two following queries: declare @UserId as int set @UserId = 1 --
DECLARE @TotalQuestions int; DECLARE @CorrectQuestions int; DECLARE @IncorrectQuestions int; SELECT ( SET CorrectQuestion =
DECLARE @T INT EXEC('SELECT @T = count(user_id) FROM ( .. .. .. )') PRINT
DECLARE @table table(XYZ VARCHAR(8) , id int) INSERT INTO @table SELECT '4000', 1 UNION
declare @servername varchar(2000) set @EmriServerit=(select @@servername) declare @dbname varchar(2000) set @dbname ='Test1' declare @Dir
declare c int set c = 1 while c<700 do update users set profile_display_name
I want to do this: Declare @a int; Declare @b int; SET @a,@b =
ALTER PROCEDURE Sp_Num_Tracking @UserID int AS BEGIN declare @lastLogonDate datetime; ALTER PROCEDURE Sp_num_Tracking @UserID
say I had a stored proc like this: BEGIN DECLARE user_id INT; SET user_id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.