Skip to content

Tag: sql

Update multiple columns/rows case statement SQL

I have looked at similar questions previously but they seem to update a single column with multiple case conditions and the one answer I found for multiple columns seems a little long. Let’s say I have a table like this: Column_1 Column_2 Column_3 Column_4 Column_5 First Value 1 Second Value 2 …. …

I want to update a string in a table

I have a table, content_history with a column, doc_filename, and each row has a string value. Each string has a | and the portion in front of the | is a filepath. I want to replace that portion of the string with the correct filepath if it doesn’t already match. What is the best way to go about doing th…

How to aggregate upvote and post tables together in postgres?

I’m looking for a way to aggregate posts and the upvotes related to that post together using PostgreSQL. I want to make it similar to reddit’s upvote systems, where upvotes come already fetched and don’t need to be loaded on client side. I have made these simple tables. upvotes.sql users.sql…

How to show pivot data without directly using pivot statement?

I am trying to pivoting the data without directly using the pivot function. I have a simple table t1 which has: My expected output is: I want to achieve this without using pivot. I tried using: How can I achieve the output without pivot? Answer You are sort of there, try the following: See Example Fiddle

How to list 3 largest numbers from table in SQL?

I’m trying to unite two bits of SQL codes and list three largest numbers from it. Here is table relation diagram Query #1: Query #2: My final code shows the full list of all first and second names, simply adding largest number to all of them from orders table. How can I modify it to make it show three n…

How can multiple unique in partition tabale?

I have a SQL table like below. I have a table. I need to unique order_no column with user_id column. there is no any problem. But I have to unique data_time column because of I use partition. but date_time column has to be separated with user_id and order_no. By the way I don’t want like this unique (us…

MySQL- Find records with overlapping period

I have a table hotel_booking reference start_date numberOfDays ABC 11-08-2021 2 DEF 12-08-2021 2 GHI 13-08-2021 3 JKL 10-08-2021 2 So the first task I wanted was to get all bookings which has startDate between x and y date which is straight forward query This returns me ABC and DEF which is perfect. Now I wan…