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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:05:42+00:00 2026-05-13T14:05:42+00:00

update: what I was calling coalesce I should have been calling pivot . I’m

  • 0

update: what I was calling coalesce I should have been calling pivot.

I’m extracting some daily usage counts from a log table. I can easily get this data one row per date/item, but I would like to pivot coalesce the columns into a single row.

e.g., I have:

date    item-to-be-counted count-of-item
10/1    foo                23
10/1    bar                45
10/2    foo                67
10/2    bar                89

I want:

date    count-of-foo     count-of-bar
10/1    23               45
10/2    67               89

Here’s my current 10g query.

select    trunc(started,'HH'),depot,count(*)
  from    logstats
 group by trunc(started,'HH'),depot
 order by trunc(started,'HH'),depot;

TRUNC(STARTED,'HH')       DEPOT      COUNT(*)
------------------------- ---------- --------
10/01/11 01.00.00         foo        28092
10/01/11 01.00.00         bar        2194
10/01/11 02.00.00         foo        3402
10/01/11 02.00.00         bar        1058

update: 11g has a pivot operation. The accepted answer shows how to do this in 9i and 10g.

  • 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-13T14:05:43+00:00Added an answer on May 13, 2026 at 2:05 pm

    What you’re looking for is pivoting – transposing the row data into columnar.

    Oracle 9i+, Using WITH/CTE:


    Use:

    WITH summary AS (
        SELECT TRUNC(ls.started,'HH') AS dt,
               ls.depot,
               COUNT(*) AS num_depot
          FROM logstats ls
      GROUP BY TRUNC(ls.started,'HH'), ls.depot)
      SELECT s.dt,
             MAX(CASE WHEN s.depot = 'foo' THEN s.num_depot ELSE 0 END) AS "count_of_foo",
             MAX(CASE WHEN s.depot = 'bar' THEN s.num_depot ELSE 0 END) AS "count_of_bar"
        FROM summary s
    GROUP BY s.dt
    ORDER BY s.dt
    

    Non-WITH/CTE Equivalent


    Use:

      SELECT s.dt,
             MAX(CASE WHEN s.depot = 'foo' THEN s.num_depot ELSE 0 END) AS "count_of_foo",
             MAX(CASE WHEN s.depot = 'bar' THEN s.num_depot ELSE 0 END) AS "count_of_bar"
        FROM (SELECT TRUNC(ls.started,'HH') AS dt,
                     ls.depot,
                     COUNT(*) AS num_depot
                FROM LOGSTATS ls
            GROUP BY TRUNC(ls.started, 'HH'), ls.depot) s
    GROUP BY s.dt
    ORDER BY s.dt
    

    Pre Oracle9i would need the CASE statements changed to DECODE, Oracle specific IF/ELSE logic.

    Oracle 11g+, Using PIVOT


    Untested:

      SELECT * 
        FROM (SELECT TRUNC(ls.started, 'HH') AS dt,
                     ls.depot
                FROM LOGSTATS ls
            GROUP BY TRUNC(ls.started, 'HH'), ls.depot)
       PIVOT (
         COUNT(*) FOR depot
       )
    ORDER BY 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE : I discovered that I was calling the permalink from another table, with
I have two scripts. running an update and calling shell_exec('svn update') and shell_exec('svn st')
I have noticed, by using log4net, that when calling ISession.Update, it updates all the
Base question: TStatusBar flickers when calling Update procedure. Ways to painlessly fix this The
If I have a line like this ContentRepository.Update(existing); that goes into datastore repository to
In Ruby on Rails, I have an update method in a controller, that is
I'm writing statistic system. It should make some output with given params. For example:
I have a form in jQuery where I update all the images via a
I have an app that plays an mp3 file and I'm trying to update
I got this error when calling bundle update: Installing curb (0.7.10) with native extensions

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.