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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:56:38+00:00 2026-06-15T11:56:38+00:00

I have this sample data +——+————+————+ | CODE | START_DATE | END_DATE | +——+————+————+

  • 0

I have this sample data

+------+------------+------------+
| CODE | START_DATE | END_DATE   |
+------+------------+------------+
| 0001 | 2012-01-01 | 2012-01-31 |
+------+------------+------------+
| 0001 | 2012-02-01 | 2012-02-29 |
+------+------------+------------+
| 0001 | 2012-03-01 | NULL       |
+------+------------+------------+
| 0002 | 2012-02-01 | 2012-02-29 |
+------+------------+------------+
| 0002 | 2012-03-01 | 2012-03-31 |
+------+------------+------------+
| 0002 | 2012-04-01 | NULL       |
+------+------------+------------+
| 0003 | 2012-02-01 | 2012-02-29 |
+------+------------+------------+
| 0003 | 2012-03-01 | 2012-03-31 |
+------+------------+------------+

DDL

CREATE TABLE SAMPLE
(
    CODE VARCHAR(4),
    START_DATE DATETIME,
    END_DATE DATETIME
)

INSERT INTO SAMPLE (CODE, START_DATE, END_DATE) VALUES ('0001', {d '2012-01-01'}, {d '2012-01-31'})
INSERT INTO SAMPLE (CODE, START_DATE, END_DATE) VALUES ('0001', {d '2012-02-01'}, {d '2012-02-29'})
INSERT INTO SAMPLE (CODE, START_DATE, END_DATE) VALUES ('0001', {d '2012-03-01'}, NULL)

INSERT INTO SAMPLE (CODE, START_DATE, END_DATE) VALUES ('0002', {d '2012-02-01'}, {d '2012-02-29'})
INSERT INTO SAMPLE (CODE, START_DATE, END_DATE) VALUES ('0002', {d '2012-03-01'}, {d '2012-03-31'})
INSERT INTO SAMPLE (CODE, START_DATE, END_DATE) VALUES ('0002', {d '2012-04-01'}, NULL)

INSERT INTO SAMPLE (CODE, START_DATE, END_DATE) VALUES ('0003', {d '2012-02-01'}, {d '2012-02-29'})
INSERT INTO SAMPLE (CODE, START_DATE, END_DATE) VALUES ('0003', {d '2012-03-01'}, {d '2012-03-31'})

I’m expected to get this data

+------+------------+------------+
| CODE | START_DATE | END_DATE   |
+------+------------+------------+
| 0001 | 2012-01-01 | NULL       |
+------+------------+------------+
| 0002 | 2012-02-01 | NULL       |
+------+------------+------------+
| 0003 | 2012-02-01 | 2012-03-31 |
+------+------------+------------+

currently in SQL Server 2005, I use this query

SELECT  CODE,
        MIN(START_DATE) AS START_DATE,
        CASE
            WHEN    MAX(ISNULL(END_DATE, {d '9999-12-31'})) = {d '9999-12-31'}
            THEN    NULL
        ELSE
            MAX(END_DATE)
        END AS END_DATE
FROM    SAMPLE
GROUP BY    CODE

I’m cheating NULL value to date 9999-12-31

I’m looking for better solution for this

thank you

  • 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-15T11:56:39+00:00Added an answer on June 15, 2026 at 11:56 am

    You could utilize the fact that the COUNT of NULL and NOT NULL columns will be different. So the script can go like this:

    SELECT  CODE,
            MIN(START_DATE) AS START_DATE,
            CASE 
                WHEN    COUNT(*) > COUNT(END_DATE)
                THEN    NULL
            ELSE
                MAX(END_DATE)
            END AS END_DATE
    FROM    SAMPLE
    GROUP BY    CODE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this sample code to create a new data frame 'new_data' from the
i have this simple code to load data from other php page using get
I have this sample code: DirectoryEntry _entry = new DirectoryEntry( connectionString, this.userPrinicipalName, this.password, AuthenticationTypes.SecureSocketsLayer
I have this sample code for async operations (copied from the interwebs) public class
Say I have this sample code I'm writing for a Python lesson I'm holding:
I have this code sample, defining a fixture in a googletest. #include solver.h #include
I have this sample data akb.ggb@yahoo.com should output akb ggb sdsd.sdsd@gmail.com should output sdsd
I have this schema Sample Data | ID | TITLE | CONTROLLER | METHOD
I have created a image plot for this data: sample p p.1 p.2 p.3
I have this code and this one has more than one data from the

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.