I have the following table with two columns which is generated by a query in SQL:
Lookup Value Result
1 2
2 1
4 3
3 4
As you can see it contains duplicate results. I only want it to show the first line and the third line. Does anyone know how to do this in SQL?
Thanks
There are several methods. Here is one using union all:
select t.*
from t
where col1 < col2
union all
select t1.*
from t1
where col1 > col2 and
not exists (select 1 from t t2 where t1.col1 = t2.col2 and t1.col2 = t2.col1);
If you always know that both pairs exist (as in your sample data), you can just use:
select t.*
from t
where col1 < col2;
SELECT DISTINCT
CASE WHEN Lookup Value < Result
THEN Lookup Value
ELSE Result
END as first,
CASE WHEN Lookup Value < Result
THEN Result
ELSE Lookup Value
END as second
FROM YourTable
Create Table T (
[Lookup Value] int,
Result int
)
Insert into T values (1,2),(2,1),(4,3),(3,4)
Select distinct T.[Lookup Value], T.Result
From T
where T.[Lookup Value]<=T.Result
Related
I've got a query like this
select column, count(*)
from mytable
where column in ('XXX','YYY','ZZZ',....)
group by column;
But I want also to get a row for values the aren't in the table.
Let's suppose that 'ZZZ' doesn't exist in mytable, I'd like to get:
COLUMN COUNT(*)
XXX 3
YYY 2
ZZZ 0 (or NULL)
Oracle version 10g
Thanks in advance
Mark
In general, you would need to have a second table which contains all the possible column values whose counts you want to appear in the output. For demo purposes only, we can use a CTE for that:
WITH vals AS (
SELECT 'XXX' AS val UNION ALL
SELECT 'YYY' UNION ALL
SELECT 'ZZZ'
)
SELECT t1.val, COUNT(t2.col) AS cnt
FROM vals t1
LEFT JOIN mytable t2
ON t2.col = t1.val
GROUP BY
t1.val;
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');
I have a following table with following data:
Table
Now I want to get all those users (distinct only) who do not have value 5 in Column B. What I mean is user1 has a value 5 in some row, then all user 1 rows are dismissed.
Following result should be produced:
user2 (because value is null)
user3 (no value 5)
How can I do that?
Perhaps the easiest way to do this would be aggregation by user:
SELECT ColumnA
FROM yourTable
GROUP BY ColumnA
HAVING COUNT(CASE WHEN ColumnB = 5 THEN 1 END) = 0;
One method is aggregation:
select columnA
from t
group by columnA
having sum(case when columnB = 5 then 1 else 0 end) = 0;
You can do this by Minus operator
SELECT distinct colA
FROM have
WHERE colB not in(5)
MINUS
SELECT distinct colA
FROM have
WHERE colB=5;
Using NOT EXISTS you can able to get the result
SELECT DISTINCT T1.ColumnA
FROM TableName T1
WHERE NOT EXISTS (
SELECT * FROM TableName T2 WHERE T2.ColumnA = T1.ColumnA AND T2.ColumnB <> 5
)
One more way -
SELECT DISTINCT T1.ColumnA
FROM TableName T1
WHERE T1.ColumnA NOT IN
(
SELECT T2.ColumnA FROM TableName T2 WHERE T2.ColumnB = 5
)
Need to return column 1 only if identical values are found in 2nd column of a repeating log. If any other value is seen exclude from result.
A 2
A 2
A 2
A 2
A 2
Exlude
B 2
B 1
B 2
B 3
B 2
select b. column1
from
( select *
from table
where column2 != 1
) b
where b.column2 = 2
Results:
A
You could use aggregation and HAVING:
SELECT col1
FROM tab
GROUP BY col1
HAVING COUNT(DISTINCT col2) = 1;
or if you need original rows:
SELECT s.*
FROM (SELECT t.*, COUNT(DISTINCT col2) OVER(PARTITION BY col1) AS cnt
FROM tab t) s
WHERE s.cnt = 1;
If you need the original rows, I would recommend not exists:
select t.*
from t
where not exists (select 1 from t t2 where t2.col1 = t.col1 and t2.col2 <> t.col2);
If you just want the col1 values (which makes sense to me), then I would phrase the aggregation as:
select col1
from t
group by col1
having min(col2) = max(col2);
If you want to include "all-null" as a valid option, then:
having min(col2) = max(col2) or min(col2) is null
Try this query
select column1 from (select column1,column2 from Test group by column1,column2) a group by column1 having count(column1)=1;
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)