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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:11:57+00:00 2026-05-16T23:11:57+00:00

I was wondering if it was possible to determine from which column a coalesce

  • 0

I was wondering if it was possible to determine from which column a coalesce value is drawn from?

I have the following example data (actual years range from 1989 – 2010 in data, not shown for brevity)

ID    |   2000  | 2000 value | 2001  |2001 value |  2002  |2002 value | 2003  |2003 value |  2004  | 2004 value | 2005 | 2005 value
id001 |  single | 15.46      |regular|50         | NULL   | 0         |single | 152       | regular|15.20       |single| 15.99
id002 | regular | 20.46      |regular|17.99      |single  | 150.23    |both   |  256.3    | NULL   |  0         | NULL |  0

Where single / regular / both reflect how that ID paid for something in that year (and NULL represents no purchases).

What I would ideally like to have is a three columns per year for the years 2005-2010 tells you the most recent single payment type before that year (and in which year it falls), as well as a column for regular and both payment types

So for the example above the results would look like:

ID    |   2005 prior single year  |  2005 prior regular year  |  2005 prior both year
id001 |   2003                    |  2004                     | NULL
id002 |   2002                    |  2001                     | 2003

I would also like to be able to pull out the respective values as well (for all years 2005-2010).

Fundamentally it’s just a case of looking across columns to find the first instance, but beyond some kind of coalesce I’m not sure how best to approach this!

Thanks! 🙂

  • 1 1 Answer
  • 1 View
  • 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-16T23:11:57+00:00Added an answer on May 16, 2026 at 11:11 pm

    First, I’d write a view to normalize the data:

    select  2000 as year
    ,       [2000 value] as value
    ,       [2000 type] as type
    from    YourTable
    where   year = 2000
    union all
    select  2001
    ,       [2001 value]
    ,       [2001 type]
    from    YourTable
    where   year = 2001
    ....
    

    Then you can look up the first year before 2005 like:

    select  a.year [prior to 2005]
    ,       a.value
    ,       a.id
    from    YourView a
    where   year = 
            (
            select  max(year) 
            from    YourView b 
            where   a.id = b.id 
                    and a.type = b.type 
                    and b.year < 2005
            )
    

    Once you have the normalized data, you can create many variations on this theme.

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

Sidebar

Related Questions

I was wondering if it's possible to determine what kind of iPhone (for example)
I am wondering if it possible to determine if an accepted socket connection has
Just wondering is it possible to build CLI app that can be run from
I am wondering if its possible to have a an (||)or or (&&)and operator
Suppose we have 2 classes, Child, and the class from which it inherits, Parent.
Playing around with MapKit and I'm wondering ... Is it possible to determine where
I am wondering if it is possible to use XPath or XSL to determine
I was wondering if it possible to determine if the user has enabled the
I am calling python functions from C++. I was wondering if it is possible
just wondering if its possible to have a hidden parent and a visible child

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.