Obtain a report using a variety of different conditions

My database has a table with the following fields:

TPI
CLICKS
IMPRESSION
CLASSIFY

I am looking to retrieve 3 specific results:

1) Calculate SUM(CLICKS)/SUM(IMPRESSION) * 100 GROUPED BY TPI
2) Calculate SUM(IMPRESSION) WHERE CLASSIFY = "XYZ" GROUPED BY TPI
3) Calculate SUM(IMPRESSION) WHERE CLASSIFY = "ABC" GROUPED BY TPI

The desired output format is:

TPI 1st_data 2nd_data 3rd_data

I want the output to only include unique entries for TPI. Is there a way to achieve this using a single query?

Thank you in advance!

Answer №1

To achieve your desired result, you can utilize conditional aggregation. By incorporating a CASE statement within the argument of sum(), you can selectively include the value of impression based on the condition being met for classify.

SELECT tpi,
       sum(clicks) / sum(impression) * 100 AS first_data,
       sum(CASE classify
             WHEN 'XYZ' THEN impression
             ELSE 0
           END) AS second_data,
       sum(CASE classify
             WHEN 'ABC' THEN impression
             ELSE 0
           END) AS third_data
FROM elbat
GROUP BY tpi;

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

What is the best way to retrieve the Axios response using Express?

I've recently delved into working with Express and I'm currently struggling with making an Axios request using route parameters, and then updating some local variables based on the response. Here's a snippet of what I've been working on ...

Implementing Do Not Track in an express application

I am trying to implement a feature named "consent" in my Nodejs express app that utilizes the Do Not Track (DNT) functionality from browsers. This function is supposed to integrate Google analytics on rendered pages only when DNT is not active or its state ...

Toggle the visibility of a div by clicking on another div in a

I have created a unique design where a div features a background image of a face, along with a paragraph, two buttons, and an input box inside it. While I know this question has been asked before, my scenario is slightly different. I want the div with the ...

Error: Node application using Express does not display 'Server Started' message in console upon execution

I'm encountering an issue where I don't see the "Server Start: 3001" message on the console when I run 'node app.js' in the terminal. Below is the content of my app.js file: var createError = require('http-errors'); var exp ...

Confusion about the unwinding of the call stack in the Graph Depth-

Issue with function: hasPathDFSBroken Fix implemented in: hasPathDFS The updated version includes a forced parameter to address the issue, which I would prefer to avoid. I'm trying to comprehend why in the broken version, when the call stack unwinds ...

Chargebee encountered an error of type TypeError when attempting to create a subscription. The action

Every time I attempt to send a request with Chargebee, an error appears. Error: Failed to create subscription async createSubscriptionForTeamMember(customerId, options) { try { // Proceed with subscription creation using Chargebee API ...

What is the proper way to eliminate the 'unique' attribute from a column in the users table in Laravel 5.3?

When I first started following tutorials, I ended up with a 'username' column that was set to unique. However, I now want to change this so that my unique user identifier is linked to the email. I have completed the following steps: Ran compos ...

Setting a specific time zone as the default in Flatpickr, rather than relying on the system's time zone, can be

Flatpickr relies on the Date object internally, which defaults to using the local time of the computer. I am currently using Flatpickr version 4.6.6 Is there a method to specify a specific time zone for flatpickr? ...

What is the best way to target and manipulate the transform property of multiple div elements in JavaScript?

Looking at this code snippet, my goal is to have all the boxes rotate 180deg with a single click, without needing to apply different ID names: function rotateAllBoxes() { var boxes = document.getElementsByClassName("box"); for (var i = 0; i < box ...

Revise the code to eliminate the need for numerous SQL queries

In my Node.js and MySQL e-commerce database project, I am retrieving data from three different tables: products, product_images (linked by foreign key to product id), and reviews (also linked by foreign key to product id). Here are my three queries: const ...

Access and retrieve pkpass files from a server using React

I've exhausted all options but still can't get it to work. I'm attempting to create an Apple wallet pass using https://github.com/walletpass/pass-js. When I download the pass on the node server where I've implemented it, everything work ...

Do we really need to create our own action creators in Redux Toolkit?

As I delve into implementing the redux toolkit in my react projects, I've come up with a structure for writing slices using redux-thunk to handle API requests. import { createSlice } from "@reduxjs/toolkit"; import axios from "axios&quo ...

Angular Cascade of Multiple Requests

I am currently working on a project where I need to develop a service that can take CSV data, convert it into rows, and then make API requests for each row, adding the formatted results to an output string. To explain further, my code (written in Coffeesc ...

Exploring the beauty of ASCII art on a webpage

Having trouble displaying ASCII art on my website using a JavaScript function, the output is not as expected... This is how it should appear: https://i.sstatic.net/MCwPb.png And here is the code I am trying to implement for this purpose: function log ...

How to display all elements in a Postgreql JSON data array

In one of my PostgreSQL DB tables, there is a jsonB field with the following data: { "units": [ { "id": 299872379221376, "unitNumber": "1", "unitFloorSpace": 1, "createdTi ...

What is the process for deleting an event in Vue?

My Vue instance currently includes the following code: async mounted () { document.addEventListener('paste', this.onPasteEvent) }, beforeDestroy () { document.removeEventListener('paste', this.onPasteEvent) }, methods: { onPasteEv ...

"Contrasting the initialization of state in the constructor with managing state

Can you explain the distinction between these two methods of initializing state in ES6 other than their access to props? constructor(props) { super(props); this.state = { highlighted: 5, backgroundColor: '#f3f3f3', ...

Tips on finding the ID of a textbox using the cursor's position

In the container, there are several textboxes. When a button is clicked, I want to insert some text at the cursor position in one of the textboxes. I have managed to insert text into a specific textbox using its ID, but I am facing difficulty in identifyin ...

Ways to verify multiple radio groups to ensure none have been left unchecked

https://i.sstatic.net/EoE1A.png Is there a more elegant solution to check if either "salad" or "side dish" is left unchecked after submission? I currently have a working approach, but it feels overly complex for such a simple task. This is my current me ...

Waiting for the initial render before using document.getElementById() in next.js

I followed a tutorial that demonstrates how to access a canvas element by id using plain JavaScript. The tutorial can be found here. In the video, the method is explained around 5:10 and I adapted it for next.js in the code snippet below. import React, { u ...