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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:49:58+00:00 2026-06-08T01:49:58+00:00

I’ve been working on this for 2 days and I can’t figure it out.

  • 0

I’ve been working on this for 2 days and I can’t figure it out. I’m hoping someone smarter than me will give this a go.

Let’s say I have the following tables:

Rating:
  Id | Name
  1  | A
  2  | B
  3  | C
  4  | D
  5  | E

Inspection:
  Id | Date (mm/dd/yyyy)
  1  | 01/04/2012
  2  | 04/04/2012
  3  | 28/03/2012
  4  | 04/04/2012

Observation:
  Id | InspectionId | RatingId
  1  | 2            | 3
  2  | 1            | 2
  3  | 4            | 3
  4  | 2            | 1
  5  | 3            | 3
  6  | 1            | 2

I want the query to return:

 RatingName | Date(mm/dd/yyyy) | ObservationCount
 A          | 01/04/2012       | 0
 B          | 01/04/2012       | 1
 C          | 01/04/2012       | 1
 D          | 01/04/2012       | 0
 E          | 01/04/2012       | 0
 A          | 04/04/2012       | 1
 B          | 04/04/2012       | 0
 C          | 04/04/2012       | 2
 D          | 04/04/2012       | 0
 E          | 04/04/2012       | 0
 A          | 28/03/2012       | 0
 B          | 28/03/2012       | 0
 C          | 28/03/2012       | 1
 D          | 28/03/2012       | 0
 E          | 28/03/2012       | 0

So I need the number of Observations for each rating for each date. And yes I need to have the records which return 0 Observations because I’m using this data in a stacked chart and without them it throws an error. I’ve managed to get the above table but without the records that return 0 Observations with the following Linq To Sql query, but from this point I get stuck.

MyDataContext DB = new MyDataContext();

var data = 
    (from r in DB.Ratings
    join o in DB.Observations on r.Id equals o.RatingId into ro

    from observation in ro.DefaultIfEmpty()
    join i in DB.Inspections on observation.InspectionId equals i.Id into roi

    from q in roi.DefaultIfEmpty()
    group q by new { Name = r.Name, Date = q.Date } into grouped
    select
    new
    {
        RatingName = grouped.Key.Name,
        Date = grouped.Key.Date,
        ObservationCount = grouped.Count(x => x != null)
    }).OrderBy(x => x.Date);

I would appreciate an answer in either Linq To Sql or just plain old SQL, 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-08T01:50:00+00:00Added an answer on June 8, 2026 at 1:50 am

    You should try a CROSS JOIN on the two reference tables, and then OUTER JOIN back to your ‘data’ table – and then check whether the Observation is null… then group and sum!

    SELECT 
        [Name],
        [Date],
        SUM([Exists])
    FROM
    (
    SELECT 
        name, 
        [Date],
        CASE WHEN o.Id IS NULL THEN 0
            ELSE 1
        END as [Exists]
    FROM 
        Rating r CROSS JOIN
        Inspection i
        LEFT OUTER JOIN Observation o 
            ON o.RatingId = r.Id AND o.InspectionId = i.Id
    ) as [source]
    GROUP BY [Name], [Date]
    ORDER BY
        [Date],
        name
    

    Translating to LINQ would be a similar two-step process – get the inner result set (checking whether observation is NULL), before grouping and summing.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.