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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:51:50+00:00 2026-06-12T20:51:50+00:00

I have a simple ‘Action’ table I use to record user actions that looks

  • 0

I have a simple ‘Action’ table I use to record user actions that looks like this:

{
  label (varchar),
  timestamp
}

A record is saved for every action a user completes.

I now need to output a report that looks something like this:

Date, Label1Count, Label2Count, Label3Count, ... 
dd/mm/yy, 34, 44, 55, ...
dd/mm/yy, 34, 44, 55, ...
dd/mm/yy, 34, 44, 55, ...

Where Date is the day the action took place, and each label header is one of the labels that I record.

I can output something similar no probs:

label, date, count
label, date, count
...

And order it so that labels are grouped together and then dates.

I would then need to copy and paste it in Excel to match the desired view.

Any ideas how I would get the first view using pure SQL? I could do it with a combo of python and SQL, but I’m sure SQL alone could do it?

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-06-12T20:51:51+00:00Added an answer on June 12, 2026 at 8:51 pm

    You can use crosstab in PostGreSQL, but I don’t like it.

    So I use this pattern instead…

    SELECT
      timestamp,
      SUM(CASE WHEN label = 'x' THEN 1 ELSE 0 END)           AS label_x_count,
      SUM(CASE WHEN label = 'y' THEN 1 ELSE 0 END)           AS label_y_count,
      SUM(CASE WHEN label = 'z' THEN 1 ELSE 0 END)           AS label_z_count
    FROM
      yourTable
    GROUP BY
      timestamp
    

    Obviously, this only works when you know all the labels that you are pivotting.

    If you don’t knw all the labels, and you want a result set with a dynamic number of columns, you need dynamic SQL (code that writes SQL, specifically the new lines of code for the different labels). There isn’t a single static query that can do that for you.

    Because you need dynamic SQL when you don’t know the different label values at design time, the normalised data-set is often actually the best methodology in your data layer. Your client code could then transform that normalised data set into flat file dataset in a model layer. Or similar.

    For this reason, I often recommend not pivotting the data in a SQL environment.

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

Sidebar

Related Questions

I have simple web application based on JSP. Root of application looks like this:
i have simple html5 video player like this <video id=video controls=true src=video.webm></video> If the
I have simple WebView code like this: WebView wv = (WebView) findViewById(R.id.webview1); wv.loadUrl(http://en.wikipedia.org/wiki/Book); But
I have simple user model: var user = Backbone.Model.extend({ initialize: function(){ this.bind(change:auth, function (){
I have simple view in my Ember.js application, like this. Each content element consists
I have simple SQL string like this: SELECT * FROM Office WHERE OfficeId IN
I have simple WCF Service Application (based on this tutorial : Getting Started ).
I have simple serial port program that is supposed to read the serial port
Have simple Spring Security webapp with password encoding: <security:authentication-manager alias=authenticationManager> <security:authentication-provider user-service-ref=personService> <security:password-encoder hash=md5
i have simple url of image, i want to display that image in Bitmap

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.