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

I'm calling collection update from ruby driver to mongodb and gets a return code
I have a function in vim that is calling update and I'd like to
I have two scripts. running an update and calling shell_exec('svn update') and shell_exec('svn st')
I'm calling a update SPROC from my DAL, passing in all(!) fields of the
I'm trying an example from M$ site regarding calling Windows Update programatically. 'http://msdn.microsoft.com/en-us/library/aa387102%28VS.85%29.aspx 'http://msdn.microsoft.com/en-us/library/aa386526%28v=vs.85%29.aspx
I'm trying to create table from view from remote server (calling procedure UPDATE_PROC), like
I am successfully getting Fluent NHibernate to update my database by calling UpdateBaseFiles: Public
I've got a quite strange problem here. I'm calling some simple code via Ajax.Updater:
UPDATE: I've been playing around with this more, and it seems like tmux's clear-history
Update : This is no longer an issue from C# 6, which has introduced

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.