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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:15:45+00:00 2026-05-13T12:15:45+00:00

Yes I’ve tried the code. My requirement is that user inputs Year and Month

  • 0

Yes I’ve tried the code. My requirement is that user inputs Year and Month & prices are shown date-wise in columns for that year and month, with first column as CompetitorID. I want my result like:

Competitors | day1  | day2  | day3  | day4 ..............|day31
================================================================
competitor 1| Price | Price | price | price..............|price 
competitor 2| Price | Price | price | price..............|price
competitor 3| Price | Price | price | price..............|price 
competitor 4| Price | Price | price | price..............|price

My Table structure is:

COMPETITORDETAIL (ID, CompetitorID, oDate, Price)

  • 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-13T12:15:45+00:00Added an answer on May 13, 2026 at 12:15 pm

    This is a lot easier. I wrote a stored proc named pivot_query that makes PIVOT a lot easier to use for SQL Server 2005+. The source for the proc is here, some examples how to use it are here.

    For your code example:

    create table Competitors
       (
       CompetitorId      integer identity,
       Name              varchar(30)
       )
    
    insert into Competitors values ('Bobs Discount Emporium')
    go
    insert into Competitors values ('Joes Really Cheap Crap')
    go
    
    create table CompetitorDetail
       (
       Id                integer identity,
       CompetitorId      integer,
       oDate             datetime,
       Price             decimal(12,3)
       )
    
    insert into CompetitorDetail values (1, getdate()-10, 10.00)
    go
    insert into CompetitorDetail values (1, getdate()-10, 10.00)
    go
    insert into CompetitorDetail values (1, getdate()-10, 10.00)
    go
    insert into CompetitorDetail values (1, getdate()-10, 10.00)
    go
    insert into CompetitorDetail values (1, getdate()-8, 11.00)
    go
    insert into CompetitorDetail values (1, getdate()-8, 11.00)
    go
    insert into CompetitorDetail values (1, getdate()-8, 11.00)
    go
    insert into CompetitorDetail values (1, getdate()-8, 11.00)
    go
    insert into CompetitorDetail values (1, getdate()-6, 12.00)
    go
    insert into CompetitorDetail values (1, getdate()-6, 12.00)
    go
    insert into CompetitorDetail values (1, getdate()-6, 12.00)
    go
    insert into CompetitorDetail values (1, getdate()-2, 13.00)
    go
    insert into CompetitorDetail values (1, getdate()-2, 13.00)
    go
    insert into CompetitorDetail values (1, getdate()-2, 13.00)
    go
    insert into CompetitorDetail values (2, getdate()-10, 14.00)
    go
    insert into CompetitorDetail values (2, getdate()-10, 14.00)
    go
    insert into CompetitorDetail values (2, getdate()-10, 14.00)
    go
    insert into CompetitorDetail values (2, getdate()-10, 14.00)
    go
    insert into CompetitorDetail values (2, getdate()-8, 15.00)
    go
    insert into CompetitorDetail values (2, getdate()-8, 15.00)
    go
    insert into CompetitorDetail values (2, getdate()-8, 15.00)
    go
    insert into CompetitorDetail values (2, getdate()-8, 15.00)
    go
    insert into CompetitorDetail values (2, getdate()-6, 16.00)
    go
    insert into CompetitorDetail values (2, getdate()-6, 16.00)
    go
    insert into CompetitorDetail values (2, getdate()-6, 16.00)
    go
    insert into CompetitorDetail values (2, getdate()-2, 18.00)
    go
    insert into CompetitorDetail values (2, getdate()-2, 18.00)
    go
    insert into CompetitorDetail values (2, getdate()-2, 18.00)
    go
    
    declare @mySQL varchar(MAX)
    
    set @mySQL = '
    select
       c.Name,
       right(cast(month(cd.oDate) + 100 as varchar(3)),2) + ''_'' + right(cast(day(cd.oDate) + 100  as varchar(3)),2) mon_day,
       cd.Price
    from
       Competitors c
    
       JOIN CompetitorDetail cd
          on (cd.CompetitorId = c.CompetitorId )
       ';
    
    exec pivot_query @mySQL, 'Name', 'Mon_Day', 'max(Price) MaxP,min(Price) MinP'
    

    which results in:

    Name                           01_09_MaxP   01_09_MinP   01_11_MaxP   01_11_MinP   01_13_MaxP   01_13_MinP   01_17_MaxP   01_17_MinP   
    ------------------------------ ------------ ------------ ------------ ------------ ------------ ------------ ------------ ------------ 
    Bobs Discount Emporium         10.000       10.000       11.000       11.000       12.000       12.000       13.000       13.000       
    Joes Really Cheap Crap         14.000       14.000       15.000       15.000       16.000       16.000       18.000       18.000       
    

    Hope that helps!

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

Sidebar

Related Questions

Yes, there is a similar question here . However, that question doesn't seem to
Yes, another NULL vs empty string question. I agree with the idea that NULL
Yes I want to read a simple a logfile into a TStringList and that
Yes, I know that in ASP.NET MVC you have to use ViewModels. But I'm
Yes, I know. The existence of a running copy of SQL Server 6.5 in
...Yes I've seen: Best Resources for Learning JavaFX? but it doesn't really answer the
Yes, There's More Than One Way To Do It but there must be a
Yes, I know you could use regular objects as associative arrays in JavaScript, but
Yes we're talking about ASCII codes. My appologies I'm not the Delphi dev here.
Yes, I do understand the difference between them. What I want to know is:

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.