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

  • Home
  • SEARCH
  • 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 7077859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:28:31+00:00 2026-05-28T06:28:31+00:00

I have a single database table that stores week entries. Id Value WeekId 1

  • 0

I have a single database table that stores week entries.

Id        Value     WeekId
1         1.0000    1
2         2.0000    1

There can be up to three entries with the same week.

So I figured using a self join would solve this

SELECT w1.Value, w2.Value, w3.Value 
FROM [List].[dbo].[testWeekEntries] as w1 
LEFT OUTER JOIN [List].[dbo].[testWeekEntries] as w2 ON w1.WeekId = w2.weekId 
LEFT OUTER JOIN [List].[dbo].[testWeekEntries] as w3 ON w2.WeekId = w3.WeekId 
WHERE w1.Id < w2.Id AND w2.Id < w3.Id

The problem: It worls fine with the maximum number of entries however it doesn’t pull back a row with one or two entries.

Is there a different type of join I can use to pull back a row with only one or two entries or a different way of approaching this?

  • 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-28T06:28:32+00:00Added an answer on May 28, 2026 at 6:28 am

    These entries are not returning because your WHERE clause explicitly filters them out when the joined tables return NULL values.

    This solution adds a sequential rownumber to each record, restarting to 1 for each week. This allows you to use this sequential number in a PIVOT statement

    SQL 2000 Statement

    SELECT  *
    FROM    (
              SELECT  (SELECT  COUNT(*) 
                       FROM    testWeekEntries 
                       WHERE   Id <= we.Id 
                               AND WeekId = we.WeekId) as rn
                      , Value
                      , WeekId
              FROM    testWeekEntries we
            ) q
    PIVOT   (MAX(Value) FOR rn IN ([1],[2],[3]) ) AS PVT
    

    SQL 2008 Statement

    ;WITH q AS (
    SELECT  rn = ROW_NUMBER() OVER (PARTITION BY WeekId ORDER BY Id)
            , Id
            , Value
            , WeekId
    FROM    [testWeekEntries] as w1 
    )
    SELECT  Value
            , (SELECT Value FROM q q1 WHERE q1.rn = q.rn + 1 AND q1.WeekId = q.WeekId)
            , (SELECT Value FROM q q2 WHERE q2.rn = q.rn + 2 AND q2.WeekId = q.WeekId)
    FROM    q
    WHERE   q.rn = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that stores a value that will be added to over
I have a database table that stores imported information. For simplicity, its something like:
I have two database tables. One table stores data for a commitment that a
Say I have a database that stores table data by year using an identical
I have a forum database that stores forum information in a single column. The
I have several database tables that just contain a single column and very few
I have a database containing a single huge table. At the moment a query
I have a fairly huge database with a master table with a single column
Let's say you have a database with a single table like... --------------------------------------------- | Name
I have a database that is stuck in single-user mode. I kill the process

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.