Utilize MySQL/Javascript to determine percentages

I'm facing a challenge with an SQL query in Entrinsik's Informer. I need to calculate a percentage using JavaScript on the result, but unfortunately, Informer cannot access data down columns (such as the total for the percentage). Therefore, I have to modify my SQL query to generate this total. The current output is:

refund_code    refund_amount    month_group
-----------    -------------    -----------
ref1               10           january
ref2               20           january
ref3               30           january
ref1               40           february
ref2               50           february
ref3              60            february

What I am aiming for is something like this:

refund_code    refund_amount    month_group    month_total  
-----------    -------------    -----------    -----------  
ref1               10           january                 60  
ref2               20           january                 60  
ref3               30           january                 60  
ref1               40           february               150  
ref2               50           february               150  
ref3               60           february               150  

This led me to the following query:

SELECT mr.month_group,
    bd.transaction_code AS refund_code,
    SUM(bd.extended) AS refund_amount
FROM   billing_details AS bd
    LEFT JOIN monthly_ranges AS mr
        ON ( bd.entry_date BETWEEN mr.start_date AND mr.end_date )
WHERE  bd.transaction_code IN ( 'REFPRI', 'REFSEC', 'REFPT', 'REFREQPRI' )
    AND bd.entry_date >= '2012-01-05'
GROUP BY mr.month_group, bd.transaction_code
ORDER BY mr.month_group, bd.transaction_code

In addition, I created a second query to obtain totals per month:

SELECT mr.month_group,
    SUM(bd.extended) AS refund_amount
FROM   billing_details AS bd
    LEFT JOIN monthly_ranges AS mr
        ON ( bd.entry_date BETWEEN mr.start_date AND mr.end_date )
WHERE  bd.transaction_code IN ( 'REFPRI', 'REFSEC', 'REFPT', 'REFREQPRI' )
    AND bd.entry_date >= '2012-01-05'
GROUP BY mr.month_group
ORDER BY mr.month_group

Now, I'm exploring ways to merge these two queries together. Any suggestions?

Answer №1

To combine the two as subqueries, you could technically join them.

For example:

SELECT m.refund_code, m.refund_amount, m.month_group, t.month_total
 FROM (the refund query you have) m
 JOIN (the total query you have) t
   ON m.month_group = t.month_group

Just remember to rename 'refund_amount' in your 'total query' to 'month_total' or something similar.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Capturing keydown events exclusively in the topmost layer of the HTML document

Currently, I am developing a web application that includes an underlying HTML file with some JavaScript functionality that I cannot modify. In my code, I create a layer on top of this page using CSS with z-index: 1000;. However, I am facing an issue where ...

Regular intervals and asynchronous JavaScript and XML (AJAX) requests are

There is a simple chat tool in place to ensure the chat room stays updated: setInterval (loadLog, 2500); function loadLog(){ //Scroll height prior to the request var oldScrollHeight = document.getElementById("chatMessages").scrollHeight - 20; ...

Errors persist with Angular 2 iFrame despite attempts at sanitization

Attempting to add an iFrame within my Angular 2 application has been challenging due to the error message that keeps popping up. unsafe value used in a resource URL context The goal is to create a dynamic URL to be passed as a parameter into the iFrame ...

Utilizing CSS to set a map as the background or projecting an equirectangular map in the backdrop

How can I set an equirectangular projection like the one in this example http://bl.ocks.org/mbostock/3757119 as a background for only the chart above the X-axis? Alternatively, is it possible to use an image of a map as a CSS background instead? .grid . ...

Long-term responsibilities in Node.js

Currently, I have a node.js server that is communicating between a net socket and a python socket. The flow is such that when a user sends an asynchronous ajax request with data, the node server forwards it to Python, receives the processed data back, and ...

React 15 is found to be incompatible with React-redux due to certain compatibility

I'm currently working on upgrading to the newest version of [email protected] in my project, which also utilizes the react-redux@^4.4.0 package. However, I encountered issues when attempting to follow the upgrade instructions provided in the documenta ...

Point the direction to nextjs and react native expo web

I am currently working on redirecting a URL to another within my website, specifically in Next.js and Expo React Native Web. While I don't have an actual "About" page, I do have other pages nested under the "about" folder and am aiming to direct any ...

Hide bootstrap card on smaller screens such as sm and md

Utilizing the Bootstrap 4.1 card component, I showcase categories within the right sidebar - check out a sample image of the card here: Card example image; When viewing on smaller screens, it's preferable to collapse this large card as shown in this ...

Retrieve the name associated with the specific ID from a separate table using SQL

I have a situation involving two tables: Table1 --------------------------------- | id_table1 | id_test1| id_test2| --------------------------------- | 1 | 10 | 1 | --------------------------------- | 2 | 20 | 3 | -- ...

Encountering a 400 error while trying to implement file upload feature in Spring AngularJS -

Whenever I attempt to upload a file using Spring and AngularJS, I keep encountering the dreaded 400 Bad Request error: > error: "Bad Request" > exception: "org.springframework.web.multipart.support.MissingServletRequestPartException" > message: " ...

Building Unique Staircases with Three.js Geometry

I have been working on creating a custom three.js geometry for round staircases, but I seem to have made some errors with the vertices and indexes of the steps. Below is an example staircase that utilizes my custom geometry: https://i.sstatic.net/uQXvP.j ...

Does the current protected route that I am on restrict the user from navigating back to the Home component?

I'm having trouble figuring out how to redirect the user to the Home component when they logout. The API is functioning correctly based on my tests. However, I am unsure of how to implement the logout method in the current context to successfully log ...

Tips for implementing multi-language URL routing in a Node.js application

I am seeking guidance on how to handle routing for multi-language URLs in Node.js, Currently, I have the following routes set up where each language generates specific routes as shown below. However, with more than 5 languages, efficiency is becoming a co ...

Display the second dropdown after the user has made a selection in the first dropdown

Recently, I've been delving into the world of html/javascript and navigating through the process of implementing the code found in this specific link. While I can see the solution outlined in the link provided below, I'm struggling with how to i ...

The functionality of saving a file using the jsPDF method is not functioning properly

After dedicating four days to resolving a seemingly straightforward task that involved the jsPDF library, I found myself faced with a challenge. My goal was to save a file using this library, not just print it like I had successfully done before. This is ...

Combining Socket.io with AJAX for real-time web applications

I am currently working on a web application that relies on Socket.io for delivering notifications to users. I'm wondering if it would be more beneficial to utilize Socket.io exclusively for all client-server communication, or if mixing in traditional ...

When conditions are met, all items are added to the array

In my Angular app, I have a user list that I am trying to filter based on the condition age > 45. However, all users are being pushed into the validForScheme array instead of just those meeting the condition. I am unable to figure out what is going wron ...

What is the process for obtaining a compilation of warnings from the next.js terminal?

While running my project on VScode, I noticed a series of warnings appearing in the terminal. One specific warning that caught my attention is: The `bg-variant` mixin has been deprecated as of v4.4.0. It will be removed entirely in v5. on line 8 ...

Is there a way to display a modal before redirecting to the next page after clicking a submit button in a rails application?

In the scenario where I have two models, Employer and Jobs, consider this situation: an Employer creates an account to post a job and provides their phone number. When they fill out a new job posting and click on the post job button (Submit button), I need ...

"Enhance user experience with Sweetalert 2's customizable sorting options for select inputs

Here is the code snippet that I am currently working with: this.getdata((params.....).then((data) => { var selectOptions = []; for (let i = 0; i < data.length; i++) { selectOptions[data[i].id_calc] = data[i].surname + " " + data[i ...