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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:51:53+00:00 2026-05-20T13:51:53+00:00

I am writing a query against an advanced many-to-many table in my database. I

  • 0

I am writing a query against an advanced many-to-many table in my database. I call it an advanced table because it is a many-to-many table with and extra field. The table maps data between the fields table and the students table. The fields table holds potential fields that a student can used, kind of like a contact system (i.e. name, school, address, etc). The studentvalues table that I need to query against holds the field id, student id, and the field answer (i.e. studentid=1; fieldid=2; response=Dave Long).

So my table looks like this:

Table Layout

What I need to do is take a few passed in values and create a grouped accumulated report. I would like to do as much in the SQL as possible.

So that data that I have will be the group by field (a field id), the cumulative field (a field id) and I need to group the students by the group by field and then in each group count the amount of students in the cumulative fields.

So for example I have this data

ID     STUDENTID     FIELDID     RESPONSE 
1      1             2 *(city)*  Wallingford
2      1             3 *(state)* CT
3      2             2 *(city)*  Wallingford
4      2             3 *(state)* CT
5      3             2 *(city)*  Berlin
6      3             3 *(state)* CT
7      4             2 *(city)*  Costa Mesa
8      4             3 *(state)* CA

I am hoping to write one query that I can generate a report that looks like this:

CA - 1 Student
Costa Mesa          1

CT - 3 Students
Berlin              1
Wallingford         2

Is this possible to do with a single SQL statement or do I have to get all the groups and then loop over them?

EDIT Here is the code that I have gotten so far, but it doesn’t give the proper stateSubtotal (the stateSubtotal is the same as the citySubtotal)

SELECT state, count(state) AS stateSubtotal, city, count(city) AS citySubtotal
FROM(
  SELECT s1.response AS city, s2.response AS state
  FROM studentvalues s1
  INNER JOIN studentvalues s2
    ON s1.studentid = s2.studentid
  WHERE s1.fieldid = 5
    AND s2.fieldid = 6
) t
GROUP BY city, state
  • 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-20T13:51:54+00:00Added an answer on May 20, 2026 at 1:51 pm

    So to make a table that looks like that, I would assume something like

    State  StateSubtotal   City         CitySubtotal
    CA     1               Costa Mesa   1
    CT     3               Berlin       1
    CT     3               Wallingford  2
    

    Would be what you want. We can’t just group on Response, since if you had a student answer LA for city, and another student that responds LA for state (Louisiana) they would add. Also, if the same city is in different states, we need to first lay out the association between a city and a state by joining on the student id.

    edit – indeed, flawed first approach. The different aggregates need different groupings, so really, one select per aggregation is required. This gives the right result but it’s ugly and I bet it could be improved on. If you were on SQL Server I would think a CTE would help but that’s not an option.

    select t2.stateAbb, stateSubtotal, t2.city, t2.citySubtotal from 
    (
    select city, count(city) as citySubTotal, stateAbb from (
    select s1.Response as city, s2.Response as StateAbb
    from aaa s1 inner join aaa s2 on s1.studentId = s2.studentId 
    where s1.fieldId = 2 and s2.fieldId=3
    ) t1 
    group by city, stateabb
    ) t2 inner join (
    select stateAbb, count(stateabb) as stateSubTotal from (
    select s1.Response as city, s2.Response as StateAbb
    from aaa s1 inner join aaa s2 on s1.studentId = s2.studentId 
    where s1.fieldId = 2 and s2.fieldId=3
    ) t3 
    group by stateabb
    ) t4 on t2.stateabb = t4.stateabb
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a query that inserts customer data into a MSSQL database. Very basic.
I was writing a query against a table today on a SQL Server 2000
I'm writing a query to get ALL of the products in the products table,
Doing a bit of investigation and writing a query against a logs db. I've
Ok so I am writing a report against a third party database which is
I'm writing a Rails application against a legacy database. One of the tables in
I need help writing a T-SQL query that will generate 52 rows of data
I am writing a query against a domain model where (for whatever reason) a
I am writing a LINQ query against the ObjectContext. What I essentially need to
I'm currently writing some installer script that fires SQL files against different database types

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.