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 6931019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:31:22+00:00 2026-05-27T11:31:22+00:00

For a class project, a few others and I have decided to make a

  • 0

For a class project, a few others and I have decided to make a (very ugly) limited clone of StackOverflow. For this purpose, we’re working on one query:

Home Page: List all the questions, their scores (calculated from votes), and the user corresponding to their first revision, and the number of answers, sorted in date-descending order according to the last action on the question (where an action is an answer, an edit of an answer, or an edit of the question).

Now, we’ve gotten the entire thing figured out, except for how to represent tags on questions. We’re currently using a M-N mapping of tags to questions like this:

CREATE TABLE QuestionRevisions (
id INT IDENTITY NOT NULL,
question INT NOT NULL,
postDate DATETIME NOT NULL,
contents NTEXT NOT NULL,
creatingUser INT NOT NULL,
title NVARCHAR(200) NOT NULL,
PRIMARY KEY (id),
CONSTRAINT questionrev_fk_users FOREIGN KEY (creatingUser) REFERENCES
Users (id) ON DELETE CASCADE,
CONSTRAINT questionref_fk_questions FOREIGN KEY (question) REFERENCES
Questions (id) ON DELETE CASCADE
);

CREATE TABLE Tags (
id INT IDENTITY NOT NULL,
name NVARCHAR(45) NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE QuestionTags (
tag INT NOT NULL,
question INT NOT NULL,
PRIMARY KEY (tag, question),
CONSTRAINT qtags_fk_tags FOREIGN KEY (tag) REFERENCES Tags(id) ON
DELETE CASCADE,
CONSTRAINT qtags_fk_q FOREIGN KEY (question) REFERENCES Questions(id) ON
DELETE CASCADE
);

Now, for this query, if we just join to QuestionTags, then we’ll get the questions and titles over and over and over again. If we don’t, then we have an N query scenario, which is just as bad. Ideally, we’d have something where the result row would be:

+-------------+------------------+
| Other Stuff | Tags             |
+-------------+------------------+
| Blah Blah   | TagA, TagB, TagC |
+-------------+------------------+

Basically — for each row in the JOIN, do a string join on the resulting tags.

Is there a built in function or similar which can accomplish this in T-SQL?

  • 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-27T11:31:23+00:00Added an answer on May 27, 2026 at 11:31 am

    Here’s one possible solution using recursive CTE:

    The methods used are explained here

    TSQL to set up the test data (I’m using table variables):

    DECLARE @QuestionRevisions TABLE  ( 
    id INT IDENTITY NOT NULL, 
    question INT NOT NULL, 
    postDate DATETIME NOT NULL, 
    contents NTEXT NOT NULL, 
    creatingUser INT NOT NULL, 
    title NVARCHAR(200) NOT NULL)
    
    DECLARE @Tags TABLE ( 
    id INT IDENTITY NOT NULL, 
    name NVARCHAR(45) NOT NULL
    )
    
    DECLARE @QuestionTags TABLE ( 
    tag INT NOT NULL, 
    question INT NOT NULL
    )
    INSERT INTO @QuestionRevisions 
    (question,postDate,contents,creatingUser,title)
    VALUES
    (1,GETDATE(),'Contents 1',1,'TITLE 1')
    
    INSERT INTO @QuestionRevisions 
    (question,postDate,contents,creatingUser,title)
    VALUES
    (2,GETDATE(),'Contents 2',2,'TITLE 2')
    
    INSERT INTO @Tags (name) VALUES ('Tag 1')
    INSERT INTO @Tags (name) VALUES ('Tag 2')
    INSERT INTO @Tags (name) VALUES ('Tag 3')
    INSERT INTO @Tags (name) VALUES ('Tag 4')
    INSERT INTO @Tags (name) VALUES ('Tag 5')
    INSERT INTO @Tags (name) VALUES ('Tag 6')
    
    INSERT INTO @QuestionTags (tag,question) VALUES (1,1)
    INSERT INTO @QuestionTags (tag,question) VALUES (3,1)
    INSERT INTO @QuestionTags (tag,question) VALUES (5,1)
    INSERT INTO @QuestionTags (tag,question) VALUES (4,2)
    INSERT INTO @QuestionTags (tag,question) VALUES (2,2)
    

    Here’s the action part:

    ;WITH CTE ( id, taglist, tagid, [length] ) 
          AS (  SELECT question, CAST( '' AS VARCHAR(8000) ), 0, 0
                FROM @QuestionRevisions qr
                GROUP BY question
                UNION ALL
                SELECT qr.id
                    ,  CAST(taglist + CASE WHEN [length] = 0 THEN '' ELSE ', ' END + t.name AS VARCHAR(8000) )
                    ,  t.id
                    ,  [length] + 1
                FROM CTE c 
                INNER JOIN @QuestionRevisions qr ON c.id = qr.question
                INNER JOIN @QuestionTags qt ON qr.question=qt.question
                INNER JOIN @Tags t ON t.id=qt.tag
                WHERE t.id > c.tagid )
    SELECT id, taglist 
    FROM ( SELECT id, taglist, RANK() OVER ( PARTITION BY id ORDER BY length DESC )
             FROM CTE ) D ( id, taglist, rank )
    WHERE rank = 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Project(models.Model): categories = models.ManyToManyField(Category) class Category(models.Model): name = models.CharField() now, i make some
I have a class named project with the following data members. class Project {
I have a Project model similar to: class Project(models.Model): ... lead_analyst=models.ForeignKey(User, related_name='lead_analyst') ... I
I'm working on a class project to build a little Connect4 game in Java.
I have a Class Library project and MVC project in the one solution. My
I have a custom project type based on the C# Class Library project type
I have a few static util methods in my project, some of them just
I have a class on low-level programming which requires a final project (syllabus at
I'm working on a Spring MVC project, and I have unit tests for all
I have posted a question on here previously asking similar advise, but this project

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.