Skip to content

Tag: sql

Sort COUNT(CASE WHEN) results

I am taking a database of statuses and creating the statuses as columns in order to count how many records from a network exist in each status. I’d love to sort the results based on the Partnered column DESC, but I can’t figure out how or where to do that?? Here’s my code: Here are my result…

Subtract row values of 2 columns within same table in SQL query

I’m trying to subtract the row values of 2 different columns in the same table. Table schema I want to subtract com – refund if support column is not NULL and status = 1 Output should be like as Answer Try this: The ‘as output’ names the column in your result set. The where statement f…

use quotes in sql query node.js

I have my query like so: but when I insert the query into my database (I am storing the query for analytical purposes), it fails because of the quotes. how can I fix this? I tried converting thequery to toString(), but that was a useless attempt. Any other ideas? edit: error i am recieving is: Answer This is …

Using IF in PostgreSQL

I need to check (here for an example) if the average earnings of DE workers are higher than IT workers. I tried using but it doesn’t seem to work. Is there any way to do this in PostgresSQL (I am using version 14) first_name last_name country earnings Andrea Pfeiffer DE 800 Eufrosina Marchesi IT 2975 El…

Use SQL to find best 4 consecutive weeks

I have a dataset that has a product, weeknumber, and sales for that week. I am trying to find the 4 best consecutive sales weeks in the data, so for example, Product A’s 4 best weeks are 1-4 and Product S’s best weeks are 5-8 I am using SQL to query the table to return the 4 best consecutive weeks

Convention for IDs of child tables

Every tutorial that I watched implemented child tables with two IDs, one ID for the table itself, and one ID that was just a reference to the parent’s table ID, like so: In my project, I’m doing a discord bot, and the only identifier that I need is the server’s ID. There’s no need for …

select only those who have no contact

I updated my question. I have table like this: id name contact 1 A 65489 1 A 1 A 45564 2 B so, i want table like this: id name contact 2 B Answer Using exists logic we can try: In plain English, the above query says to return any records for which we cannot find another record belonging to

Multiple sum subqueries for percentage

I need help with the following problem: I want to make a query that contains multiples sums and then takes those sums and uses them to get a percentage: percentage= s1/s1+s2. I have as input the following data: Orders shipping date, Nb of orders that have arrived late, Nb of orders that have arrived on time W…