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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:59:03+00:00 2026-05-15T15:59:03+00:00

I know that the question sounds like this is going to be an easy

  • 0

I know that the question sounds like this is going to be an easy question, but let me explain. I have a result set where I get

     Count    Team   Score  Team2   Score
-----------------------------------------
        10    TeamA    1    TeamB     2
        7     TeamB    2    TeamA     1

Now, because I have the same result, but the Teams are in different columns I get the 2 results. I am looking for a way of retrieving the one result like so:

     Count    Team   Score  Team2   Score
-----------------------------------------
        17    TeamA    1    TeamB     2

Is this possible?

EDIT

SELECT TOP 5 SUM([CountryCount]) AS [CountryCount]
      ,[Country], [CustomFieldB], [CustomFieldC], [CustomFieldD]
  FROM (
    SELECT COUNT([Country]) AS [CountryCount], [CustomFieldB], [CustomFieldC], [CustomFieldD]
          ,[Country]
      FROM (
        SELECT [CustomFieldA] AS [Country], [CustomFieldB], [CustomFieldC], [CustomFieldD]
          FROM [Target]
         WHERE [TargetListID] = xxx
        ) as tbl
     GROUP
        BY [Country], [CustomFieldB], [CustomFieldC], [CustomFieldD]
    ) as T
 GROUP
    BY [Country], [CustomFieldB], [CustomFieldC], [CustomFieldD]
 ORDER
    BY [CountryCount] DESC;
  • 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-15T15:59:04+00:00Added an answer on May 15, 2026 at 3:59 pm

    The following code accomplishes the desired results using a Common Table Expression and a UNION ALL. The data was made up using the query and results provided.

    DECLARE @Target TABLE (
        [TargetListID] int,
        [CustomFieldA] varchar(5),
        [CustomFieldB] int,
        [CustomFieldC] varchar(5),
        [CustomFieldD] int
        ) 
    
    INSERT INTO @Target
        VALUES
            (1, 'TeamA', 1, 'TeamB', 2),
            (1, 'TeamA', 1, 'TeamB', 2),
            (1, 'TeamA', 1, 'TeamB', 2),
            (1, 'TeamA', 1, 'TeamB', 2),
            (1, 'TeamA', 1, 'TeamB', 2),
            (1, 'TeamA', 1, 'TeamB', 2),
            (1, 'TeamA', 1, 'TeamB', 2),
            (1, 'TeamA', 1, 'TeamB', 2),
            (1, 'TeamA', 1, 'TeamB', 2),
            (1, 'TeamA', 1, 'TeamB', 2),
            (1, 'TeamB', 2, 'TeamA', 1),
            (1, 'TeamB', 2, 'TeamA', 1),
            (1, 'TeamB', 2, 'TeamA', 1),
            (1, 'TeamB', 2, 'TeamA', 1),
            (1, 'TeamB', 2, 'TeamA', 1),
            (1, 'TeamB', 2, 'TeamA', 1),
            (1, 'TeamB', 2, 'TeamA', 1)
    
    ;WITH CTE AS (
        SELECT
                COUNT([CustomFieldA]) AS [Count],
                [CustomFieldA] AS [Team1],
                [CustomFieldB] AS [Team1Score],
                [CustomFieldC] AS [Team2],
                [CustomFieldD] AS [Team2Score]
            FROM (
                SELECT
                        [CustomFieldA],
                        [CustomFieldB],
                        [CustomFieldC],
                        [CustomFieldD]
                    FROM @Target
                    WHERE [TargetListID] = 1
                ) AS tbl
            GROUP BY
                [CustomFieldA],
                [CustomFieldB],
                [CustomFieldC],
                [CustomFieldD]
        )
    SELECT
            SUM([Count]) AS [Count],
            Team1 AS Team,
            Team1Score AS Score,
            Team2 AS Team2,
            Team2Score AS Score
        FROM (
            SELECT
                    CTE.[Count],
                    CTE.[Team1],
                    CTE.[Team1Score],
                    CTE.[Team2],
                    CTE.[Team2Score]
                FROM CTE
                WHERE CTE.[Team1Score] <= CTE.[Team2Score]
            UNION ALL
            SELECT
                    CTE.[Count],
                    CTE.[Team2],
                    CTE.[Team2Score],
                    CTE.[Team1],
                    CTE.[Team1Score]
                FROM CTE
                WHERE CTE.[Team2Score] < CTE.[Team1Score]
            ) AS U
        GROUP BY
            [Team1],
            [Team1Score],
            [Team2],
            [Team2Score]
        ORDER BY [Count] DESC
    

    This query gives the desired result.

    Count       Team  Score       Team2 Score
    ----------- ----- ----------- ----- -----------
    17          TeamA 1           TeamB 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question sounds weird, but please, let me explain myself. I'm using
This probably sounds like a stupid question, but I'm going to give it a
I know this sounds like a dumb question, but it's to settle an argument
I know that question sounds a little odd but i will try to explain
This sounds like a trivia question but I really need to know. If you
I know this sounds like a dumb question but I need to ask this.
This sounds like a simple question, but I don't know how to search for
I know that this is a simple question for PHP guys but I don't
I know this sounds like a subjective answer, but I will try to make
This is going to be a very simple question, I have code that looks

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.