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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:00:34+00:00 2026-06-16T00:00:34+00:00

ID M1 M2 M3 M4 DateM2 DateM3 DateM4 1 2 NULL NULL NULL NULL

  • 0
ID    M1      M2       M3    M4       DateM2        DateM3      DateM4 
1     2       NULL     NULL  NULL     NULL          NULL        NULL
2     NULL     2       1     1        11.11.2011    15.11.2011  20.11.2011
3     1       NULL     NULL  NULL     NULL          NULL        NULL
4     2       3        1     2        11.12.2012    12.12.2012  13.12.2012
5     1       1        1     1        11.10.2012    12.10.2012  13.10.2012
6     2       2        2     2        12.9.2012     12.12.2012  1.2.2013

i want to produce the following table which will have the ID column and a new Column

ID M1+M2+M3+M4
1  2
2  4
3  1
4  8
5  4
6  8

the new column will be M1+M2+M3+M4 with the following rules

  1. M1 added always to the sum and if its null i set it to 0
  2. M2 is added to the sum if DateM2 wasnt NULL AND DATE1 < DateM2 < DATE2
  3. M3 is added to the sum if DateM3 wasnt NULL AND DATE1 < DateM3 < DATE2
  4. M4 is added to the sum if DateM4 wasnt NULL AND DATE1 < DateM4 < DATE2

DATE1 and DATE2 are variables specified in the query, now my problem with this i am not sure what to use for the performance i have tens of thausands of records, should i use select case, you are welcome to help the way you can!

  • 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-16T00:00:35+00:00Added an answer on June 16, 2026 at 12:00 am

    Something like this:

    SELECT
    t.ID,
    isnull(t.M1,0)+
    CASE WHEN t.DateM2 is not null AND @DATE1 < t.DateM2 < @DATE2
         THEN t.M2
         ELSE 0 END +
    CASE WHEN t.DateM3 is not null AND @DATE1 < t.DateM3 < @DATE2
         THEN t.M3 
         ELSE 0 END +
    CASE WHEN t.DateM4 is not null AND @DATE1 < t.DateM4 < @DATE2
         THEN t.M4 
         ELSE 0 END AS 'M1+M2+M3+M4'
    FROM Table1 t
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this table in database which called data and i want make this
I have a list of List<KeyvaluePair<DateTime, int>> , which I want to merge into
So I have the following membership history table User_ID | Start date | End
I have the following table: date1 date2 sc cash date 2010-09-20 2010-09-21 202 300
I have a pretty simple table here: Table1 ID Date1 Date2 Date3 I want
i have table MyTbl that contain date1, date2, date3 if date1=null i need that
In my view I have the following code: @using (Html.BeginForm()) { @Html.TextBox(Date2, Model.Date2) <br/>
I have created a datagrid in ActionScript class and I want to add an
table1 - date1 datetime not null - date2 nvarchar null I want to get
I want to pull out the records that have a date that is LIKE

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.