Skip to content
Advertisement

How to use data from multiple similar columns as rows in SQL

I have a table as below: ItemName 1mth_presale_cost 2mth_presale_cost 1mth_postsale_cost 2mth_postsale_cost 1000 10.1 12.1 12.5 15.1 1001 20.2 15.2 25.2 17.3 I want the result to be like below table: ItemName 1mth_cost 2mth_cost 1000 10.1 12.1 1000 12.5 15.1 1001 20.2 15.2 1001 25.2 17.3 I don’t want to use UNION for this. Answer First each row converted into two

MariaDB Created view takes too long

I have a problem. I have a table with 6 million records in it. Every record has a column dateTime, and for my code I need the most recent 16 records in ascending order. This took too long to query directly out of the original table, so I created a view using the following query: This means that the view

SQL MAX(col1, col2) with priorities

I need to select row with MAX(val1) for each group. If there is more than one row with equal val1 = MAX(val1) I need to select from them row with MAX(val2), etc. In pseudo code it should be like this: For example: I have table nums with one TEXT column name and three INTEGER columns — id, num1 and num2

Calculate total units sold and total sales value

Can you help me check my answers whether is this the right way? Im really new to database This is the question This is my answer The reason i use with read only constraint was to enhance the security! Answer The way I see it, query would look like this: If compared to yours: don’t select columns you don’t need

Conditional debit from credits and running balance using SQL

I’ve data like below in a table cryptotransactionledger id transaction_typeid transaction_type amount totalcoins 1 1 bitcoin-credit 30 30 2 2 ethereum-credit 20 50 If I spend bitcoin, I’m putting a new entry like below in the same table with transaction_typeid 3 and similarly for ethereum with transaction_typeid 4 id transaction_typeid transaction_type amount totalcoins 1 1 bitcoin-credit 30 30 2 2

Rank Date based on today’s date

I have a simple date dimension, I’m attempting to write a Window function (without much luck..) to net the following result. where 1 is today and 0 is yesterday. What can I try next? I’m unsure what to search for. Answer Assuming the dates are continuous, we don’t even need analytic functions but instead we can just use DATEDIFF: Demo

Query condition where a value is in a comma separated string

I have a table in which one of the table columns third_row stores a comma-separated list of numbers as a string but when its value is A then it means a combination of all the possible numbers. How do I approach this so that the query returns all the rows that have the third_row as A and the rest where

Advertisement