Is there any way I can select from a table without specifying the order by column in the order by clause?
select col1 from table order by col2
This works in TSQL, but doesn't appear to be allowed in Snowflake.
Yes, this is possible:
CREATE OR REPLACE TABLE tab AS
SELECT 1 AS col1, 'B' AS col2 UNION ALL
SELECT 2, 'A';
SELECT col1
FROM tab
ORDER BY col2;
Output:
Basically I have two tables. Table1 has millions of rows. Table2 has very few rows.
Table1 has field1 which is a product ID (not unique). Table2 has field2 which is just a list of productID's that need to be included from Table1 in the select statement.
SELECT Table1.*
FROM Table1
Join Table2 ON Table2.field2 = table1.field1
I have a query as:
SELECT T1.col1, T1.col2, T2.col3, T2.col4 from Table1 T1 join Table2 T2
This returns the columns(T1,T2,T3,T4) as expected.
col1 col2 col3 col4
A 11 0.5 B
Now i want to add a column to the result set with out modifying the above Select statement.
I want output result set should have another column(col8) from Table1 but i cannot add this column(col8) to the exisiting Select statement.
col1 col2 col3 col4 col8
A 11 0.5 B 9
I know there is Union in SQL server. But Union requires
Each SELECT statement within UNION must have the same number of columns
The columns must also have similar data types
The columns in each SELECT statement must also be in the same order.
I want to achieve similar to UNION but with out above conditions. I want only column to be added to the result set.
Is this achievable in SQL Server?
Any help?
Thanks in advance.
while creating view I am getting error Column names in each view or function must be unique but while framing select query i am getting only one record.i have to use col1 and col2 for both the tables..if data doesnot exist in A table it will take from B table.how i can do this.Thanks in advance..
Create View ViewName AS
select
A.col1 as col1,
A.col2 as col2,
null as col1,
B.col2 as col2
from table A,table B where A.col3=B.col3
From your comments, it looks like you are looking for this:
Create View ViewName AS
select
A.col1,
COALESCE(A.col2, B.col2) AS col2
from table A
left join table B
on A.col3=B.col3;
Use a LEFT OUTER JOIN to handle the condition where the join fails.
Explanation of the actual error:
The error says it all - your view has two columns named col1 and two columns col2. Change the columns names of one of the sets. Unlike views, an adhoc select query doesn't require unique names (or any column name at all, for that matter).
Based on your comment, you will probably need something like this:
CREATE VIEW ViewName AS
SELECT ISNULL(A.col1, B.col1) as col1, -- This will return B.Col1 if A.Col1 is null
ISNULL(A.col2, B.col2) as col2,
FROM table A INNER JOIN table B ON(A.col3 = B.col3)
Edit
Based on your comments to this answer, you can do something like this:
ALTER VIEW temp AS
SELECT COALESCE(A.col1, D.col1) as col1,
COALESCE(A.col2, B.col2, C.col2, D.col2) as col2
FROM table A
INNER JOIN table1 B ON (A.col3=B.col3)
INNER JOIN table3 C ON (A.col3=C.col3)
INNER JOIN table4 D ON (A.col3=D.col3)
Note: you wrote COALESCE(A.col1,null,null,D.col1), this is equivalent to COALESCE(A.col1, D.col1), since the coalesce function will return the first argument it receives that is not null.
LIke this we can do?
alter view temp as select COALESCE(A.col1,null,null,D.col1) as col1, COALESCE(A.col2,B.col2,C.col2,D.col2) as col2 from table A INNER JOIN table1 B INNER JOIN table3 C INNER JOIN table4 ON (A.col3=B.col3 and A.col3=C.col3 and A.col3=D.col3)
I was just wondering on how create a query that in such a way it will check if the column is in between in a reference table.
such as
SELECT *
FROM Table1 WHERE Column1 BETWEEN ( SELECT Column1 , Column2 FROM TABLE2 )
I just don't know how to implement it in a correct way.
Thank you.
If you can have overlapping ranges in Table2, and all you want are (unique) Table1 records that are in any range in Table2, then this query will do it.
SELECT *
FROM Table1
WHERE EXISTS (
SELECT *
FROM Table2
Where Table1.Column1 BETWEEN Table2.Column1 and Table2.Column2)
You can also solve this using JOINs, if the ranges in Table2 are not overlapping, otherwise you will need to use either DISTINCT or ROW_NUMBER() to pare them down to unique Table1 records.
Try This....
SELECT *
FROM Table1 as t1
INNER JOIN Table2 t2 ON t1.Column1 BETWEEN t2.Column1 AND t2.Column2
this works
SELECT * FROM table1 as t1,table2 as t2
WHERE t1.Column1 BETWEEN t2.Column1 AND t2.Column2.