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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:27:57+00:00 2026-06-16T03:27:57+00:00

Admitted SQL newbie here but I’m working on a query where the request just

  • 0

Admitted SQL newbie here but I’m working on a query where the request just got more complicated. Long short of it is that I have a table with a few columns. One of which is a date column.

I’m writing a query to return a set of columns along with adding a new column based on the date range (<=90 Days, 91-180, 180+). This is what I have so far. It works for the first 90 but I’m having a hard timing nesting the other select commands to get the other data sets.

Select Key, Name, 'C90' as Code
From LD_List
WHERE DATEDIFF(DAY,[LAST_PURCHASE_DATE],GETDATE()) <= 90

I appreciate the help.

  • 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-16T03:27:58+00:00Added an answer on June 16, 2026 at 3:27 am

    If you are using TSQL, SQL Server or MYSQL you may use, CASE WHEN : Key is a resreved keyword. So use squre brackets. In my sample I have used key as id.

    • SQLFIDDLE

    Query: SQL Server version:

    Select id, Name, 
    case when DATEDIFF(DAY,[LAST_PURCHASE_DATE],GETDATE()) <= 90 
    Then 'C90' 
    when (DATEDIFF(DAY,[LAST_PURCHASE_DATE],GETDATE()) >= 91 
    and DATEDIFF(DAY,[LAST_PURCHASE_DATE],GETDATE()) <= 180) 
    then 'C180' 
    else 'C181' 
    end AS Code
    From LD_List 
    

    Results:

    ID  NAME    CODE
    1   john    C181
    2   tim     C181
    3   jack    C181
    4   kate    C181
    5   jim     C181
    6   ron     C180
    7   henry   C180
    8   liza    C90
    

    EXTRA 🙂

    Now if you need a pivot like the following: Check this * SQLFIDDLE Demonstration out

    Results:

    CODE    PURCHASERS
    C180    ron,henry
    C181    john,tim,jack,kate,jim
    C90     liza
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Admitted, this doesn't sound like a best practice altogether, but let me explain. During
I'm an admitted novice JavaScript programmer and am attempting to learn more. So I
this is a bit of a newbie question but I can't seem to find
I've been reading a little about temporary tables in MySQL but I'm an admitted
I'm trying to evaluate NHibernate.LINQ 1.0 without actually writing any code. Ayende has admitted
I have multiple strings that are in the following format: 12/18/2009 02:08:26 Admitted Doe,
I'm new to this website. Here's a problem that troubled me for >2 hr.
'lo all. I am a self-described admitted noob in iPhone programming (having a much
I have now admitted defeat that you cannot use/build a good audio pitch function
I'll preface this by admitted that I may not totally understand the inner-workings of

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.