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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:04:01+00:00 2026-06-07T07:04:01+00:00

I have a query that returns data like the following: Event | Sum |

  • 0

I have a query that returns data like the following:

Event | Sum | Date 
sent  | 400 | 1/1/12 
open  | 200 | 1/1/12
click | 50  | 1/1/12 
sent  | 300 | 1/8/12
open  | 150 | 1/8/12
click | 30  | 1/8/12

The SQL query:

select event, sum(thecount), rundate
from send_open_click_counts
group by event, rundate
order by rundate, event desc 

To send these values to google visualization api to render a graph I need to arrange the values like this:

['date' , 'sent' , 'opened' , 'clicked']
['1/1/12', '400' , '200' , '50']
['1/8/12', '300', '150' , '30']

I’m not really familiar with this stuff, but to me that is pivoting by date.

Here is the linq query I used to simulate my original SQL

    var dbLinqObj= from r in db.SEND_OPEN_CLICK_COUNTS
                group r by new { r.EVENT, r.RUNDATE } into g
                select new {
                    EVENT = g.Key.EVENT,
                    THECOUNT = g.Sum(r => r.THECOUNT),
                    RUNDATE = g.Key.RUNDATE

                } ;
    dbLinqObj= dbLinqObj.OrderBy(r => r.RUNDATE);

==== EDIT ==== SOLUTION FOUND =====

After digging into this more and getting a few suggestions from the comments I found an elegant solution using LINQ.

Here is the LINQ that will pivot the result properly, so that it can easily be transformed into a JSON string.

        var query = from q in db.SEND_OPEN_CLICK_COUNTS
                    group q by q.RUNDATE  into g
                    select new
      {
          Date = g.Key,
          Send = g.Where(x => x.EVENT == "sent").Sum(x => x.THECOUNT),
          Open = g.Where(x => x.EVENT == "opened").Sum(x => x.THECOUNT),
          Click = g.Where(x => x.EVENT == "clicked").Sum(x => x.THECOUNT)
      };
  • 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-07T07:04:03+00:00Added an answer on June 7, 2026 at 7:04 am

    The linq pivot you’ve used is generally how it’s done, instead of converting to an array you could try using the Aggregate linq method to build the JSON.

    For example:

    var dbLinqObj= from r in db.SEND_OPEN_CLICK_COUNTS
                group r by new { r.EVENT, r.RUNDATE } into g
                select new {
                    EVENT = g.Key.EVENT,
                    THECOUNT = g.Sum(r => r.THECOUNT),
                    RUNDATE = g.Key.RUNDATE
    
                } ;
    var dbLinqObjJSON = dbLinqObj.OrderBy(r => r.RUNDATE).Aggregate((json, x) => json + "['" + x.RUNDATE + ", "'" + x.THECOUNT + "', '" + ...  + "]";
    

    Note that Aggregate does not use a StringBuilder, so there will be a performance hit for large sequences. This is currently a problem in your posted code, since you are using String instead of StringBuilder to create your JSON. Each time you use the + operator on a String the .NET runtime needs to create a new String.

    If you want to make it as fast as possible, there is an override of Aggregate that will take a StringBuilder:

    var dbLinqObjJSON = dbLinqObj.OrderBy(r => r.RUNDATE).Aggregate(new StringBuilder("['date' , 'sent' , 'opened' , 'clicked'] , "),
                             (json, x) => json.append("['" + x.RUNDATE + "', '" + x.THECOUNT + "', '" + ...  + "], "),
                             (json) => json.ToString());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that returns data with group category and some details like
I have a query like the following that returns the correct number of rows
I have a query that returns a date, and I'd like to format it
I have a Microsoft SQL Server 2008 query that returns data from three tables
In my web application, I have a dynamic query that returns huge data to
I have a query that returns a lot of data into a CSV file.
I have a YQL query that extracts data from a page and returns it
I have been trying to add data from my ajax query that returns json
I have a LINQ query that returns data and I want to filter based
I have a query that returns a few rows, and I have the following

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.