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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:40:14+00:00 2026-06-15T15:40:14+00:00

I Have a table ‘C’ in MySQL db with the following structure: Object1Id (int)

  • 0

I Have a table ‘C’ in MySQL db with the following structure:

Object1Id (int) – PK and has one to many relation to table ‘A’,

Object2Id (int) – PK and has one to many relation to table ‘A’,

OccuranceId (int) – FK and has one to many relation to table ‘B’.

Basically table ‘C’ has many to many relation with table ‘A’ and one to many relation with table ‘B’.

The data:

1, 3, 1
1, 3, 39
3, 1, 402
3, 1, 27
1, 3, 40
3, 1, 12

I try to group the records with the query:

SELECT CASE WHEN least(Object1Id, Object2Id) = Object1Id THEN Object1Id ELSE Object2Id END AS Object1Id, 
       CASE WHEN Greatest(Object1Id, Object2Id) = Object2Id THEN Object2Id ELSE Object1Id END AS Object2Id, 
       count(OccuranceId) AS OccuranceCount
FROM 'C'
GROUP BY Object1Id, Object2Id

The result I get back from the query are:

1, 3, 3 – for object1Id = 1 and Object2Id = 3

1, 3, 3 – for Object1Id = 3 and Object2Id = 1

I would like the result to sum up as : 1, 3, 6

How can I produce the desired result?

Thanks.

  • 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-06-15T15:40:15+00:00Added an answer on June 15, 2026 at 3:40 pm

    First I would simplify the query to

    SELECT least(Object1Id, Object2Id) AS Object1Id,
           Greatest(Object1Id, Object2Id) AS Object2Id,
           count(OccuranceId) AS OccuranceCount
    FROM C
    GROUP BY Object1Id, Object2Id
    

    Your query doesn’t work, because you have columns object1id and object2id and reuse these names for the result columns least() and greatest(). But the group by clause groups by the table columns not your result columns. When you rename the result columns to some other names, it works as you expect

    SELECT least(Object1Id, Object2Id) AS least_val,
           Greatest(Object1Id, Object2Id) AS greatest_val,
           count(OccuranceId) AS OccuranceCount
    FROM C
    GROUP BY least_val, greatest_val
    

    SQL Fiddle for testing

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

Sidebar

Related Questions

I have table the following data structure in SQL Server: ID Date Allocation 1,
I have table X, and each record in X has many records in Y.
I have table tab1 (pseudo-structure) tab1 { fname char(20) code int } I have
I have table which has one field (Information). Information =========== Hello World 1 This
I have table in my database which has fields of ID,NAME,CONTEXT. I am showing
I have table with data 1/1 to 1/20 in one column. I want the
I have table inside a div tab. The table has 40 rows in it
I have table which has a class data and it has table rows inside
I have table cars: Id int, Model nvarchar(max), DateOfProduction (datetime). And data like: 1
I have table with following details Table name EMPLOYEE and columns EMPID (PK smallint

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.