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.
Related
To find for one column, I can use this.
SELECT column1
FROM `dataset.table`
WHERE column1 IS NULL OR column1 = '';
But what if i have 100 columns? Instead of going through column by column, changing column 1 to 2,3,etc.,I'm looking for one for all solution. I'm kinda new to SQL and Data Cleaning.
Consider below approach
select *
from your_table t
where regexp_contains(to_json_string(t), r':(?:null|"")[,}]')
above will return you all rows where any column either null or empty string
I am using SQL in Microsoft access. The table may contain more than one value for a bar code. I want to a query that returns one row with each unique bar code and columns for each of the first 5 values. Right now, I have a query returning the last value or the first value with min or max. How can I show the second result in the next column? or the nth result in that column? This is in Access but any other SQL help would be appreciated.
Current table:
Current query:
SELECT table.barcode, MIN(table.value)
FROM table
GROUP BY table.barcode
Current output:
Goal query output:
You can use aggregation:
SELECT table.barcode,
MIN(table.value),
IIF(MIN(table.value) = MAX(table.value), MAX(table.VALUE), NULL)
FROM table
GROUP BY table.barcode
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.
Select Last(column_name) from table_name will return the last value in the column in standard SQL. What is a similar query that will work in derby to fetch the last value in the column?
Please find the sample table below: 'secondcol' is the column name in the table.
secondcol
33
45
78
Select Last(secondcol) from table_name in sql will return 78 as output. If i want to get similar output in javadb/derby database, how can i query it? I don't want to change any order in the column values.
To select last row in table is little bit different then it is in pure SQL:
SQL:
SELECT * FROM tableName ORDER BY id DESC LIMIT 1;
DERBY:
SELECT * FROM tablename ORDER BY id DESC FETCH FIRST ROW ONLY;
Have a nice day ;)
Is there some unique key on the table where you could form the question as "give me the secondcol value on the row with the max key value"? If so, there is a technique that will work in any database engine - the idea is to concatenate the key plus any desired result data, perform a min/max, then extract the result data.
See here and here.
I have table with 3 columns. One is Id, second column is Name and the third one Description. How can I select the value in the Description field by giving the column index, 3?
Thanks in advance
You can't, from plain SQL (other than in the ORDER BY clause, which won't give you the value but will allow you to sort the result set by it).
If you are using another programming language to construct a dynamic query, you could use that to identify the column being selected by its index number.
Alternatively, you could parameterise your query to return a specific column based on a case statement - like so:
select a, b, c, d, e, ...,
case ?
when 1 then a
when 2 then b
when 3 then c
when 4 then d
when 5 then e
...
end as parameterised_column
from ...
The problem with referring to a column by an index number is that, one day, someone may add a column and break your application as the wrong value will be returned.
This principle is enforced in SQL because you can select named columns, or all columns using the * syntax.
This principle is not enforced in programming languages, where you can usually access the column by ordinal in code, but you should consider the principle before deciding to use a statement such as (psuedo code)
value = results[0].column[2].value;
It should be possible. You'd have to query the system tables (which do vary from one version of SQL to another) to get the 3rd (or Nth) column name as a string to form a following query using that column name.
In SQL 2000 the tables you'll need to start with are syscolumns with a join to sysobjects for the table name. Then the rank() function on "Colid" will give you the Nth column and "name" (shockingly) the name of the column. Once you've got that in a variable the following command can return the value, compare to it, order by it or whatever you need.
This is how you can retrieve a Column's name by passing it's index.
Here variable AcID is used as the index of the column.
Below is the code e.g
dim gFld as string
vSqlText1 = "Select * from RecMast where ID = 1000"
vSql1 = New SqlClient.SqlCommand(vSqlText1, cnnRice)
vRs1 = vSql1.ExecuteReader
if vRs1.Read then
gFld = vRs1.GetName(AcID)
msgbox gfld
end if
declare #searchIndex int
set #searchIndex = 3
select Description from tbl_name t where t.Id = #searchIndex