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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:24:08+00:00 2026-06-15T21:24:08+00:00

I need help to create a query in sql 2005. I have 4 dates:

  • 0

I need help to create a query in sql 2005.

I have 4 dates: 1 standard startdate and 3 date to compare.
I need to count the datediffs in days in ranges of 10.

datediff(day, startdate, enddate1)

How many times the range from 1 till 10, from 11 till 20 and from 21 till 30 will appear.

This for every 3 enddates!

Then a sum of all (3 enddates) 1 till 10 aso ( 510 times < 11, 275 times > 10 and < 21)

After this calculating the percentage of the ranges by all enddates.

Like 510/785 = XX.XX% and 275/785 = XX.XX%

Does I make myself clear?

Thanks in advance
1SeoAdvies

@mitch-wheat, It’s not a poor question it’s a difficult question to explain!
There is no table to explain this. Nearly Every table has a datatimefield!

I will try it again to explain this in very simple english.
I have a table with a datetime field. In a excelsheet I have an other datetime.
Both have the same key.
I know how to calculate the datediff in days.
I want to know how many time the datediff between 1 and 10 will appear.

example:

datediff(day, DateTime1, DateTime2) = 2

datediff(day, DateTime1, DateTime2) = 3

datediff(day, DateTime1, DateTime2) = 11

datediff(day, DateTime1, DateTime2) = 2

datediff(day, DateTime1, DateTime2) = 3

datediff(day, DateTime1, DateTime2) = 11

datediff(day, DateTime1, DateTime2) = 7

datediff(day, DateTime1, DateTime2) = 6

The result is

    < 11  = ***6*** (smaller then 11)

10 and <21 = 2 (between 10 and 21)

Sum the results 6 + 2 = 8

Calculate percentage:

smaller then 11 = 6\8 = 75%

Greater then 11 + <21 = 2\8 = 25%

The bold /invers items are the values I need!

Does I make myself now clear????????

Thanks in advance

  • 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-15T21:24:09+00:00Added an answer on June 15, 2026 at 9:24 pm

    SQL Fiddle

    MS SQL Server 2008 Schema Setup:

    create table YourTable
    (
      dt1 datetime,
      dt2 datetime
    )
    
    insert into YourTable values(getdate(), dateadd(day, 2, getdate()))
    insert into YourTable values(getdate(), dateadd(day, 3, getdate()))
    insert into YourTable values(getdate(), dateadd(day, 11, getdate()))
    insert into YourTable values(getdate(), dateadd(day, 2, getdate()))
    insert into YourTable values(getdate(), dateadd(day, 3, getdate()))
    insert into YourTable values(getdate(), dateadd(day, 11, getdate()))
    insert into YourTable values(getdate(), dateadd(day, 7, getdate()))
    insert into YourTable values(getdate(), dateadd(day, 6, getdate()))
    

    Query 1:

    select S1 / S2 as 'smaller than 11',
           (S2 - S1) / S2 as 'greater than 11 + < 21'
    from (
         select 1.0 * sum(case when datediff(day, dt1, dt2) < 11 then 1 else 0 end) as S1,
                1.0 * sum(case when datediff(day, dt1, dt2) < 21 then 1 else 0 end) as S2
         from YourTable
         where datediff(day, dt1, dt2) < 21
         ) as T
    

    Results:

    | SMALLER THAN 11 | GREATER THAN 11 + < 21 |
    --------------------------------------------
    |            0.75 |                   0.25 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help with SQL. I have an sqlite table like so; CREATE TABLE
I need help creating an SQL query to count rows broken out on two
i am new in Cassandra db. need help to create data model. i have
I'm having trouble creating a query. I need to create a SQL query where
I need a little help writing a query. I have this data... vDir iNumber
I'm trying to create a query on SQL server 2005 that will check if
I need help writing a complex exclusion sql query (at least for me). First
I need some help is MS SQL Server Query. I’m not much of a
I need help writing a T-SQL query that will generate 52 rows of data
I need to create an sql query inside a ruby on rails application. I

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.