Select statement within a cell in a database table - sql

I have a basic database table. I would like to implement a functionallity that would allow to insert a select query within a random cell in the table. The result of this query would then be used as as any other cell of elementary type - in my case to compare it to an another value.
The problem is that I do not know in advance how those queries look like.
Here is an example. Say I have a an incoming parameter "score", which assumes some random integer values. I would like to see if the parameter "score" falls within the range defined between the values in Col1 and Col2, and if so happens, then to return the value in Col3.
Table1:
Col1 Col2 Col3
5 10 first row
10 15 second row
20 30 third row
* 50 forth row
* -> select avg(some_number) from Table2;
This random query can occur in any cell and is certain to return a single value. That is why I cannot use a simple JOIN statement.
Edit: Thanks Tim for suggesting to give an example.

You should look at CASE statements in SQL, and also at virtual or symbolic columns whose value is the result of an expression or function.

Related

How I can use where Condition in SQL I have comma separated value in A columns in multiple rows

Column1 EventTypes_pKey
Are 5,3
Test 1,4,5
test 1,3,5
If I am using
Select * from Table name where EventTypes_pKey in('5,1,4)
then I want that record where these value belongs the column.
How I can use where condition on the basis of EventTypes_pKey this is my Varchar column.
I want If I am selecting 5,3,4 the there should be all three row data.
Please help me.
If you are using Postgres, you can do this by by converting the value into an array and then using the overlaps operator &&
select *
from badly_designed_table
where string_to_array(eventtypes_pkey, ',')::int[] && array[5,3,4];
Online example

Access Queries doesn't get all the required records

I've imported a table from Microsoft excel and when i use query to get the required range of records using Between function, when i enter 1 and 20 the records with the value 2-9 aren't returned, or 10 and 200 11-99 aren't returned!
It is most likely because the data type of the column is defined as Text. When you have a text value access uses a text based order to lookup for the values. What you need to do is to convert the value to number, like this:
SELECT col1, col2
from Table1
WHERE Val(col1) BETWEEN 1 AND 20
But the better solution will be to fix you table structure so that the numeric values are stored in the numeric data types

SQL delete objects having and id range

I want to create a function in sql server that take as input an id range and delete all the object in that range (included the start and the end).
Let's have for example:
delete_objects_with_id(id_start,id_end)
It will delete all the objects with the id in the range id_start and id_end.
The problem is that the id are of this form: id_start=2017-0001 id_end=2017-0050 is there a sql server function that iterate on a list given a range?
first_issue content id
2011-01-01 test 2011-0001
2012-10-01 test 2012-0001
2012-11-01 test 2012-0002
2012-11-01 test 2012-0003
No, there's no builtin SQL Server function that will iterate on a list given a range.
If the id values are always nine characters, in the format four numeric digits, a dash and four more digits, then a comparison to character strings would get you the id values.
That is, you could write a query (SQL SELECT statement) to return the id values in that range,
SELECT t.id
FROM myobjects t
WHERE t.id >= '2017-0001'
AND t.id <= '2017-0050'
ORDER BY t.id
and with that query, you could define a cursor loop, and loop through (iterate) through those id values returned.
If the id values aren't in a canonical format, then the range operation isn't necessarily going to work. You'd need a mechanism to convert the id values into values that are canonical, so you can do a range.
But I'm not getting why you would need (or want) to iterate, if by "objects" you are referring to "rows" in a table. If you can write a SELECT statement that returns those rows, you could write a DELETE statement using the same predicates, and delete the rows in one fell swoop.
DELETE
FROM myobjects
WHERE id >= '2017-0001'
AND id <= '2017-0050'
It's not at all clear what you are attempting to achieve, why you would need to "iterate". But a cursor loop is one way to do that.
Again, to answer the question you asked: No. There is no "sql server function that iterate on a list given a range"
There is a simple workaround, Just remove - character and cast id as BIGINT
(This will work assuming that the first part of the id is year value and the second part is an id for each year)
DELETE FROM TABLE_1
WHERE CAST(REPLACE(id,'-','') as BIGINT) >= CAST(REPLACE(id_start,'-','') as BIGINT)
AND CAST(REPLACE(id,'-','') as BIGINT) <= CAST(REPLACE(id_end,'-','') as BIGINT)
Or use BETWEEN
DELETE FROM TABLE_1
WHERE CAST(REPLACE(id,'-','') as BIGINT) BETWEEN CAST(REPLACE(id_start,'-','') as BIGINT)
AND CAST(REPLACE(id_end,'-','') as BIGINT)

SQLite WHERE-Clause for every column?

Does SQLite offer a way to search every column of a table for a searchkey?
SELECT * FROM table WHERE id LIKE ...
Selects all rows where ... was found in the column id. But instead to only search in the column id, I want to search in every column if the searchstring was found. I believe this does not work:
SELECT * FROM table WHERE * LIKE ...
Is that possible? Or what would be the next easy way?
I use Python 3 to query the SQLite database. Should I go the route to search through the dictionary after the query was executed and data returned?
A simple trick you can do is:
SELECT *
FROM table
WHERE ((col1+col2+col3+col4) LIKE '%something%')
This will select the record if any of these 4 columns contain the word "something".
No; you would have to list or concatenate every column in the query, or reorganize your database so that you have fewer columns.
SQLite has full-text search tables where you can search all columns at once, but such tables do not work efficiently with any other queries.
I could not comment on #raging-bull answer. So I had to write a new one. My problem was, that I have columns with null values and got no results because the "search string" was null.
Using coalesce I could solve that problem. Here sqlite chooses the column content, or if it is null an empty string (""). So there is an actual search string available.
SELECT *
FROM table
WHERE (coalesce(col1,"") || coalesce(col2,"") || coalesce(col3,"") || coalesce(col4,"")) LIKE '%something%')
I'm not quite sure, if I understood your question.
If you want the whole row returned, when id=searchkey, then:
select * from table where id=searchkey;
If you want to have specific columns from the row with the correct searchkey:
select col1, col2, col3 from table where id=searchkey;
If you want to search multiple columns for the "id": First narrow down which columns this could be found in - you don't want to search the whole table! Then:
select * from table where col1=searchkey or col2=searchkey or col3=searchkey;

conditionally displaying rows based on values in next row - SQL

I have a table with few columns. I have a column which has numeric data. Eg:
1
2
3
5
4
4
I need to display row pertaining to that column based on value in the next row. I need fetch the rows for which the subsequent row is having even number.In the above example, I need to display 1 ,5 and 4.Is there a way to do it using SQL? I am using oracle database.
Oracle lead Analytical function can be used.
Oracle Lead Analytical Function
select COLUMN from
(select COLUMN, case when mod(lead(COLUMN) over (order by COLUMN),2)=0 then 1 else 0 end CONTROL
from YOURTABLE t)
where CONTROL=1
Just change the COLUMN to your column name and YOURTABLE to your table name.
I would read the values into a list and do the logic in the code. This is faster than doing fancy stuff in a query. It is also probably more understandable.