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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:02:58+00:00 2026-05-28T05:02:58+00:00

I have the following table: CREATE table DataDiff (CLI tinyint, id tinyint, date datetime,

  • 0

I have the following table:

CREATE table DataDiff (CLI tinyint, id tinyint, date datetime, countable bit)
insert into DataDiff values (234,1,convert(datetime,'17/12/1997',103),1)
insert into DataDiff values (234,2,convert(datetime,'09/07/1998',103),0)
insert into DataDiff values (234,3,convert(datetime,'11/08/1998',103),1)
insert into DataDiff values (234,4,convert(datetime,'29/12/1998',103),0)
insert into DataDiff values (234,5,convert(datetime,'01/02/1999',103),1)
insert into DataDiff values (234,6,convert(datetime,'03/02/1999',103),0)
insert into DataDiff values (234,7,convert(datetime,'03/02/1999',103),1)
insert into DataDiff values (234,8,convert(datetime,'29/03/1999',103),0)
insert into DataDiff values (234,9,convert(datetime,'29/03/1999',103),1)
insert into DataDiff values (234,10,convert(datetime,'31/03/1999',103),0)

I am having a hard time rewriting, without RBAR, a UDF to count the days from ID=1 up to ID=2 then from ID=3 to ID=4 and so on, always starting the count when COUNTABLE=1 and stopping when COUNTABLE=0 and then return the sum for a given CLI.

The length in days of the five intervals pictured is:

ID=2 - ID=1  = 204
ID=4 - ID=3  = 140
ID=6 - ID=5  = 2
ID=8 - ID=7  = 54
ID=10 -ID=9  = 2

for a total of 402 “countable” days from the total of 469 days between ID=1 and ID=10

  • 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-05-28T05:02:58+00:00Added an answer on May 28, 2026 at 5:02 am

    Try:

    select t1.cli ,
           sum(t1.countable  * 
               datediff(day, t1.[DATE], coalesce(t2.[DATE],getdate())) ) daycount
    from DataDiff t1
    left join DataDiff t2 on t1.cli = t2.cli and t1.id+1 = t2.id
    group by t1.cli
    

    (Assumes that if the last recorded date for the cli is countable, then it should be counted up to the current date.)

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

Sidebar

Related Questions

I have the following table: CREATE TABLE TEST(ID TINYINT NULL, COL1 CHAR(1)) INSERT INTO
I have the following table: CREATE TABLE #weeks( ProposalLineWeekID INT, StartWeek DATETIME, EndWeek DATETIME,
I have the following table: create table ARDebitDetail(ID_ARDebitDetail int identity, ID_Hearing int, ID_AdvancedRatePlan int)
I have the following table: CREATE TABLE IF NOT EXISTS `customer_list` ( `id` INT
I have the following table: CREATE TABLE [dbo].[Data] ( [Id] UNIQUEIDENTIFIER NOT NULL, [Data]
Let's say we have the following table: CREATE TABLE T ( ID INT, String1
I have the following table structure CREATE TABLE `table` ( `id` int(11) NOT NULL
I have the following table: $sql = CREATE TABLE received_queries ( sender_screen_name varchar(50), text
I have the following table in a SQLite3 database: CREATE TABLE overlap_results ( neighbors_of_annotation
I have the following table schema; CREATE TABLE `db1`.`sms_queue` ( `Id` INTEGER UNSIGNED NOT

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.