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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:01:47+00:00 2026-06-05T22:01:47+00:00

I have a table with questions, where each row is a question and all

  • 0

I have a table with questions, where each row is a question and all questions has a asked_by and answered_by field. I want to select all unique combinations of “askers” and “answerers” and count how many times this combination of asking/answering have occurred.

question_tbl:

id      | asked_by | answered_by | session
--------+----------+-------------+---------
1       | AA       | JD          | 2011-2012
2       | JD       | AA          | 2011-2012
9       | AA       | JD          | 2011-2012
12      | AA       | JD          | 2009-2010

I want to return the unique combinations in in session, and the count for this combination.

E.g. for 2011-2012:

AA, JD, 2
JD, AA, 1

PS: Ideally I’d write this for the django orm, but as I can’t even seem to figure out how to formulate a sensible google search for this, to find the SQL-way is my goal.

Note after problem was solved:
As this turned out to be quite easy in SQL, it turned out to be quite easy in Djangos ORM too. I’ll leave it here for later record:

from django.db.models import Count
from myproject.models import Questions

Questions.objects.filter(sesjonid='2011-2012').values('sporsmal_til', 'sporsmal_fra').annotate(count=Count('besvart_av'))

By testing how that looks in SQL it seems to me to correct (in the ./manage.py shell):

print str(Questions.objects.filter(sesjonid='2011-2012').values('sporsmal_til', 'sporsmal_fra').annotate(count=Count('sporsmal_til')).query)

This prints SQL that does exactly the same as Bohemians’.

  • 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-05T22:01:48+00:00Added an answer on June 5, 2026 at 10:01 pm

    A straightforward COUNT(*) with a GROUP BY will do it:

    select asked_by, answered_by, count(*)
    from question_tbl
    where session = '2011-2012'
    group by 1, 2;
    

    If you wanted a query to return data for all sessions, use this:

    select session, asked_by, answered_by, count(*)
    from question_tbl
    group by 1, 2, 3;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with a list of businesses. Each row has details such
I have a table whose values are generated dynamically with PHP. Each row in
I have a table that has some content in it. Not all rows has
I have a page that creates a table using backbone.js. each table row is
I'm having a problem in a SQL command. I have a table with questions,
I have two Tables: Table 1: Questions : QuestionId NUMERIC Title TEXT Test Data
Requirements : I have a table of several thousand questions. Users can view these
I have a MS access database. In that, one table consists of questions and
have 2 questions : A computer with 32-bit address uses 2-level page table (9
I have the following setup: Table: Question QuestionId Table: QuestionTag QuestionId TagId Table: Tag

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.