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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:17:30+00:00 2026-06-06T06:17:30+00:00

SQL Server 2005 I have an SQL Function (ftn_GetExampleTable) which returns a table with

  • 0

SQL Server 2005

I have an SQL Function (ftn_GetExampleTable) which returns a table with multiple result rows

EXAMPLE

ID    MemberID MemberGroupID   Result1    Result2 Result3 Year Week
1     1        1               High Risk  2       xx      2011 22
2     11       4               Low Risk   1       yy      2011 21 
3     12       5               Med Risk   3       zz      2011 25
etc.

Now I do a count and group by on a table above this for Result 2 for instance so I get

SELECT MemberGroupID, Result2, Count(*) AS ExampleCount, Year, Week 
FROM ftn_GetExampleTable 
GROUP BY MemberGroupID, Result2, Year, Week

MemberGroupID  Result2  ExampleCount Year Week
1              2        4            2011 22
4              1        2            2011 21
5              3        1            2011 25

Now imagine when I go to graph this new table between Weeks 20 and 23 of Year 2011, you’ll see that it won’t graph 20 or 23 or certain groups or even certain results in this example as they are not in the included data, so I need “false data” inserted into this table which has all the possibilities so they at least show on a graph even if the count is 0, does this make sense?

I am wondering on the easiest and kind of most dynamic way as it could be Result1 or Result3 I want to Graph on (different column types).

Thanks in advance

  • 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-06T06:17:31+00:00Added an answer on June 6, 2026 at 6:17 am

    It looks like your dimensions are: MemberGroupID,Result2, and week (Year,Week).

    One approach to solving this is to generate a list of all values you want for all the dimensions, and produce a cartesian product of them. As an example,

    SELECT m.MemberGroupID, n.Result2, w.Year, w.Week
      FROM (SELECT MemberGroupID FROM ftn_GetExampleTable GROUP BY MemberGroupID) m
     CROSS
      JOIN (SELECT Result2       FROM ftn_GetExampleTable GROUP BY Result2      ) n
     CROSS
      JOIN (SELECT Year, Week FROM myCalendar WHERE ... ) w
    

    You don’t necessarily need a table named myCalendar. (That approach does seem to be the popular one.) You just need a row source from which you can derive a list of (Year, Week) tuples. (There are answers to the question elsewhere in Stackoverflow, how to generate a list of dates.)

    And the list of MemberGroupID and Result2 values doesn’t have to come from the ftn_GetExampleTable rowsource, you could substitute another query.

    With a cartesian product of those dimensions, you’ve got a complete “grid”. Now you can LEFT JOIN your original result set to that.

    Any place you don’t have a matching row from the “gappy” result query, you’ll get a NULL returned. You can leave the NULL, or replace it with a 0, which is probably what you want if it’s a “count” you are returning.

    SELECT d.MemberGroupID
         , d.Result2
         , d.Year
         , d.Week
         , IFNULL(r.ExampleCount,0) as ExampleCount
      FROM ( <dimension query from above> ) d
      LEFT
      JOIN ( <original ExampleCount query> ) r
        ON r.MemberGroupID  = d.MemberGroupID
        AND r.Result2       = d.Result2
        AND r.Year          = d.Year
        AND r.Week          = d.Week
    

    That query can be refactored to make use of Common Table Expressions, which makes the query a little easier to read, especially if you are including multiple measures.

    ; WITH d AS ( /* <dimension query with no gaps (example above)> */ 
           )
         , r AS ( /* <original query with gaps> */
                  SELECT MemberGroupID, Result2, Count(*) AS ExampleCount, Year, Week
                  FROM ftn_GetExampleTable
                  GROUP BY MemberGroupID, Result2, Year, Week
           )
    SELECT d.*
         , IFNULL(r.ExampleCount,0)
      FROM d
      LEFT 
      JOIN r 
        ON r.Year=d.Year AND r.Week=d.Week AND r.MemberGroupID = d.MemberGroupID
           AND r.Result2 = d.Result2
    

    This isn’t a complete working solution to your problem, but it outlines an approach you can use.

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

Sidebar

Related Questions

I have a user defined function in a SQL Server 2005 database which returns
We have a Single Statement FUNCTION in SQL Server 2005 which uses CONTAINSTABLE(). All
in context of SQL Server 2005, I have a table for which the primary
I have a sql server 2005 table which has xml store in a text
I have a table in SQL Server 2005 which has three columns: id (int),
[using SQL Server 2005] I have a table full of users, I want to
I'm using SQL Server 2005 express I have a datetime field in a table
On SQL Server 2005, I have a complex multi-level allocation process which looks like
In SQL Server 2005, I have a table of input coming in of successful
Environment: SQL Server 2005 I have a stored proc which receives comma separated value

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.