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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:57:49+00:00 2026-05-13T11:57:49+00:00

I am on SQL Server 2008 and I have a table containing WA metrics

  • 0

I am on SQL Server 2008 and I have a table containing WA metrics of the following form :

CREATE TABLE #VistitorStat
(
    datelow datetime,
    datehigh datetime,
    name varchar(255),
    cnt  int
)

Two days worth of data in the table looks like so:

2009-07-25 00:00:00.000 2009-07-26 00:00:00.000   New Visitor       221
2009-07-25 00:00:00.000 2009-07-26 00:00:00.000   Unique Visitors   225
2009-07-25 00:00:00.000 2009-07-26 00:00:00.000   Return Visitors   0
2009-07-25 00:00:00.000 2009-07-26 00:00:00.000   Repeat Visitors   22
2009-07-26 00:00:00.000 2009-07-27 00:00:00.000   New Visitor       263
2009-07-26 00:00:00.000 2009-07-27 00:00:00.000   Unique Visitors   269
2009-07-26 00:00:00.000 2009-07-27 00:00:00.000   Return Visitors   4
2009-07-26 00:00:00.000 2009-07-27 00:00:00.000   Repeat Visitors   38

I want to group by the days and pivot the metrics into row form. The examples for using the PIVOT operator that I can find only show aggregation based on the SUM and MAX aggregate function. Presumably I need to convey GROUP BY semantics to the PIVOT operator — note: I can’t find any clear examples/ documentation on how to achieve this. Could someone please post the correct syntax of this — with the use of the PIVOT operator — of this query.

If this is not possible with pivot — can you come up with an elegant way of writing the query ? If not I’ll just have to generate the data in transposed form.

Post answer edit:

I have come to the conclusion that the pivot operator is unelegant (so far so that I consider it a syntax hack) — I have solved the problem by generating the data in a transposed fashion. I welcome comments.

  • 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-13T11:57:49+00:00Added an answer on May 13, 2026 at 11:57 am

    I m not sure of the result you want but this gives a line per day:

    CREATE TABLE #VistitorStat
    (
     datelow datetime,
     datehigh datetime,
     name varchar(255),
     cnt  int
    )
    
    
    insert into #VistitorStat
          select  '2009-07-25 00:00:00.000','2009-07-26 00:00:00.000',   'New Visitor',       221 
    union select  '2009-07-25 00:00:00.000',' 2009-07-26 00:00:00.000',   'Unique Visitors',   225
    union select  '2009-07-25 00:00:00.000',' 2009-07-26 00:00:00.000',   'Return Visitors',   0
    union select  '2009-07-25 00:00:00.000',' 2009-07-26 00:00:00.000',   'Repeat Visitors',   22
    union select  '2009-07-26 00:00:00.000',' 2009-07-27 00:00:00.000',   'New Visitor'    ,   263
    union select  '2009-07-26 00:00:00.000',' 2009-07-27 00:00:00.000',   'Unique Visitors',   269
    union select  '2009-07-26 00:00:00.000',' 2009-07-27 00:00:00.000',   'Return Visitors',   4
    union select  '2009-07-26 00:00:00.000',' 2009-07-27 00:00:00.000',   'Repeat Visitors',   38
    
    
    select * from #VistitorStat 
    pivot (
        sum(cnt)
        for name in ([New Visitor],[Unique Visitors],[Return Visitors], [Repeat Visitors])
    
    ) p
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 284k
  • Answers 284k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer String s = new String("abc"); the string object referred by… May 13, 2026 at 4:29 pm
  • Editorial Team
    Editorial Team added an answer SWT TrayItems seems not to implement mouselisteners. However: If you… May 13, 2026 at 4:29 pm
  • Editorial Team
    Editorial Team added an answer If you are recompiling the application between runs, note that… May 13, 2026 at 4:29 pm

Related Questions

I am using SQL Server 2008 and I have the following SQL script: Select
I have a reference application that I use to work through DDD issues, and
I am running SQL Server Reporting Services on SQL Server 2008 Standard and trying
I've searched all over and I can't figure this one out. I am working
I was trying to contact the author of a book I am reading on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.