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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:44:29+00:00 2026-06-07T17:44:29+00:00

I know there are a lot of Pivot table examples on the internet, however

  • 0

I know there are a lot of Pivot table examples on the internet, however I’m new to SQL and I’m having a bit of trouble as all the examples seem to be pertaining to aggregate functions.

Table 1:

|Date         | Tag  |Value |
|06/10 2:00pm |  A   |  65  | 
|06/10 2:00pm |  B   |  44  |
|06/10 2:00pm |  C   |  33  |
|06/10 2:02pm |  A   |  12  |
|06/10 2:02pm |  B   |  55  |
|06/10 2:02pm |  C   |  21  |
....
|06/10 1:58am |  A   |  23  |

What I would like it to look like is (table 2):

|Date        |  A  |  B  |  C  |
|06/10 2:00pm|  65 |  44 |  33 | 
|06/10 2:02pm|  12 |  55 |  21 | 
.....
|06/10 1:58am| 23 | etc.  | etc. |

(sorry for the format)

Some problems that encounter (doesn’t work with code I have found online)
I’d like to run this as a stored procedure (rather a SQL job), every 2 minutes so that this data from table 1 is constantly being moved to table 2. However I think I would need to alter the date every single time? (thats the syntax I’ve seen)
The pivot table itself seems simple on its own, but the datetime has been causing me grief.
Any code snipets or links would be greatly appreciated.

Thanks.

  • 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-07T17:44:31+00:00Added an answer on June 7, 2026 at 5:44 pm

    The pivot itself seems simple:

    select *
    from table1
    pivot (min (Value) for Tag in ([A], [B], [C])) p
    

    As for stored procedure, I would use last date saved in table2 as a filter for table1, excluding incomplete groups (I’m assuming that there will be, at some point, all three tags present, and that only last date can be incomplete. If not, you will need special processing for last date to update/insert a row).

    So, in code:

    create proc InsertPivotedTags
    as
        set NoCount ON
        set XACT_ABORT ON
    
        begin transaction
    
        declare @startDate datetime
        -- Last date from Table2 or start of time
        select @startDate = isnull (max ([Date]), '1753-01-01')
          from Table2
    
        insert into Table2
        select *
          from Table1
         pivot (min (Value) for Tag in ([A], [B], [C])) p
         where [Date] > @startDate
         -- exclude incomplete groups
           and a is not null
           and b is not null
           and c is not null
    
        commit transaction
    

    If groups can be incomplete you should remove exlude filter and add a delete statement that removes last date in case it is incomplete, and adjusts @startDate to three milliseconds earlier to get the same rows again, but now in more filled up state.

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

Sidebar

Related Questions

I know there is a lot of information in the Internet about solving this
I know there are a lot of examples of this on the web, but
I know there are a lot of these topics around but none seem to
Note: I know there are a lot of similar questions on SO. However, I
Hey all, I know there are a lot of questions out there for matching
I know there are a lot of other SO entries that seem like this
Ok guys, we all know there are all lot of typedef/struct questions out there,
I know there is a lot of discussions around custom spinners, however I am
I know there a lot of similar questions to this, but I didn't find
I know there is a lot on this topic but I can't get any

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.