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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:49:21+00:00 2026-06-12T08:49:21+00:00

I want to combine multi rows into one but have a new row when

  • 0

I want to combine multi rows into one but have a new row when a certain value appears in a field see example data below

how data is currently in table

IncomingNumber  QueNumber Datetime
--------------  --------- -------------------
12345678        1         2012-01-01 09:01:00
12345678        2         2012-01-01 09:02:00
12345678        3         2012-01-01 09:05:00
12345678        2         2012-01-01 09:07:00
12345678        3         2012-01-01 09:08:00
12345678        1         2012-01-01 09:10:00
12345678        2         2012-01-01 09:11:00
12345678        3         2012-01-01 09:13:00
09876543        1         2012-01-01 09:01:00
09876543        2         2012-01-01 09:02:00
09876543        1         2012-01-01 09:05:00
09876543        2         2012-01-01 09:06:00
09876543        3         2012-01-01 09:08:00

how I want the data to look like

IncomingNumber  Datetime1            Datetime2            Datetime3            Datetime4            Datetime5
--------------  -------------------  -------------------  -------------------  -------------------  -------------------
12345678        2012-01-01 09:01:00  2012-01-01 09:02:00  2012-01-01 09:05:00  2012-01-01 09:07:00  2012-01-01 09:08:00
12345678        2012-01-01 09:10:00  2012-01-01 09:11:00  2012-01-01 09:13:00  Null                 Null
09876543        2012-01-01 09:01:00  2012-01-01 09:02:00  Null                 Null                 Null
09876543        2012-01-01 09:05:00  2012-01-01 09:06:00  2012-01-01 09:08:00  Null                 Null 

So every time a 1 appears in the QueNumber field it’s a new record. I know it has something to do with a CTE query but I’ve never really used them and am totally stuck

  • 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-12T08:49:22+00:00Added an answer on June 12, 2026 at 8:49 am

    Welcome to StackOverflow. It usually helps other people if you post DDL and sample data to help them help you. Here’s your table data.

    create table tbl (IncomingNumber int,QueNumber int,Datetime datetime);
    insert tbl values
        (12345678 ,1 ,'2012-01-01 09:01:00'),
        (12345678 ,2 ,'2012-01-01 09:02:00'),
        (12345678 ,3 ,'2012-01-01 09:05:00'),
        (12345678 ,2 ,'2012-01-01 09:07:00'),
        (12345678 ,3 ,'2012-01-01 09:08:00'),
        (12345678 ,1 ,'2012-01-01 09:10:00'),
        (12345678 ,2 ,'2012-01-01 09:11:00'),
        (12345678 ,3 ,'2012-01-01 09:13:00'),
        (09876543 ,1 ,'2012-01-01 09:01:00'),
        (09876543 ,2 ,'2012-01-01 09:02:00'),
        (09876543 ,1 ,'2012-01-01 09:05:00'),
        (09876543 ,2 ,'2012-01-01 09:06:00'),
        (09876543 ,3 ,'2012-01-01 09:08:00');
    

    The following query gives you what you need, up to 5 datetime columns. If any row produces more than 5 columns, the extras won’t get shown. I’ve used a pattern that is easy enough for you to expand upon. Counting from the bottom up, you only need to change the 2nd and 4th lines to cater for more datetime columns.

    ;with c1 as (
        select *,rn=ROW_NUMBER() over (partition by IncomingNumber order by DateTime)
        from tbl
    ), c2 as (
        select IncomingNumber,1 row,1 col,rn,DateTime
        from c1
        where rn=1
        union all
        select c1.IncomingNumber,
                case when c1.QueNumber=1 then c2.row+1 else c2.row end,
                case when c1.QueNumber=1 then 1 else c2.col+1 end,
                c1.rn,
                c1.DateTime
        from c2
        join c1 on c1.IncomingNumber=c2.IncomingNumber and c1.rn=c2.rn+1
    )
    select IncomingNumber,[1][DateTime1],[2][DateTime2],[3][DateTime3],[4][DateTime4],[5][DateTime5]
    from(select IncomingNumber,row,col,DateTime from c2)p
    pivot(max(DateTime)for col in([1],[2],[3],[4],[5]))v
    order by IncomingNumber,row
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to combine 2 tables into one. Let say I have: Table1 ID
I want to combine these two queries into one: mysql_query(INSERT INTO categories (name, parent)
I have a controller with 3 hashes I want to combine into 1 object
I have a multi-select checkbox. Depending on which one is checked, I want to
I want to combine two ggplots, from two different data.frames, into one plot. Below
I want to combine two ArrayList into a single one. My first arraylist looks
I want to combine two LINQ query results into one: var query1 = from
Sometimes I want to combine two statements into one to omit curly braces in
I have a folder of several ppt decks that I want to combine into
I want to combine several PDFs into one by appending the pages of each

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.