Select 1 Col_Name means in sql server 2008? - sql

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)

Related

Aggregate function COUNT not scalar

The COUNT function doesn't result in a scalar as expected:
CREATE TABLE MyTable (Col1 INT, Col2 INT, Col3 INT)
INSERT INTO MyTable VALUES(2,3,9) -- Row 1
INSERT INTO MyTable VALUES(1,5,7) -- Row 2
INSERT INTO MyTable VALUES(2,3,9) -- Row 3
INSERT INTO MyTable VALUES(3,4,9) -- Row 4
SELECT COUNT(*) AS Result
FROM MyTable
WHERE Col3=9
GROUP BY Col1, Col2
I filter out the 3 rows where Col3=9.
In the remaining 3 rows there are two groups:
Group 1 where Col1=2 AND Col2=3 (Row 1 and 3)
Group 2 where Col1=3 AND Col2=4 (Row 4)
Finally I count those two rows.
Therefore, I expect the answer to be a scalar Result = 2 (the two groups where Col3=9).
But I got a non scalar result.
There are other ways to solve the this, so thats not the problem, but where am I thinking wrong?
Seems like you are looking for the total count of all the groups matching any condition. For this try like the following query.
SELECT COUNT(*) [Count] FROM
(
SELECT COUNT(*) AS Result
FROM MyTable
WHERE Col3=9
GROUP BY Col1, Col2
)T
SQL Fiddle
You can use subquery with singe aggregation :
select count(*)
from (select distinct col1, col2
from mytable
where col3 = 9
) t;

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)

How to delete duplicate results in SQL

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

remove alternate rows from sql query Oracle

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.