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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:31:02+00:00 2026-05-27T18:31:02+00:00

I have a table called crewWork as follows : CREATE TABLE crewWork( FloorNumber int,

  • 0

I have a table called crewWork as follows :

CREATE TABLE crewWork( 
       FloorNumber int, AptNumber int, WorkType int, simTime int )

After the table was populated, I need to know how many times a change in apt occurred and how many times a change in floor occurred. Usually I expect to find 10 rows on each apt and 40-50 on each floor.
I could just write a scalar function for that, but I was wondering if there’s any way to do that in t-SQL without having to write scalar functions.

Thanks

The data will look like this:

FloorNumber  AptNumber    WorkType      simTime  
1            1            12            10  
1            1            12            25  
1            1            13            35  
1            1            13            47  
1            2            12            52  
1            2            12            59  
1            2            13            68  
1            1            14            75  
1            4            12            79  
1            4            12            89  
1            4            13            92  
1            4            14            105  
1            3            12            115  
1            3            13            129  
1            3            14            138  
2            1            12            142  
2            1            12            150  
2            1            14            168  
2            1            14            171  
2            3            12            180  
2            3            13            190  
2            3            13            200  
2            3            14            205  
3            3            14            216  
3            4            12            228  
3            4            12            231  
3            4            14            249  
3            4            13            260  
3            1            12            280  
3            1            13            295  
2            1            14            315  
2            2            12            328  
2            2            14            346  

I need the information for a report, I don’t need to store it anywhere.

  • 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-27T18:31:03+00:00Added an answer on May 27, 2026 at 6:31 pm

    If I am not missing anything, you could use the following method to find the number of changes:

    • determine groups of sequential rows with identical values;

    • count those groups;

    • subtract 1.

    Apply the method individually for AptNumber and for FloorNumber.

    The groups could be determined like in this answer, only there’s isn’t a Seq column in your case. Instead, another ROW_NUMBER() expression could be used. Here’s an approximate solution:

    ;
    WITH marked AS (
      SELECT
        FloorGroup = ROW_NUMBER() OVER (                         ORDER BY simTime)
                   - ROW_NUMBER() OVER (PARTITION BY FloorNumber ORDER BY simTime),
    
        AptGroup   = ROW_NUMBER() OVER (                         ORDER BY simTime)
                   - ROW_NUMBER() OVER (PARTITION BY AptNumber   ORDER BY simTime)
      FROM crewWork
    )
    SELECT
      FloorChanges = COUNT(DISTINCT FloorGroup) - 1,
      AptChanges   = COUNT(DISTINCT AptGroup)   - 1
    FROM marked
    

    (I’m assuming here that the simTime column defines the timeline of changes.)


    UPDATE

    Below is a table that shows how the distinct groups are obtained for AptNumber.

    AptNumber  RN  RN_Apt  AptGroup (= RN - RN_Apt)
    ---------  --  ------  ---------
    1          1   1       0
    1          2   2       0
    1          3   3       0
    1          4   4       0
    2          5   1       4
    2          6   2       4
    2          7   3       4
    1          8   5   =>  3
    4          9   1       8
    4          10  2       8
    4          11  3       8
    4          12  4       8
    3          13  1       12
    3          14  2       12
    3          15  3       12
    1          16  6       10
    …          …   …       …
    

    Here RN is a pseudo-column that stands for ROW_NUMBER() OVER (ORDER BY simTime). You can see that this is just a sequence of rankings starting from 1.

    Another pseudo-column, RN_Apt contains values produces by the other ROW_NUMBER, namely ROW_NUMBER() OVER (PARTITION BY AptNumber ORDER BY simTime). It contains rankings within individual groups of identical AptNumber values. You can see that, for a newly encountered value, the sequence starts over, and for a recurring one, it continues where it stopped last time.

    You can also see from the table that if we subtract RN from RN_Apt (could be the other way round, doesn’t matter in this situation), we get the value that uniquely identifies every distinct group of same AptNumber values. You might as well call that value a group ID.

    So, now that we’ve got these IDs, it only remains for us to count them (count distinct values, of course). That will be the number of groups, and the number of changes is one less (assuming the first group is not counted as a change).

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

Sidebar

Related Questions

I have a table called 'survey_product' with the following structure: id int(11) order_id int(11)
I have a table called person, one of the attributes is years_worked. I need
Assuming I have table called events with the columns INT id DATETIME start_time DATETIME
I have table name called Person with following column names P_Id(int), LastName(varchar), FirstName (varchar).
I have a table called rent on MySQL id, int (5), autoincrement PRIMARY KEY
I have table called 'shipped_data' https://i.stack.imgur.com/JXBej.png and need go get all 'caseNumbers' that match
I have a table called Visits : ID(PKey,Int) PatiendID(int), DoctorID(int), ExpectedDate(date), ActualDate(date), How can
I'm using mysql database. In that I have table called tbl_user I need to
I have a table called datetest CREATE TABLE DATETEST.DATETEST (FNAME VARCHAR2(20 BYTE), DOB DATE,
I have table called stats . In am inserting yes or no in 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.