Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Count (Distinct) with left join and group by

Count (Distinct) with left join and group by

Scheduled Pinned Locked Moved LINQ
databasecsharplinqhelptutorial
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mark Jerzykowski
    wrote on last edited by
    #1

    Hi, Pretty new to Linq and struggling to get my head around it a bit. Here goes.... Survey Tool with a simple (database) hierarchy of: Categories > Sub Categories > Questions > Answers and then another table called SurveyAnswers which basically stores a list of AnswerIds for each survey response. The questions are all multiple choice so you only ever get one SurveyAnswer for each Question (for a particular response). Now, I want to count the number of questions and the number of questions answered for each sub category. In sql:

    select c.CategoryId,
    sc.SubCategoryId,
    COUNT(distinct q.QuestionId) as NumberOfQuestions,
    COUNT(sa.AnswerId) as QuestionsAnswered
    from Category c
    join SubCategory sc
    on c.CategoryId = sc.CategoryId
    join Question q
    on sc.SubCategoryId = q.SubCategoryId
    join Answer a
    on q.QuestionId = a.QuestionId
    left join SurveyAnswer sa
    on a.AnswerId = sa.AnswerId
    and sa.SurveyResponseId = 1
    group by c.CategoryId,
    sc.SubCategoryId

    Any help with showing me how to do this would be great. I can get to the left outer join bit but the select and group bits defeat me! Thanks.

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups