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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:37:39+00:00 2026-05-29T23:37:39+00:00

If I have the following table Hour Clicks Conversions 12:00 2 0 1:00 3

  • 0

If I have the following table

Hour    Clicks  Conversions
12:00   2   0
1:00    3   2
2:00    1   1

How do I write a SELECT statment that dissaggrates it across both columns, so I get:

12:00   1   0
12:00   1   0
1:00    1   0
1:00    1   1
1:00    1   1
2:00    1   1

If I can’t do it with a SELECT, how do I write a stored proc that does it with a loop?

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-05-29T23:37:42+00:00Added an answer on May 29, 2026 at 11:37 pm

    This assumes there will only ever be one row for any given value of [Hour]. If there can be duplicates, I would aggregate those first (I assume the source is already an aggregation of some kind).

    DECLARE @x TABLE ([Hour] CHAR(5), Clicks INT, Conversions INT);
    
    INSERT @x SELECT '12:00',2,0
    UNION ALL SELECT '13:00',3,2
    UNION ALL SELECT '14:00',1,1;
    
    ;WITH x AS
    (
        SELECT n = ROW_NUMBER() OVER (ORDER BY s1.[object_id]) FROM sys.all_columns AS s1
        -- CROSS JOIN (SELECT 1 UNION ALL SELECT 2) AS s2 -- to double the rows if you need more
    ),
    y AS 
    (
        SELECT [Hour], Conversions, Clicks, 
        m = MAX(CASE WHEN Conversions > Clicks THEN Conversions ELSE Clicks END) 
        FROM @x GROUP BY [Hour], Conversions, Clicks
    )
    SELECT y.[Hour],
        Clicks = CASE WHEN Clicks < x.n THEN 0 ELSE 1 END,
        Conversions = CASE WHEN Conversions < x.n THEN 0 ELSE 1 END
    FROM x INNER JOIN y ON x.n <= y.m
    ORDER BY CONVERT(TIME, y.[Hour]), Clicks, Conversions;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table that lists all awarded cups: Name: Cups Columns: Month,
I have the following table: date, hour, accidents I have to get the number
I have a table which contain following columns, separately: year, month, day, hour, minute,
I have the following data in a database table, Columns : Date, Hour(from 1
I have the following table: TKG hour CAP SWITCH CALLS AAA 7 45 HH
I have a table with a few columns that are used as datetime stamps.
I have the following situation: In a table I have 4 columns: CommentsTBL: ID
So, I have got following data in a MySQL table: mysql> SELECT * FROM
Hi i have a table with the following fields 'Site Code' 'Date' 'Hour' 'Quantity'
I have a Day Strucuture Table , which has following Columns I want to

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.