remove alternate rows from sql query Oracle - sql

How can I remove from sql query alternate rows?
for example query returns something like this:
1 : 2
2 : 1
how can I remove second row?

Very easy to do with one statement:
create table myTable as
select 1 col1, 2 col2 from dual
union all
select 2 col1, 1 col2 from dual;
select * from myTable;
delete from myTable outtab
where exists (select null
from myTable
where col1 = outtab.col2
and col2 = outtab.col1
and col2 > col1);
select * from myTable;
drop table myTable;
The key point here is:
col2 > col1
which does not allow to remove both of the rows but only one.
To remove it from your dataset it is totally the same:
select *
from myTable outtab
where exists (select null
from myTable
where col1 = outtab.col2
and col2 = outtab.col1
and col2 > col1);
or not exists, as you wish.

Related

Match columns 1 if data not found then search column 2 oracle query

I am trying to find a way if data is not found based on col1 of a table then search with other column value
SELECT * FROM TABLE
WHERE COL1='123'
IF NULL
THEN
SELECT * FROM TABLE
WHERE COL2='ABC';
Thanks
This a typical SQL select statement involving an OR expression.
SELECT * from TABLE WHERE Col1 = '123' or Col2 = 'ABC';
You want all rows that satisfy the first condition - but if no row matches, then you want all rows that satisfy the second condition.
I would adress this with a row limiting clause (available starting version 12c):
select *
from mytable
where 'ABC' in (col1, col2)
order by rank() over(order by case col1 = 'ABC' then 1 else 2 end)
fetch first 1 row with ties
This is more efficient than union all because it does not require two scans on the table.
You can use exists with union all :
select t.*
from table t
where col1 = 123 union all
select t.*
from table t
where col2 = 'abc' and
not exists (select 1 from table t1 where t1.col1 = 123);
If you are expecting only one row, you can use:
SELECT t.*
FROM TABLE t
WHERE COL1 = '123' OR COL2 = 'ABC'
ORDER BY (CASE WHEN COL1 = '123' THEN 1 ELSE 2 END)
FETCH FIRST 1 ROW ONLY;
With multiple possible rows in the result set, I would go for:
SELECT t.*
FROM TABLE t
WHERE COL1 = '123' OR
(COL2 = 'ABC' AND
NOT EXISTS (SELECT 1 FROM TABLE t2 WHERE t2.COL1 = '123');

sql select where columns don't contain a number

I need an sql command that would select all rows that don't contain a certain number.
What I have:
Select * from table
Where (col1 != 1 or col2 != 1 or col3 != 1)
The problem is this does not select any rows that have either of the columns empty.
All 3 columns are integer type.
Use is distinct from instead of != for nullable columns:
select *
from my_table
where (
col1 is distinct from 1
or col2 is distinct from 1
or col3 is distinct from 1)

Select 1 Col_Name means in sql server 2008?

Today i found a query like
SELECT 1 [Col_name] FROM MyTable
and
SELECT [Col_name] FROM MyTable
Both seems to return the same result. I am confused.
This is the actual query:
SELECT 1 Col1 FROM [Table1] WHERE Col1 = 1
UNION
SELECT 2 Col1 FROM [Table1]
Any help is appreciated
Your query:
SELECT 1 [Col_name] FROM MyTable
will return the literal value 1 with an alias col_name. Even if col_name is an identifier in your table.
However:
SELECT [Col_name] FROM MyTable;
will select col_name from your table.
The same with:
SELECT 1 Col1 FROM [Table1] WHERE Col1 = 1
UNION
SELECT 2 Col1 FROM [Table1]
Will give you only two rows:
1
2
regardless the values in the table. Because SELECT 1 Col1 FROM [Table1] WHERE Col1 = 1 returns the literal value 1 with an alias col1, SELECT 2 Col1 FROM [Table1] returns the literal value 2 with the same alias col1 with UNION(Distinct select) set operator, will give you only two values (1, 2) since they are the only distinct values.
Live Demo
on the actual query, Col1 are called the alias for the column which has the value of 1.
if Col1 is not specified, the column with value given of 1 has no columnName.
SQLFiddle Demo (for more clarification)

Get row where column2 is X and column1 is max of column1

I have a SQLite table like this:
Col1 Col2 Col3
1 ABC Bill
2 CDE Fred
3 FGH Jack
4 CDE June
I would like to find the row containing a Col2 value of CDE which has the max Col1 value i.e. in this case June. Or, put another way, the most recently added row with a col2 value of CDE, as Col1 is an auto increment column. What is an SQL query string to achieve this? I need this to be efficient as the query will run many iterations in a loop.
Thanks.
SELECT * FROM table WHERE col2='CDE' ORDER BY col1 DESC LIMIT 1
in case if col1 wasn't an increment it would go somewhat like
SELECT *,MAX(col1) AS max_col1 FROM table WHERE col2='CDE' GROUP BY col2 LIMIT 1
Try this:
SELECT t1.*
FROM table1 t1
INNER JOIN
(
SELECT MAX(col1) MAXID, col2
FROM table1
GROUP BY col2
) t2 ON t1.col1 = t2.maxID AND t1.col2 = t2.col2
WHERE t1.col2 = 'CDE';
SQL Fiddle Demo1
1: This demo is mysql, but it should work fine with the same syntax in sqlite.
Use a subquery such as:
SELECT Col1, Col2, Col3
FROM table
WHERE Col1 = (SELECT MAX(Col1) FROM table WHERE Col2='CDE')
Add indexes as appropriate, e.g. clustered index on Col1 and another nonclustered index on Col2 to speed up the subquery.
In SQLite 3.7.11 and later, the simplest query would be:
SELECT *, max(Col1) FROM MyTable WHERE Col2 = 'CDE'
As shown by EXPLAIN QUERY PLAN, both this and passingby's query are most efficient, if there is an index on Col2.
If you'd want to see the correspondig values for all Col2 values, use a query like this instead:
SELECT *, max(Col1) FROM MyTable GROUP BY Col2

SQL get Max Column value for each Row

Sample Table : MyTable1
Col1 | Col2 | Col3
3 10 5
10 9 40
1 2 6
The Output must be:
NewColumn
10
40
6
As you can see, I just need to get the highest value from each row..
Is it possible by just using a sql select query?
Thanks in advance
You will need to use the MS Access function IIF()
IIF(condition, valueiftrue, valueiffalse)
condition is the value that you want to test.
valueiftrue is the value that is returned if condition evaluates to
TRUE.
valueiffalse is the value that is returned if condition evaluates to
FALSE.
so your query would be
SELECT IIf(C12 > Col3, C12, Col3) as newColumn
FROM
(
select IIF(Col1 > Col2, Col1, Col2) as C12, Col3
from mytable1
) x
I wonder if this would work in MS Access (can't verify myself, I'm afraid):
SELECT
(
SELECT MAX(Col)
FROM (
SELECT Col1 AS Col UNION ALL
SELECT Col2 UNION ALL
SELECT Col3
) s
) AS NewColumn
FROM yourtable
This works in the other family of database products by the same vendor, so I thought they might just happen to have added support for this in MS Access too.
Example which works:
Table1:
Col - Text
Col1 - Number
Col2 - Number
Col3 - Number
Col4 - Number
Data:
col; col1; col2; col3; col4;
a; 1; 4; 6; 7;
b; 3; 66; 23; 235;
c; 34; 634; 11; 23;
Query:
SELECT Col, Max(colx) AS MaxOfColx
FROM
(
SELECT Col, Col1 AS Colx From Table1 UNION ALL
SELECT Col, Col2 AS Colx From Table1 UNION ALL
SELECT Col, Col3 AS Colx From Table1 UNION ALL
SELECT Col, Col4 AS Colx From Table1
)
group by Col
Result:
Col; MaxOfColx
a; 7
b; 235
c; 634
This will work for as many columns as you need.