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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:46:24+00:00 2026-06-10T01:46:24+00:00

See this fiddle . I have a table with questions where each question falls

  • 0

See this fiddle. I have a table with questions where each question falls into a category and I have to find user averages of each category. I think it is working fine but I wanted to add a total that shows the total number of answers being included in the average for each user. I can not figure out what to put in my where clause to actually return the total number of questions for each user. Whether I include userid,QID,or choice it is giving me astronomical numbers.

Query SQL:

DECLARE @tblTmpCatStats TABLE (userid NVARCHAR(10),cat1_mean FLOAT,cat2_mean FLOAT,cat3_mean FLOAT,cat4_mean FLOAT,N FLOAT)
INSERT INTO @tblTmpCatStats SELECT d.userid
    ,AVG(CAST(c1.choice AS FLOAT))
    ,AVG(CAST(c2.choice AS FLOAT))
    ,AVG(CAST(c3.choice AS FLOAT))
    ,AVG(CAST(c4.choice AS FLOAT))
    ,COUNT(d.userid)
FROM tblTmpDemographics d
JOIN tblTmpDemographics c1 ON d.userid = c1.userid
JOIN tblTmpDemographics c2 ON d.userid = c2.userid
JOIN tblTmpDemographics c3 ON d.userid = c3.userid
JOIN tblTmpDemographics c4 ON d.userid = c4.userid
WHERE c1.QID IN ('1','5')
AND c2.QID IN ('2','6')
AND c3.QID IN ('3','7')
AND c4.QID IN ('4','8')
GROUP BY d.userid
SELECT * FROM @tblTmpCatStats

I am trying to make N eqaul to the total number choices included in the AVG

Setup SQL:

CREATE TABLE tblTmpDemographics (userid NVARCHAR(10),QID INT,choice NVARCHAR(1000))
INSERT INTO tblTmpDemographics (userid,QID,choice)
SELECT 'user1',1,'5' UNION ALL SELECT 'user1',2,'3' UNION ALL
SELECT 'user1',3,'4' UNION ALL SELECT 'user1',4,'5' UNION ALL
SELECT 'user1',5,'5' UNION ALL SELECT 'user1',6,'3' UNION ALL
SELECT 'user1',7,'4' UNION ALL SELECT 'user1',8,'5' UNION ALL

SELECT 'user2',1,'3' UNION ALL SELECT 'user2',2,'2' UNION ALL
SELECT 'user2',3,'3' UNION ALL SELECT 'user2',4,'5' UNION ALL
SELECT 'user2',5,'3' UNION ALL SELECT 'user2',6,'2' UNION ALL
SELECT 'user2',7,'3' UNION ALL SELECT 'user2',8,'5' UNION ALL

SELECT 'user3',1,'2' UNION ALL SELECT 'user3',2,'1' UNION ALL
SELECT 'user3',3,'5' UNION ALL SELECT 'user3',4,'5' UNION ALL
SELECT 'user3',5,'2' UNION ALL SELECT 'user3',6,'1' UNION ALL
SELECT 'user3',7,'5' UNION ALL SELECT 'user3',8,'5' UNION ALL

SELECT 'user4',1,'4' UNION ALL SELECT 'user4',2,'3' UNION ALL
SELECT 'user4',3,'3' UNION ALL SELECT 'user4',4,'5' UNION ALL
SELECT 'user4',5,'4' UNION ALL SELECT 'user4',6,'3' UNION ALL
SELECT 'user4',7,'3' UNION ALL SELECT 'user4',8,'5' GO

Why is it returning 128 and not 8?

  • 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-10T01:46:25+00:00Added an answer on June 10, 2026 at 1:46 am

    Try this:

    SELECT d.userid
        ,AVG(CAST(c1.choice AS FLOAT))
        ,AVG(CAST(c2.choice AS FLOAT))
        ,AVG(CAST(c3.choice AS FLOAT))
        ,AVG(CAST(c4.choice AS FLOAT))
        , d.cnt
    FROM
    (
      SELECT userid, count(*) cnt
      from tblTmpDemographics
      group by userid
    ) d
    INNER JOIN tblTmpDemographics c1 
      ON d.userid = c1.userid
    INNER JOIN tblTmpDemographics c2 
      ON d.userid = c2.userid
    INNER JOIN tblTmpDemographics c3 
      ON d.userid = c3.userid
    INNER JOIN tblTmpDemographics c4 
      ON d.userid = c4.userid
    WHERE c1.QID IN ('1','5')
      AND c2.QID IN ('2','6')
      AND c3.QID IN ('3','7')
      AND c4.QID IN ('4','8')
    GROUP BY d.userid,  d.cnt
    

    See SQL Fiddle with Demo

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

Sidebar

Related Questions

See this fiddle: http://jsfiddle.net/uqJHf/ I have set the first column to show up with
Please see this fiddle for my situation. Essentially, I have a floating span which
Please see this Fiddle . QUESTION #1 If you click on Overview and click
I'm trying to have my table use 100% of the height. This fiddle shows
In this fiddle you see a table with a select-field where you should select
see this fiddle . This upon running gives an error in console. I'm currently
Please see this fiddle I want the scrolling div stop following when left div
See this question. How to show popup message like in stackoverflow I am trying
see this demo from jquery ui you have to hold down the Ctrl key
I'll preface this question by stating I have historically used tables for my HTML

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.