How to create duplicate columns in sql server 2008

  • Hi,

    Below is my query,

    select t.range as [score range], count(*) as [number of occurences]
    from (
    select case
      when answer_count between 0 and 5 then ' 0- 9'
      when answer_count between 5 and 10 then '10-19'
      else '20-99' end as range
    from points where type='product_quiz') t
    group by t.range

    The output is,

    score range  number of occurences
    10-19             121327
    0- 9               129195

    But I want the output as,

    score_range  number_of_occurences   score_range1   number_of_occurences1
    10-19              121327                         10-19                     121327
    0- 9                129195                          0- 9                       129195

    Please help me to achieve this.

    Thanks in Advance,
    Best Regards,
    Poornima

  • poornima.s_pdi - Friday, January 13, 2017 1:07 AM

    Hi,

    Below is my query,

    select t.range as [score range], count(*) as [number of occurences]
    from (
    select case
      when answer_count between 0 and 5 then ' 0- 9'
      when answer_count between 5 and 10 then '10-19'
      else '20-99' end as range
    from points where type='product_quiz') t
    group by t.range

    The output is,

    score range  number of occurences
    10-19             121327
    0- 9               129195

    But I want the output as,

    score_range  number_of_occurences   score_range1   number_of_occurences1
    10-19              121327                         10-19                     121327
    0- 9                129195                          0- 9                       129195

    Please help me to achieve this.

    Thanks in Advance,
    Best Regards,
    Poornima

    SELECT

    t.[range] as [score range], COUNT(*) as [number of occurences],

    t.[range] as [score range1], COUNT(*) as [number of occurences1]

    FROM (

    SELECT CASE

    WHEN answer_count BETWEEN 0 AND 5 THEN ' 0- 9'

    WHEN answer_count BETWEEN 5 AND 10 THEN '10-19'

    ELSE '20-99' END as [range]

    FROM points

    WHERE [TYPE_ID] = 'product_quiz'

    ) t

    GROUP BY t.[range]

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Hi,
    I need to achieve the above result using sub-query for the duplicating columns.

  • poornima.s_pdi - Friday, January 13, 2017 2:02 AM

    Hi,
    I need to achieve the above result using sub-query for the duplicating columns.

    Yes you can do it that way too, although in my opinion it renders the query a little wordy.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work - Friday, January 13, 2017 2:08 AM

    poornima.s_pdi - Friday, January 13, 2017 2:02 AM

    Hi,
    I need to achieve the above result using sub-query for the duplicating columns.

    Yes you can do it that way too, although in my opinion it renders the query a little wordy.

    This is starting to sound like homework ....

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply